如何使溶解animation在iPhone上更改视图?

如何在iphone中改变视图溶解animation?

解散效应:一个视图正在改变另一个视图而没有任何移动。

非常感谢您的帮助!

您正在寻找的animation是:

[UIView animateWithDuration: 1.0 animations:^{ view1.alpha = 0.0; view2.alpha = 1.0; }]; 

使用该animation的更完整的解决scheme可能是:

 - (void) replaceView: (UIView *) currentView withView: (UIView *) newView { newView.alpha = 0.0; [self.view addSubview: newView]; [UIView animateWithDuration: 1.0 animations:^{ currentView.alpha = 0.0; newView.alpha = 1.0; } completion:^(BOOL finished) { [currentView removeFromSuperview]; }]; } 

您也可以在ios5和更高版本中使用UIViewAnimationOptionTransitionCrossDissolve …

 [UIView transitionFromView:currentView toView:nextView duration:2 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) { [currentView removeFromSuperview]; }]; 

UIView有一个名为transition(from:to:duration:options:completion:)的方法transition(from:to:duration:options:completion:) ,它具有以下声明:

 class func transition(from fromView: UIView, to toView: UIView, duration: TimeInterval, options: UIViewAnimationOptions = [], completion: ((Bool) -> Void)? = nil) 

使用给定的参数在指定的视图之间创build一个转换animation。


在你可以传递的许多UIViewAnimationOptions参数中transition(from:to:duration:options:completion:)transitionCrossDissolve

transitionCrossDissolve具有以下声明:

 static var transitionCrossDissolve: UIViewAnimationOptions { get } 

从一个视图到另一个视图的转换。


以下Swift 3 Playground代码显示了如何通过使用transition(from:to:duration:options:completion:)transitionCrossDissolve在具有交叉渐变转换的两个UIViews之间切换:

 import UIKit import PlaygroundSupport class ViewController: UIViewController { let firstView: UIView = { let view = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100)) view.backgroundColor = .red return view }() let secondView: UIView = { let view = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100)) view.backgroundColor = .blue return view }() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white view.addSubview(firstView) let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggle(_:))) view.addGestureRecognizer(tapGesture) } func toggle(_ sender: UITapGestureRecognizer) { let presentedView = view.subviews.first === firstView ? firstView : secondView let presentingView = view.subviews.first !== firstView ? firstView : secondView UIView.transition(from: presentedView, to: presentingView, duration: 1, options: [.transitionCrossDissolve], completion: nil) } } let controller = ViewController() PlaygroundPage.current.liveView = controller 
 [UIView beginAnimations: @"cross dissolve" context: NULL]; [UIView setAnimationDuration: 1.0f]; self.firstView.alpha = 0.0f; self.secondView.alpha = 1.0f; [UIView commitAnimations];