简体   繁体   English

在CAKeyframeAnimation之后,UIButton不会接收到触摸

[英]UIButton doesn't receive touch after CAKeyframeAnimation

My situation: I've a UIButton that it's animate with a CAKeyframeAnimation that's declared as a category on UIView : 我的情况:我有一个UIButton ,它的动画是一个CAKeyframeAnimation ,它在UIView上被声明为一个类别:

    CAKeyframeAnimation * scale =  [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scale.duration = duration;
    scale.beginTime = delay;
    scale.fillMode = kCAFillModeForwards;

    NSMutableArray * times = [[NSMutableArray alloc] init];

    scale.values = values;
    scale.timingFunctions = times;

    CAAnimationGroup * group = [CAAnimationGroup animation];
    [group setDelegate:self];
    [group setDuration:duration + delay];
    [group setFillMode:kCAFillModeForwards];
    [group setRemovedOnCompletion:NO];
    [group setAnimations:[NSArray arrayWithObject:scale]];

    [self.layer addAnimation:group forKey:@"scale"];

The problem is that after the animation, the button doesn't receive touch. 问题是在动画之后,按钮没有接收到触摸。 If I remove the animation all works fine. 如果我删除动画一切正常。 Does anyone know what I'm doing wrong ? 有谁知道我做错了什么?

Thanks 谢谢

You shouldn't use kCAFillModeForwards and removedOnCompletion = NO to stick the animated layer in the final position. 您不应该使用kCAFillModeForwards和removedOnCompletion = NO来将动画图层粘贴到最终位置。 This will not work for a control, and results in the behavior you noticed. 这不适用于控件,并导致您注意到的行为。

Instead, set the final state for the button before adding the animation to it's layer. 而是在将动画添加到其图层之前设置按钮的最终状态。

self.layer.transform = CGAffineTransformMakeScale(finalScaleX, finalScaleY);
[self.layer addAnimation:group forKey:@"scale"];

The problem is that the animation only changes the presentation of the button but the touch target is still the same as before. 问题是动画只会改变按钮的显示,但触摸目标仍然与之前相同。 You should either remove the animation after completion and set the transform on the button or ht test the presentation layer. 您应该在完成后删除动画并在按钮上设置变换或者测试表示层。

I wrote about hit testing animating layers in a blog post that explains it in more detail. 在博客文章中写了关于命中测试动画层的文章 ,更详细地解释了它。

You must insert the following line of code self.layer.transform = CGAffineTransformMakeScale(finalScaleX, finalScaleY); 您必须插入以下代码行self.layer.transform = CGAffineTransformMakeScale(finalScaleX, finalScaleY); when the animation is ended. 动画结束时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM