buttonでよく使われる2つのテクニックを共有します

3704 ワード

次の方法は分類で使用します.
重複クリックを避ける
//             button    0.5s           。
- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    [self addTarget:self action:@selector(buttonStartClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonStartClick:(id)sender {
    self.enabled = NO;
    [self performSelector:@selector(changeButtonStatus) withObject:nil afterDelay:0.5f];
}

- (void)changeButtonStatus {
    self.enabled = YES;
}

画像とテキストを含むbuttonを作成
//MARK: ==================      button   ==================
- (CGSize)setImage:(UIImage *)image title:(NSString *)title imagePostion:(UIViewContentMode)imagePostion additionalSpacing:(CGFloat)additionalSpacing state:(UIControlState)state{
    [self setImage:image forState:state];
    [self setTitle:title forState:state];
    
    return [self positionLabelRespectToImageWithTitle:title position:imagePostion spacing:additionalSpacing];
    
}
- (CGSize)positionLabelRespectToImageWithTitle:(NSString *)title position:(UIViewContentMode)position spacing:(CGFloat)spacing{
    if (self.imageView == nil || self.titleLabel == nil) {
        return CGSizeZero;
    }
    CGSize imageSize = self.imageView.intrinsicContentSize;
    CGSize titleSize = self.titleLabel.intrinsicContentSize;
    switch (position) {
        case UIViewContentModeLeft:{
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
        }
        case UIViewContentModeRight:{
            self.imageEdgeInsets = UIEdgeInsetsMake(0, (titleSize.width + spacing /2), 0, -(titleSize.width + spacing /2));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageSize.width + spacing/2), 0, (imageSize.width + spacing /2));
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
        }
        case UIViewContentModeBottom:
        case UIViewContentModeTop:{
            CGFloat imageOffsetX = (imageSize.width + titleSize.width) / 2 - imageSize.width/2;
            CGFloat imageOffsetY = imageSize.height/2 + spacing/2;
            CGFloat titleOffsetX = (imageSize.width + titleSize.width/2) - (imageSize.width + titleSize.width) / 2;
            CGFloat titleOffsetY = titleSize.height/2 + spacing/2;
            CGFloat changedWidth = titleSize.width + imageSize.width - MAX(titleSize.width, imageSize.width);
            CGFloat changedHeight = titleSize.height + imageSize.height + spacing - MAX(imageSize.height, titleSize.height);
            if (position == UIViewContentModeTop) {
                self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
                self.titleEdgeInsets = UIEdgeInsetsMake(titleOffsetY, -titleOffsetX, -titleOffsetY, titleOffsetX);
                self.contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, -changedWidth / 2, changedHeight - imageOffsetY, -changedWidth / 2);
            } else {
                self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX,-imageOffsetY, -imageOffsetX);
                self.titleEdgeInsets = UIEdgeInsetsMake( -titleOffsetY, -titleOffsetX, titleOffsetY, titleOffsetX);
                self.contentEdgeInsets = UIEdgeInsetsMake(changedHeight - imageOffsetY, -changedWidth / 2, imageOffsetY, -changedWidth / 2);
            }
        }
            
            break;
            
        default:
            break;
    }
    return self.intrinsicContentSize;
}