当前位置: 代码迷 >> Iphone >> 自定义UIButton-iPhone旋钮控件点击效果写法
  详细解决方案

自定义UIButton-iPhone旋钮控件点击效果写法

热度:83   发布时间:2016-04-25 06:07:42.0
自定义UIButton--iPhone按钮控件点击效果写法

当我们自定义了一个UIButton时,如果采用重绘的方式,将drawRect事件重写了,原有自带的点击的效果就没有了,这时,我们也要自己来重新写的。

例如下面效果的按钮

UIButton 自定义

- (id) initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self addObserver:self forKeyPath:@"highlighted" options:0 context:nil]; //增加对highlighted属性的观察
}
return self;
}

-(void)dealloc
{
[self removeObserver:self forKeyPath:@"highlighted"];//移除对highlighted属性的观察
[super dealloc];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"highlighted"]) {
[self setNeedsDisplay];//当按钮被按下时,重绘按钮
}
}

完整代码请点这里

  相关解决方案