button移动(或执行动画)之后无法响应点击事件的解决方法
其实问题的本质原因是button在移动之后一直处于按下的状态.导致无法触发方法.我们要做的就是在移动后取消button的按下状态.以下为MyButton的.m文件.我的自定义button模仿了苹果的虚拟home键.拖拽后有自动向左或者向右(根据坐标)的效果.
@interface MyButton : UIButton { CGPoint beginPoint; } @property(nonatomic) BOOL dragEnable; @end以下为.m文件
#import "MyButton.h" @implementation MyButton - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor redColor]; } return self; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; if (!_dragEnable) { return; } UITouch *touch = [touches anyObject]; beginPoint = [touch locationInView:self]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (!_dragEnable) { return; } UITouch *touch = [touches anyObject]; CGPoint nowPoint = [touch locationInView:self]; float offsetX = nowPoint.x - beginPoint.x; float offsetY = nowPoint.y - beginPoint.y; self.center = CGPointMake(self.center.x + offsetX, self.center.y +offsetY); //CGPoint next = [touch previousLocationInView:self]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (!_dragEnable) { return; } self.center = CGPointMake(self.center.x, self.center.y); if (self.center.x<(320-self.center.x)) { CAKeyframeAnimation *frameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; frameAnimation.duration = 1*(self.center.x)/160; frameAnimation.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:self.center],[NSValue valueWithCGPoint:CGPointMake(self.frame.size.width/2, self.center.y)], nil]; [self.layer addAnimation:frameAnimation forKey:@"sss"]; self.center = CGPointMake(self.frame.size.width/2, self.center.y); } else{ CAKeyframeAnimation *frameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; frameAnimation.duration = 1*(320 - self.center.x)/160; frameAnimation.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:self.center],[NSValue valueWithCGPoint:CGPointMake(320 - self.frame.size.width/2, self.center.y)], nil]; [self.layer addAnimation:frameAnimation forKey:@"sss"]; self.center = CGPointMake(320 - self.frame.size.width/2, self.center.y); } NSLog(@"%@",NSStringFromCGRect(self.frame)); [super touchesEnded: touches withEvent: event]; }问题的关键是
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event中的 [super touchesBegan:touches withEvent:event];
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 中的 [super touchesEnded: touches withEvent: event];
加入这两个方法后,button就可以取消按下状态了.
转载请注明本文源地址 blog.csdn.net/u013082522/article/details/183172uibutton91
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。