简体   繁体   English

快速,为特定的CGPoint设置动画

[英]Swift, animate to specific CGPoint

How could I animate programatically created object to go to a specific CGPoint. 我如何为以编程方式创建的对象设置动画以转到特定的CGPoint。 Not using CGAffineTransformMakeTranslation, as this is just on how to move it, not exactly where to move it. 不使用CGAffineTransformMakeTranslation,因为这只是如何移动它,而不是确切地将其移动到哪里。

I have tried: translatesAutoresizingMaskIntoConstraints = true 我试过了:translatesAutoresizingMaskIntoConstraints = true

But it makes no difference. 但这没什么区别。

I have tried this: 我已经试过了:

//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)

But when run in the simulator, absolutely nothing happened. 但是,在模拟器中运行时,绝对没有任何反应。

Your UI hasn't rendered yet when you are animating. 制作动画时,您的UI尚未呈现。 Put it in viewDidAppear. 将其放在viewDidAppear中。 This works for me: 这对我有用:

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)
    }
}

Result: 结果:

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM