下面方法在分類中使用
避免重復(fù)點(diǎn)擊
//重寫下面的三個方法是為了給button添加一個0.5s之內(nèi)不能重復(fù)點(diǎn)擊的效果聪富。
- (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;
}
創(chuàng)建一個既包含圖片又包含文字的button
//MARK: ==================創(chuàng)建圖片文字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;
}