CAKeyframeAnimation与CABasicAnimation下的对象都不能接收touch事件,如果你想做点击的话,只能自己在self.view(即添加btn的父视图)上面做了,具体的,给self.view添加一个UITapGestureRecognizer,然后在里面去hitTest.
需要注意一个就是btn的userinterfaceenable 需要设置为NO, 这样才能使tap的触发让self.view获取.
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tap];
self.btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
self.btn.userInteractionEnabled = NO;
self.btn.backgroundColor = [UIColor redColor];
self.btn.frame = CGRectMake((ScreenW-100)/2, (ScreenH-100)/2, 100, 100);
[self.view addSubview:self.btn];
[self startTimer];
}
- (void)startTimer {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)animationMethod:(UIButton *)sender {
sender.transform = CGAffineTransformMakeScale(0.8, 0.8);
[UIView animateWithDuration:1.0 animations:^{
sender.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
sender.transform = CGAffineTransformMakeScale(1, 1);
}];
sender.transform = CGAffineTransformMakeScale(0.8, 0.8);
}
- (void)timerAction {
[self animationMethod:self.btn];
}
- (void)tap:(UITapGestureRecognizer *)sender {
CGPoint touchPoint = [sender locationInView:self.view];
if ([self.btn.layer.presentationLayer hitTest:touchPoint]) {
NSLog(@"~~~~~~~~~~~~~~~");
}
}
网友评论