UiiOS收藏iOS Developer

iOS使用链式语法自定义UIButton

2016-07-01  本文已影响885人  Touchs

虽然iOS 自带的button带有 imageEdgeInsets和titleEdgeInsets但是个人感觉用起来有时候达不到 想要的效果,很是不爽.... 所以练习链式写法的时候 ,随手写了一个button和大家分享一下!

1.button的title.
2.button的image.
3.button的高亮效果.
4.button的点击事件.


//----------Button的创建和调用---------//
 {
    JMButton* sender1 = [JMButton creatButton:CGRectMake(100, 100, 100, 100)];
    
    sender1.ImageEdgInsets(20,0,60,60)
           .TitleEdgInsets(0,63,100,23)
           .ImagePrams(@"hehe.jpg",1)
           .TitlePrams(@"呵呵达~",17,[UIColor redColor])
           .ClickAction(^(JMButton* sender){
            NSLog(@"Action---");
        });
    
    [self.view addSubview:sender1]; 
    }
    {
    JMButton* sender2 = [JMButton creatButton:CGRectMake(100, 200, 100, 100)];
    sender2.ImageEdgInsets(0,25,40,40).TitleEdgInsets(45,32,55,25).ImagePrams(@"empty_loding.png",1).TitlePrams(@"不开心!",16,[UIColor redColor]).ClickAction(^(JMButton* sender){
        
            NSLog(@"Action---");
        });
    
    sender2.jmImagev.layer.cornerRadius = 10;
    sender2.jmImagev.layer.masksToBounds = YES;
    
    [self.view addSubview:sender2];
    }
    {
        JMButton* sender3 = [JMButton creatButton:CGRectMake(120, 300, 80, 30)];
        
        sender3.ImageEdgInsets(0,2.5,25,25)
               .TitleEdgInsets(27,2.5,55,25)
               .ImagePrams(@"111.jpg",1)
               .TitlePrams(@"发微博",16,[UIColor blueColor])
               .ClickAction(^(JMButton* sender){
        
            NSLog(@"Action---");
        });
        
        [self.view addSubview:sender3];
    }
//.h公开的方法属性
/** Imagev的frame,参数为“(x, y, w, h)”  */
@property(nonatomic,copy)
JMButton* (^ImageEdgInsets)(float left,float top,float width,float height);
/** Title的frame,参数为“(x, y, w, h)” */
@property(nonatomic,copy)
JMButton* (^TitleEdgInsets)(float left,float top,float width,float height);
/** Imagev的参数值,“(NSString* imgName,UIViewContentMode mode)” */
@property(nonatomic,copy)
JMButton* (^ImagePrams)(NSString* imgName,UIViewContentMode mode);
/** Title的参数值,“(title, fountSize, textColor)” */
@property(nonatomic,copy)
JMButton* (^TitlePrams)(NSString* title,NSInteger fountSize,UIColor*textColor);

/** 点击事件,“(id target, SEL clickAction)” */
@property(nonatomic,copy)JMButton* (^ClickAction)(void(^)(JMButton* sender));

效果截图.png

实现起来很简单,无非就是在view中创建 一个label 一个imageview

主要分享一下block实现链式的写法......

1.声明

//一个block属性返回值类型是 '自己'.   
/** Imagev的参数值,“(NSString* imgName,UIViewContentMode mode)” */
@property(nonatomic,copy)
JMButton* (^ImagePrams)(NSString* imgName,UIViewContentMode mode);
(有两个参数,一个是name ,另一个就是imageView填充方式)

2.调用

这里block属性的调用.. 调用完之后 由于返回返回值是 自身 (self), 所以可以再调用其他block属性 实现一系列的调用
 sender.ImagePrams(@"hehe.jpg",1);

3.随意链接

   sender1.ImageEdgInsets(20,0,60,60)
           .TitleEdgInsets(0,63,100,23)
           .ImagePrams(@"hehe.jpg",1)
           .TitlePrams(@"呵呵达~",17,[UIColor redColor]);

4, 点击事件---- block的嵌套

// 将点击block当另一block的参数传递出去 详情看下面.m的实现
/** 点击事件,“(id target, SEL clickAction)” */
@property(nonatomic,copy)JMButton* (^ClickAction)(void(^)(JMButton* sender));

最后贴上.m的主要代码...很简单 就是复写get方法 获取参数 赋值而已...

//block的getter 
-(JMButton *(^)(NSString *, UIViewContentMode))ImagePrams{
    
    return ^JMButton* (NSString* imgName,UIViewContentMode mode){
    
        self.jmImagev.image = [UIImage imageNamed:imgName];
        self.jmImagev.contentMode = mode;
        
        return self;
    };
}

//block的嵌套getter
-(JMButton *(^)(void(^block)(JMButton*)))ClickAction{
    
    return ^JMButton* (void(^block)(JMButton* sender)){
        
        if (!objc_getAssociatedObject(self, &clickActionKey)) {
            
            UIGestureRecognizer* gesture = objc_getAssociatedObject(self, &clickActionKey);
            
            if (!gesture) {
                
                gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAction:)];
                
                objc_setAssociatedObject(self, &clickActionKey, gesture, OBJC_ASSOCIATION_RETAIN);
                
                [self addGestureRecognizer:gesture];
            }

            objc_setAssociatedObject(self, &blockActionKey, block, OBJC_ASSOCIATION_COPY);
        }
        
        return self;
    };
}

主要还是链式 这种写法吧 希望以后有机会能排上用场..

上一篇 下一篇

猜你喜欢

热点阅读