button移动(或执行动画)之后无法响应点击事件的解决方法
来源:互联网 发布:免费发帖软件 编辑:程序博客网 时间:2024/05/21 17:32
其实问题的本质原因是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
0 0
- button移动(或执行动画)之后无法响应点击事件的解决方法
- button执行frame动画响应点击事件
- 使用animateWithDuration动画的时候无法响应点击事件
- 自定义UIView重写touchesBegan无法响应点击事件的解决方法
- Android Button按钮点击两次之后,才执行点击事件的操作。
- button响应点击事件的方法
- button 的点击事件不响应
- android Animation动画平移之后不响应点击事件
- android Animation动画平移之后不响应点击事件
- view上添加点手势 button无法响应点击事件
- view上添加点手势 button无法响应点击事件
- view上添加点手势 button无法响应点击事件
- Android 隐藏ViewPager之后,控件点击事件无法响应的问题
- Button响应鼠标点击事件
- GridView的item无法响应点击事件
- listview无法响应侧滑、点击或长按事件的应对办法
- android 防止Button的频繁点击,多次执行点击事件
- andorid布局layout监听点击事件,布局内的imageButton无法响应解决方法
- SharePoint 2013开发VS 2012必备的插件
- Perl chomp使用
- shell学习
- 仿百度文库解决方案(四)——利用JODConverter调用OpenOffice.org服务转换文档为PDF
- 将Maven项目发布到本地Maven私服Nexus
- button移动(或执行动画)之后无法响应点击事件的解决方法
- 邮件发送
- XSS攻击的介绍及防御
- android摄像头(camera)之 V4L2编程中用到的各种结构体
- linux定时任务的设置
- CI information
- CSS hack查询对照表
- mysql GROUP BY 字符相加
- 开题答辩