簡體   English   中英

快速,為特定的CGPoint設置動畫

[英]Swift, animate to specific CGPoint

我如何為以編程方式創建的對象設置動畫以轉到特定的CGPoint。 不使用CGAffineTransformMakeTranslation,因為這只是如何移動它,而不是確切地將其移動到哪里。

我試過了:translatesAutoresizingMaskIntoConstraints = true

但這沒什么區別。

我已經試過了:

//Define screen sizes
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height

// Sizes of centre cards
let centreCardWidth = screenWidth/5
let centreCardHeight = (screenWidth/5) / 0.625
let centralizeViewHorizontalCard = (screenWidth/2) - ((screenWidth/5) / 2)
let centralizeViewVerticleCard = screenHeight / 2

UIView.animateWithDuration(0.25, delay: 0.5, options: [], animations: {
    centreCard3.frame = CGRectMake(centralizeViewHorizontalCard, centralizeViewVerticleCard, centreCard3.frame.size.width, centreCard3.frame.size.height)
    }, completion: nil)

但是,在模擬器中運行時,絕對沒有任何反應。

制作動畫時,您的UI尚未呈現。 將其放在viewDidAppear中。 這對我有用:

class ViewController: UIViewController {

    @IBOutlet weak var centreCard3: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(animated: Bool) {
        let screenSize: CGRect = UIScreen.mainScreen().bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height

        // Sizes of centre cards

        let centralizeViewHorizontalCard = (screenWidth/2) - ((screenWidth/5) / 2)
        let centralizeViewVerticleCard = screenHeight / 2

        UIView.animateWithDuration(0.5, delay: 0.5, options: [], animations: {
            self.centreCard3.frame = CGRectMake(centralizeViewHorizontalCard, centralizeViewVerticleCard, self.centreCard3.frame.size.width, self.centreCard3.frame.size.height)
            }, completion: nil)
    }
}

結果:

結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM