button执行frame动画响应点击事件
来源:互联网 发布:it培训机构排名 编辑:程序博客网 时间:2024/06/05 08:59
如果按照常规方法去写这个过程,代码如下:
@interface ViewController ()@property (nonatomic, strong) UIButton * button;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; self.button.backgroundColor = [UIColor redColor]; [self.button addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.button]; //设置动画匀速并且在运动期间打开用户交互 [UIView animateWithDuration:10.f delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{ self.button.frame = CGRectMake(0, 400, 100, 100); } completion:^(BOOL finished) { }];}- (void)btnClicked{ NSLog(@"button clicked");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
会发现在动画还没有运行到目标位置的时候,直接点击目标位置也会响应点击事件。
更改代码如下:通过对比点击位置与动画当前坐标的方法来准确响应点击事件
@interface ViewController ()@property (nonatomic, strong) UIButton * button;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; self.button.backgroundColor = [UIColor redColor];// 关闭按钮的用户交互,使得点击事件穿透按钮,到达self.view self.button.userInteractionEnabled = NO; [self.view addSubview:self.button]; //设置动画匀速并且在运动期间打开用户交互 [UIView animateWithDuration:10.f delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{ self.button.frame = CGRectMake(0, 400, 100, 100); } completion:^(BOOL finished) { }];}//点击事件- (void)btnClicked{ NSLog(@"button clicked");}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //获取点击点坐标 CGPoint touchPoint = [[touches anyObject] locationInView:self.view]; //获取动画当前中心点坐标 CGPoint currentPosition = [[self.button.layer presentationLayer] position]; if (touchPoint.x > currentPosition.x - 50 && touchPoint.x < currentPosition.x + 50 && touchPoint.y > currentPosition.y - 50 && touchPoint.y < currentPosition.y + 50) { [self btnClicked]; }}
更改后,移动中的button就可以正常识别点击事件了
本文借鉴http://www.cnblogs.com/YouXianMing/p/4149103.html
0 0
- button执行frame动画响应点击事件
- button移动(或执行动画)之后无法响应点击事件的解决方法
- Button响应鼠标点击事件
- 按钮在执行frame动画的时候怎么响应触发事件?
- Android listView 中响应Button点击事件
- Android listView 中响应Button点击事件
- Android listView 中响应Button点击事件
- Android listView 中响应Button点击事件
- button响应点击事件的方法
- activity响应listview中button点击事件
- tableHeaderView中的button点击事件不响应
- button 的点击事件不响应
- ListView中嵌入Button,并响应Button点击事件
- ListView中嵌入Button,并响应Button点击事件
- android 防止Button连续点击而响应多次点击事件
- cocos2d-x 3.0 点击button,点击事件响应两次
- android 防止Button的频繁点击,多次执行点击事件
- UIImageView上添加Button不能响应点击事件
- Codeforces Round #365 (Div. 2) 703B Mishka and trip 水题
- 【HDU】5793 A Boring Question
- 遗传算法
- Java.Web学习笔记 Hibernate
- ibatis 到 MyBatis区别
- button执行frame动画响应点击事件
- AR引擎vuforia源码分析、中文注释(1)
- 关于openvpn的server.conf配置文件的参数
- 【OpenCV】VS2015与OpenCV2.4.11配置
- Laravel框架中简单使用中间件
- Oracle(入门四)
- asp.net三层架构连接Oracle 11g详解
- Hibernate(二)
- 开个博客瞧一瞧