gpt4 book ai didi

ios - 在 Swift 中添加 subview 时如何停止动画重置?

转载 作者:行者123 更新时间:2023-11-28 12:46:35 24 4
gpt4 key购买 nike

我试图让元素在云的位置生成。到目前为止,我已经按照我想要的方式重复制作云动画,但是当我生成一个项目时,云动画会从头开始重置。

添加 subview 时,我有哪些选择?

Here is a gif from the project

override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
gemCloudImage.center.x = 100
makeCloudFly()
}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
}

func makeCloudFly() {
UIView.animateWithDuration(6, delay: 1, options: [.Autoreverse, .Repeat], animations: {
self.gemCloudImage.center.x = self.view.frame.width - 100
}, completion: nil)
}

@IBAction func buttonTapped(sender: AnyObject) {

let positionX: CGFloat = gemCloudImage.layer.presentationLayer()!.frame.midX
let positionY: CGFloat = gemCloudImage.layer.presentationLayer()!.frame.midY
print(positionX)
print(positionY)

let chair = UIImageView()
chair.image = UIImage(named: "CHAIR1")
chair.frame = CGRectMake(positionX, positionY, 50, 50)
chair.frame = CGRect(x:positionX, y:positionY, width:50, height:50)
self.view.addSubview(chair)

}

最佳答案

viewDidLayoutSubviews中的代码移动到viewDidLoad中。那应该可以解决您的问题。

每次添加新 subview 时都会调用方法 viewDidLayoutSubviews

关于ios - 在 Swift 中添加 subview 时如何停止动画重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37884005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com