UIButton常用的类别

2017-08-19  本文已影响10人  猪队友小L
#import <UIKit/UIKit.h>

@interface UIButton (Extension)

+ (UIButton *)buttomWithFrame:(CGRect)frame
                        title:(NSString *)title
                         font:(UIFont *)font
                   titleColor:(UIColor *)titleColor
                titleDisColor:(UIColor *)titleDisColor
                  normalImage:(UIImage *)normalImage
                    highImage:(UIImage *)highImage
                     disImage:(UIImage *)disImage
                 cornerRadius:(CGFloat )cornerRadius
                       target:(id)target
                          tag:(NSInteger)tag
                       action:(SEL)action;

+ (UIButton *)buttonImageWithFrame:(CGRect)frame
                        title:(NSString *)title
                         font:(UIFont *)font
                   titleColor:(UIColor *)titleColor
                titleDisColor:(UIColor *)titleDisColor
                  normalImage:(UIImage *)normalImage
                    highImage:(UIImage *)highImage
                     disImage:(UIImage *)disImage
                 cornerRadius:(CGFloat )cornerRadius
                       target:(id)target
                          tag:(NSInteger)tag
                       action:(SEL)action;


+ (UIButton *)buttomWithFrame:(CGRect)frame
                  normalColor:(UIColor *)normalColor
                    highColor:(UIColor *)highColor
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                       target:(id)target
                          tag:(NSInteger)tag
                       action:(SEL)action;

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
           titleDisabledColor:(UIColor *)disColor
                         font:(UIFont *)font
                       target:(id)target
                       action:(SEL)action;

+ (UIButton *)buttonWithFrame:(CGRect)frame
                  normalImage:(UIImage *)normalImage
                  selectImage:(UIImage *)selectImage
                       target:(id)target
                       action:(SEL)action;

/**
 *  button类别
 *
 *  @param frame         位置
 *  @param title         标题
 *  @param titleColor    标题颜色
 *  @param selectedColor 选中时标题颜色
 *  @param font          大小
 *  @param target        目标
 *  @param action        方法
 *
 *  @return button
 */
+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
           titleSelectedColor:(UIColor *)selectedColor
                         font:(UIFont *)font
                       target:(id)target
                       action:(SEL)action;

+ (void)setCaptchButton:(UIButton *)button;

- (void)enableTouch;

@end
#import "UIButton+Extension.h"

@implementation UIButton (Extension)

+ (UIButton *)buttomWithFrame:(CGRect)frame
                        title:(NSString *)title
                         font:(UIFont *)font
                   titleColor:(UIColor *)titleColor
                titleDisColor:(UIColor *)titleDisColor
                  normalImage:(UIImage *)normalImage
                    highImage:(UIImage *)highImage
                     disImage:(UIImage *)disImage
                 cornerRadius:(CGFloat )cornerRadius
                       target:(id)target
                          tag:(NSInteger)tag
                       action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:titleDisColor forState:UIControlStateDisabled];
    [button.titleLabel setFont:font];
    [button setBackgroundImage:normalImage forState:UIControlStateNormal];
    [button setBackgroundImage:highImage forState:UIControlStateHighlighted];
    [button setBackgroundImage:highImage forState:UIControlStateSelected];
    [button setBackgroundImage:disImage forState:UIControlStateDisabled];
    [button setTag:tag];
    button.exclusiveTouch = YES;
    if (cornerRadius!=0) {
        
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = cornerRadius;
    }
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+(UIButton *)buttonImageWithFrame:(CGRect)frame title:(NSString *)title font:(UIFont *)font titleColor:(UIColor *)titleColor titleDisColor:(UIColor *)titleDisColor normalImage:(UIImage *)normalImage highImage:(UIImage *)highImage disImage:(UIImage *)disImage cornerRadius:(CGFloat)cornerRadius target:(id)target tag:(NSInteger)tag action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:titleDisColor forState:UIControlStateDisabled];
    [button.titleLabel setFont:font];
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:highImage forState:UIControlStateHighlighted];
    [button setImage:highImage forState:UIControlStateSelected];
    [button setImage:disImage forState:UIControlStateDisabled];
    [button setTag:tag];
    button.exclusiveTouch = YES;
    if (cornerRadius!=0) {
        
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = cornerRadius;
    }
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (UIButton *)buttomWithFrame:(CGRect)frame
                  normalColor:(UIColor *)normalColor
                    highColor:(UIColor *)highColor
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
                       target:(id)target
                          tag:(NSInteger)tag
                       action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button.titleLabel setFont:[UIFont fontWithName:@"ArinormalColoral" size:18.0f]];
    [button setFrame:frame];
    [button setTag:tag];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage createImageWithColor:normalColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage createImageWithColor:highColor] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage createImageWithColor:highColor] forState:UIControlStateSelected];
    [button setBackgroundImage:[UIImage createImageWithColor:normalColor] forState:UIControlStateDisabled];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 2;
    button.exclusiveTouch = YES;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
           titleDisabledColor:(UIColor *)disColor
                         font:(UIFont *)font
                       target:(id)target
                       action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:disColor forState:UIControlStateDisabled];
    [button.titleLabel setFont:font];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}
/**
 *  button类别
 *
 *  @param frame         位置
 *  @param title         标题
 *  @param titleColor    标题颜色
 *  @param selectedColor 选中时标题颜色
 *  @param font          大小
 *  @param target        目标
 *  @param action        方法
 *
 *  @return button
 */
+ (UIButton *)buttonWithFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)titleColor
           titleSelectedColor:(UIColor *)selectedColor
                         font:(UIFont *)font
                       target:(id)target
                       action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:selectedColor forState:UIControlStateSelected];
    [button.titleLabel setFont:font];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (UIButton *)buttonWithFrame:(CGRect)frame
                  normalImage:(UIImage *)normalImage
                  selectImage:(UIImage *)selectedImage
                       target:(id)target
                       action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:selectedImage forState:UIControlStateHighlighted];
    [button setImage:selectedImage forState:UIControlStateSelected];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;

}

+ (void)setCaptchButton:(UIButton *)button{
    button.enabled = NO;
    
    __block int timeout = 90; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束,关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                button.enabled = YES;
                [button setTitle:@"获取验证码" forState:UIControlStateDisabled];
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [button setTitle:[NSString stringWithFormat:@"%d秒后重试",timeout] forState:UIControlStateDisabled];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
- (void)enableTouch
{
    self.enabled = NO;

    [self performSelector:@selector(touch) withObject:nil afterDelay:2];
}

- (void)touch{

    self.enabled = YES;
}
@end

上一篇下一篇

猜你喜欢

热点阅读