Swift,为特定的 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)
    }
}

结果: