在用户交互期间重置动画状态

时间:2013-11-09 13:02:18

标签: ios objective-c animation uiview uikit

我有一个用户可以触摸的圆圈,当你触摸它时它会改变大小然后恢复到原来的大小以表示你触摸它。我希望用户能够随时随地触摸它,无论其当前是否具有动画效果。我更新了我的动画以使用UIViewAnimationOptionAllowUserInteraction标志,它允许我在动画期间触摸它,但它不像我期望的那样表现。

我想我需要以某种方式停止当前正在播放的动画,将大小重置为正常然后再次播放,这是否正确,如果是这样,我该怎么做?如果不是我该怎么办?

- (void)pop{
    [view.layer removeAllAnimations];

    [UIView animateWithDuration:0.2
                      delay:0.0
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction // reverse back to original value
                 animations:^{
                     // scale down 20%
                     self.transform = CGAffineTransformMakeScale(0.8, 0.8);
                 } completion:^(BOOL finished) {
                     self.transform = CGAffineTransformMakeScale(1, 1);
                 }];
}

2 个答案:

答案 0 :(得分:0)

self.btn = [[UIButton alloc] initWithFrame:CGRectMake(200, 200, 100, 30)];
[self.btn addTarget:self action:@selector(tapDown:) forControlEvents:UIControlEventTouchDown];
[self.btn addTarget:self action:@selector(tapUp:) forControlEvents:UIControlEventTouchUpInside];


- (IBAction)tapDown:(id)sender
{
    [UIView animateWithDuration:0.2
                          delay:0.0
                        options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         // scale down 20%
                         self.btn.transform = CGAffineTransformMakeScale(0.8, 0.8);
                     } completion:nil];
}


- (IBAction)tapUp:(id)sender {
    [UIView animateWithDuration:0.2
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction // reverse back to original value
                     animations:^{
                         // scale up to 100%
                         self.btn.transform = CGAffineTransformMakeScale(1, 1);
                     } completion:nil];
}

答案 1 :(得分:0)

当然这不是你想要的?当玩家点击并弹出时,动画应重新开始?

- (void)pop{
    self.transform = CGAffineTransformMakeScale(1, 1); // If you want the animation to start over you need to reset the size before staring it again
    [view.layer removeAllAnimations];

    [UIView animateWithDuration:0.2
                  delay:0.0
                options: options:UIViewAnimationOptionAutoreverse  | UIViewAnimationOptionCurveEaseOut| UIViewAnimationOptionAllowUserInteraction // reverse back to original value
             animations:^{
                 // scale down 20%
                 self.transform = CGAffineTransformMakeScale(0.8, 0.8);
             } completion:^(BOOL finished) {
                 self.transform = CGAffineTransformMakeScale(1, 1);
             }];
}