如何在iPhone上更改视图时制作溶解动画?

12

如何在iPhone上进行视图切换时制作溶解动画?

溶解效果:一个视图在没有任何移动的情况下更改另一个视图。

非常感谢您的帮助!

4个回答

20
你要找的动画是:
[UIView animateWithDuration: 1.0
                 animations:^{
                     view1.alpha = 0.0;
                     view2.alpha = 1.0;
                 }];

一个更完整的解决方案,使用该动画可能是:

- (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];
                     }];
}

19

在 iOS 5 及更高版本中,您还可以使用 UIViewAnimationOptionTransitionCrossDissolve...

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

谢谢!这段代码不能部署到iOS 4.3吗? - Dmitry
1
是的,我已经在之前说过了...它将用于iOS5及更高版本 :) - Aravindhan

1

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

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

使用给定的参数,在指定的视图之间创建过渡动画。

在你可以传递给 transition(from:to:duration:options:completion:) 的许多 UIViewAnimationOptions 参数中,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

-1
[UIView beginAnimations: @"cross dissolve" context: NULL];
[UIView setAnimationDuration: 1.0f];
self.firstView.alpha = 0.0f;
self.secondView.alpha = 1.0f;
[UIView commitAnimations];

我认为最好不要使用这些方法。块更加优雅。正如苹果文档所说:在iOS 4.0及更高版本中,不建议使用此方法。 - Gabriel
基于块的编程。请参阅@Ashley Mills的答案。 - Gabriel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接