我有一个自定义视图,我用动画呈现,提供弹跳效果。现在,我希望它以类似于收缩然后消失的方式消失。
下面一段代码以弹跳效果呈现我的视图,工作正常:
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相同。
+animateWithDuration:animations:completion:
调用会立即返回,因此一旦第二个动画运行,self.componentDetailController
就会nil
,并且所有后续的视图转换调用都无处可去。将self.componentDetailController.view
存储到其自己的变量中,并在动画块中引用该变量。例如:
UIView *detailView = self.componentDetailController.view;
detailView.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:0.1 animations:^{
detailView.alpha = 1;
// …
}];
self.componentDetailController = nil;