【问题标题】:How to make dissolve animation on changing views on iphone?如何在 iPhone 上更改视图时制作溶解动画?
【发布时间】:2012-03-01 10:51:44
【问题描述】:

如何在iphone中更改视图时制作溶解动画?

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

非常感谢您的帮助!

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch animation


    【解决方案1】:

    您要查找的动画是:

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

    【讨论】:

      【解决方案2】:

      你也可以在ios5及以后使用UIViewAnimationOptionTransitionCrossDissolve...

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

      【讨论】:

      • 谢谢!无法使用此代码部署到 iOS 4.3?
      • 是的,我已经告诉过...它将用于ios5及更高版本:)
      【解决方案3】:

      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
      

      【讨论】:

        【解决方案4】:
        [UIView beginAnimations: @"cross dissolve" context: NULL];
        [UIView setAnimationDuration: 1.0f];
        self.firstView.alpha = 0.0f;
        self.secondView.alpha = 1.0f;
        [UIView commitAnimations];
        

        【讨论】:

        • 我认为最好不要使用这种方法。块要优雅得多。正如 Apple 文档所说:在 iOS 4.0 及更高版本中不鼓励使用此方法。
        • 基于块的编程。请参阅@Ashley Mills 的答案。
        猜你喜欢
        • 2020-02-21
        • 1970-01-01
        • 2010-10-04
        • 1970-01-01
        • 1970-01-01
        • 2011-07-11
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        相关资源
        最近更新 更多