下面方法在分类中使用
避免重复点击
//重写下面的三个方法是为了给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;
}