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:)
là 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:)
và 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
Nguồn
2016-11-26 16:46:08
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
Tại sao? Khối là gì? – Dmitry
lập trình dựa trên khối. Xem câu trả lời của @Ashley Mills. – Gabriel