当前位置: 代码迷 >> Iphone >> UIButton依据不同的状态设置layer
  详细解决方案

UIButton依据不同的状态设置layer

热度:94   发布时间:2016-04-25 05:41:07.0
UIButton根据不同的状态设置layer

这需要用到KVO,监听button的highlighted属性的变化,在监听回调里根据监听到得属性值设置layer

    设置监听如下

  1. [button  addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
  2. 监听回调如下
  3. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {    UIButton * bt = (UIButton* )object;   
  4.      if([keyPath isEqualToString:@"highlighted"])  
  5.     {
  6.      if(bt.state == UIControlStateNormal) {
  7.         bt.layer.bordColor  = [UIColor redColor].CGColor;
  8.      } else{   
  9.         bt.layer.bordColor  = [UIColor black].CGColor;
  10.    }                                      
  11. }          
  相关解决方案