gpt4 book ai didi

ios - 如何让多个物体旋转?

转载 作者:行者123 更新时间:2023-11-28 06:53:32 25 4
gpt4 key购买 nike

我正在构建一个应用程序,我需要为三个按钮创建重力和旋转。我为我的其中一个按钮编写了一个无限旋转功能,但我不知道如何将它链接到其他两个按钮。

import UIKit

class ViewController: UIViewController {
var animator: UIDynamicAnimator?
var gravity: UIGravityBehavior?
var isRotating = false

@IBOutlet var Ball1: UIButton!
@IBOutlet var Ball2: UIButton!
@IBOutlet var Ball3: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

self.animator = UIDynamicAnimator(referenceView: self.view)

if !isRotating {
// create a spin animation
let spinAnimation = CABasicAnimation()
// starts from 0
spinAnimation.fromValue = 0
// goes to 360 ( 2 * π )
spinAnimation.toValue = M_PI*2
// define how long it will take to complete a 360
spinAnimation.duration = 1
// make it spin infinitely
spinAnimation.repeatCount = Float.infinity
// do not remove when completed
spinAnimation.removedOnCompletion = false
// specify the fill mode
spinAnimation.fillMode = kCAFillModeForwards
// and the animation acceleration
spinAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
// add the animation to the button layer
Ball1.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")

} else {
// remove the animation
Ball1.layer.removeAllAnimations()
}
// toggle its state
isRotating = !isRotating
}
}

最佳答案

您可以通过这种方式将动画层添加到您的 UIButtons

    Ball2.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")
Ball3.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")

并以这种方式删除它们

 Ball2.layer.removeAllAnimations()
Ball3.layer.removeAllAnimations()

关于ios - 如何让多个物体旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34294783/

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