我想动画从子视图到超级视图的过渡。
我使用以下方式显示子视图:
[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];
我有什么不同的东西可以让子视图在移除时动画化吗?
我认为你需要做forView:self.view.superview
,以便与你在添加时所做的一致,因为在这种情况下,self.view
是孩子,所以你需要在父母那样做。
如果您向上定位iOS 4.0,则可以使用动画块:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
(上面的代码来自Apple's UIView documentation)
约瑟夫在斯威夫特回答:
UIView.animateWithDuration(0.2, animations: {view.alpha = 0.0},
completion: {(value: Bool) in
view.removeFromSuperview()
})
虽然从动画完成块发送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];
}];
示例如下:
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()
}
}