美文网首页iOS项目
UIButton依据不同的状态设置layer

UIButton依据不同的状态设置layer

作者: 小鹏学长168 | 来源:发表于2017-01-10 15:34 被阅读47次

UIButton根据不同的状态设置layer 这需要用到KVO,监听button的highlighted属性的变化,在监听回调里根据监听到的属性值设置layer
设置监听如下

[button  addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];

监听回调如下

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {    

     UIButton * bt = (UIButton* )object;   
     if([keyPath isEqualToString:@"highlighted"])  
    {
     if(bt.state == UIControlStateNormal) {
        bt.layer.bordColor  = [UIColor redColor].CGColor;
     } else{   
        bt.layer.bordColor  = [UIColor black].CGColor;
   }                                      
}

相关文章

网友评论

    本文标题:UIButton依据不同的状态设置layer

    本文链接:https://www.haomeiwen.com/subject/pegkbttx.html