删除后反向CABasicAnimation

时间:2014-04-10 19:32:11

标签: ios objective-c core-animation cabasicanimation

我正在使用CABasicAnimation实例在点按时为UIControl缩放设置动画。功能应该是控件将缩放到0.9,然后在按下时保持在那里,当用户松开他/她的手指时,它将放大回原来的大小。

到目前为止,我已经创建了动画,它完美地运行,直到它将动画回原始大小。我只是删除原始动画,而不是创建单独的动画来进行放大。有没有一种动画这种去除的方式,以便它有效地看起来像控件缩小和放大以响应触摸?

- (void)shrinkView {
    CABasicAnimation *shrink = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    shrink.toValue = @(0.9);
    shrink.duration = 0.5;
    shrink.delegate = self;
    shrink.removedOnCompletion = NO;
    shrink.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:shrink forKey:@"shrink"];
}

- (void)enlargeView {
    [self.layer removeAnimationForKey:@"shrink"];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self reduceBackgroundOpacity];
    [self shrinkView];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self increaseBackgroundOpacity];
    [self enlargeView];
}

1 个答案:

答案 0 :(得分:2)

你正在努力实现这一目标。关于removedOnCompletionremoveAnimationForKey:的内容是虚假的。只需在触摸开始时为缩放变换设置动画,并在触摸结束时设置回动画识别变换。

(你甚至不需要使用CABasicAnimation;一个简单的UIView动画会做 - 除非你使用autolayout,在这种情况下CABasicAnimation会更好。)