2

我有一个自定义视图,我用动画呈现弹跳效果。现在,我希望它以类似于收缩然后消失的方式消失。

下面这段代码以弹跳效果呈现我的视图工作正常:

self.componentDetailController.view.center = iGestureRecognizer.view.center;
self.componentDetailController.view.alpha = 0.0;
self.componentDetailController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);

 [UIView animateWithDuration:(0.2) animations:^{
     self.componentDetailController.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
     self.componentDetailController.view.alpha = 0.5;
     self.componentDetailController.view.transform = CGAffineTransformMakeScale(1.05, 1.05);
 } completion:^(BOOL iFinished) {
     [UIView animateWithDuration:(0.1) animations:^{
         self.componentDetailController.view.alpha = 0.90;
         self.componentDetailController.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
         self.componentDetailController.view.transform = CGAffineTransformMakeScale(0.98, 0.98);
     } completion:^(BOOL iFinished) {
         self.componentDetailController.view.alpha = 1.0;
         self.componentDetailController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
         self.componentDetailController.view.transform = CGAffineTransformIdentity;
     }];
 }];

我已经编写了下面的代码来缩小和关闭不起作用的视图。它将视图稍微移到了前面,但并没有消除它。任何线索这里有什么问题:

 - (void)cancelButtonPressed {
    self.componentDetailController.view.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:(0.1) animations:^{
        self.componentDetailController.view.alpha = 1.0;
        self.componentDetailController.view.transform = CGAffineTransformMakeScale(1.05, 1.05);
    } completion:^(BOOL iFinished) {
        [UIView animateWithDuration:(0.4) animations:^{
            self.componentDetailController.view.center = self.tappedComponentView.center;
            self.componentDetailController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
            self.componentDetailController.view.backgroundColor = [UIColor clearColor];
            self.componentDetailController.view.alpha = 0.0;
        } completion:^(BOOL iFinished) {
            [self.componentDetailController.view removeFromSuperview];
        }];
    }];

    self.componentDetailController = nil;
    [self enableBackView];
}

这里 self.tappedComponentView.center 和 self.tappedComponentView.center 是一样的。

4

1 回答 1

5

+animateWithDuration:animations:completion:调用立即返回,因此一旦第二个动画运行,所有后续的视图转换调用都将无处可去self.componentDetailControllernil存储self.componentDetailController.view到它自己的变量中并在你的动画块中引用它。例如:

UIView *detailView = self.componentDetailController.view;
detailView.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:0.1 animations:^{
    detailView.alpha = 1;
// …
}];

self.componentDetailController = nil;
于 2012-10-02T21:32:36.580 回答