缩小然后解雇UIView的问题

时间:2012-10-02 21:30:01

标签: iphone ios cocoa-touch uiview uiviewcontroller

我有一个自定义视图,我提供动画给出弹跳效果。现在,我希望它以类似缩小的方式消失然后消失。

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

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相同。

1 个答案:

答案 0 :(得分:5)

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

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

self.componentDetailController = nil;