2012-03-01 29 views

Trả lời

17

Các hình ảnh động bạn đang tìm kiếm là:

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

Một giải pháp hoàn thiện hơn, sử dụng hình ảnh động mà có thể là:

- (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]; 
        }]; 
} 
-1
[UIView beginAnimations: @"cross dissolve" context: NULL]; 
[UIView setAnimationDuration: 1.0f]; 
self.firstView.alpha = 0.0f; 
self.secondView.alpha = 1.0f; 
[UIView commitAnimations]; 
+0

Tôi nghĩ rằng nó tốt hơn không sử dụng phương pháp này. Khối thanh lịch hơn rất nhiều. Theo tài liệu của Apple cho biết: Sử dụng phương pháp này không được khuyến khích trong iOS 4.0 trở lên. – Gabriel

+0

Tại sao? Khối là gì? – Dmitry

+0

lập trình dựa trên khối. Xem câu trả lời của @Ashley Mills. – Gabriel

16

Bạn cũng có thể sử dụng UIViewAnimationOptionTransitionCrossDissolve trong ios5 và sau đó ...

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

Cảm ơn! Triển khai cho iOS 4.3 sẽ không có sẵn với mã này? – Dmitry

+1

ya Tôi đã nói trong này ... Nó sẽ được sử dụng cho ios5 và sau này :) – Aravindhan

1

UIView có một phương pháp gọi là transition(from:to:duration:options:completion:) có khai báo như sau:

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

Tạo một hình ảnh động chuyển đổi giữa các quan điểm quy định sử dụng các thông số nhất định.


Trong số các thông số UIViewAnimationOptions nhiều mà bạn có thể vượt qua để transition(from:to:duration:options:completion:)transitionCrossDissolve.

transitionCrossDissolve có khai báo như sau:

static var transitionCrossDissolve: UIViewAnimationOptions { get } 

Một sự chuyển đổi đó tan từ một điểm đến tiếp theo.


Swift mã sau 3 sân chơi cho thấy làm thế nào để chuyển đổi giữa hai UIViews bằng cách gạch chéo hòa tan chuyển tiếp bằng cách sử dụng transition(from:to:duration:options:completion:)transitionCrossDissolve:

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 
Các vấn đề liên quan