我想设置从子视图到超级视图的过渡动画。
我使用以下方式显示子视图:
[UIView beginAnimations:@"curlup" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.view addSubview:self.mysubview.view];
[UIView commitAnimations];
以上工作正常。它回到超级视图,我没有得到任何动画:
[UIView beginAnimations:@"curldown" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
在删除子视图时,我应该采取哪些不同的动画来制作动画?
答案 0 :(得分:109)
如果您向上定位iOS 4.0,则可以使用动画块:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
(上面的代码来自Apple's UIView documentation)
答案 1 :(得分:27)
我认为你需要做forView:self.view.superview
,以便与你在添加时所做的事情保持一致,因为在这种情况下self.view
是孩子,所以你需要做它在父母身上。
答案 2 :(得分:13)
UIView.animateWithDuration(0.2, animations: {view.alpha = 0.0},
completion: {(value: Bool) in
view.removeFromSuperview()
})
答案 3 :(得分:3)
虽然从动画完成块发送removeFromSuperview
消息的方法适用于大多数情况,但有时无法阻止视图立即从视图层次结构中删除。
例如,MKMapView
在收到消息removeAnnotations
后删除了其子视图,并且API中没有此消息的“动画”替代方法。
尽管如此,以下代码允许您在从superview中删除视图或甚至取消分配后,使用视图的视觉克隆执行任何操作:
UIView * snapshotView = [view snapshotViewAfterScreenUpdates:NO];
snapshotView.frame = view.frame;
[[view superview] insertSubview:snapshotView aboveSubview:view];
// Calling API function that implicitly triggers removeFromSuperview for view
[mapView removeAnnotation: annotation];
// Safely animate snapshotView and release it when animation is finished
[UIView animateWithDuration:1.0
snapshotView.alpha = 0.0;
}
completion:^(BOOL finished) {
[snapshotView removeFromSuperview];
}];
答案 4 :(得分:0)
以下示例:
func removeSpinningGear(cell: LocalSongsCollectionViewCell) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveLinear, animations: {
cell.spinningGearBtn.alpha = 0.0
}) { _ in
cell.spinningGearBtn.removeFromSuperview()
}
}