[UIView animateWithDuration:1.4 animations:^{
sender.center = CGPointMake(sender.center.x, sender.center.y+3);
} completion:^(BOOL finished) {
NSLog (@"動(dòng)畫結(jié)束了");
}];
在這個(gè)簡(jiǎn)單的動(dòng)畫開(kāi)始后,單擊屏幕上的btnTest并不能觸發(fā)相應(yīng)事件孽水,因?yàn)閎tnTest的frame在動(dòng)畫結(jié)束位置躏救,而并不在動(dòng)畫中的位置。所以只有單擊動(dòng)畫結(jié)束位置才能響應(yīng)其事件姜凄。如何能做到在動(dòng)畫中單擊屏幕上的button能正確響應(yīng)其事件呢?大致有兩種辦法趾访。
1态秧、第一種是使用NSTimer替代上面的動(dòng)畫。在此不多做介紹扼鞋。
2申鱼、首先我們需要知道的是控件在動(dòng)畫中展示在屏幕中的是layer.presentationLayer,我們也可以稱之為展示層愤诱,可以根據(jù)它得到當(dāng)前動(dòng)畫中的坐標(biāo):layer.presentationLayer.frame。
那么我們就可以通過(guò)屏幕的觸摸方法得到觸摸點(diǎn)的坐標(biāo)與上面動(dòng)畫中的坐標(biāo)進(jìn)行判斷
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches]; //返回與當(dāng)前接收者有關(guān)的所有的觸摸對(duì)象
UITouch *touch = [allTouches anyObject]; //視圖中的所有對(duì)象
CGPoint point = [touch locationInView:self];//觸摸點(diǎn)的坐標(biāo)
CGRect frame = [btn.layer presentationLayer].frame;//移動(dòng)中的坐標(biāo)
if (CGRectContainsPoint(frame, point))
{
[self click:btn];
}
}
ps:btn.userInteractionEnabled = NO; button的交互必須關(guān)掉捐友,是事件能夠向下傳遞被view接受淫半,從而響應(yīng)touch事件。