用视图推动动画

时间:2017-07-16 19:51:50

标签: ios swift uikit

我试图创建一个动画,其中viewOne填满屏幕并被屏幕推开并被viewTwo取代。我的尝试在下面部分有效 - viewTwo表现得像它应该的那样,但是viewOne应该在那里只是一个空白区域。

import UIKit

class ViewController: UIViewController {
let viewOne = UIView()
let viewTwo = UIView()

func animate () {
    view.sendSubview(toBack: viewOne)

    let screenBounds = UIScreen.main.bounds

    let finalToFrame = screenBounds
    let finalFromFrame = finalToFrame.offsetBy(dx: 0, dy: -screenBounds.size.height)

    viewOne.frame = finalToFrame.offsetBy(dx: 0, dy: +screenBounds.size.height)

    UIView.animate(withDuration: 0.3, animations: {
        self.viewOne.frame = finalToFrame
        self.viewTwo.frame = finalFromFrame
    }, completion: nil)
}
override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(viewOne)
    viewOne.widthAnchor.constraint(equalTo: view.widthAnchor)
    viewOne.heightAnchor.constraint(equalTo: view.heightAnchor)
    viewOne.backgroundColor = UIColor.green


    view.addSubview(viewTwo)
    viewTwo.widthAnchor.constraint(equalTo: view.widthAnchor)
    viewTwo.heightAnchor.constraint(equalTo: view.heightAnchor)
    viewTwo.backgroundColor = UIColor.blue
    view.sendSubview(toBack: viewTwo)

    let swipe = UISwipeGestureRecognizer(target: self, action: #selector(animate))
    swipe.direction = .down
    view.addGestureRecognizer(swipe)
}

}

0 个答案:

没有答案