【问题标题】:Slow down Fidget spinner gradually by decreasing its Speed通过降低速度逐渐减慢指尖陀螺
【发布时间】:2017-10-25 06:18:14
【问题描述】:

我正在开发指尖陀螺。

我跟踪手指速度,它以用户给定手指速度的初始速度旋转。我正在使用触摸。

现在我希望速度逐渐降低以逐渐减慢微调器从初始速度到零。

我正在使用计时器来降低速度,但它会产生混蛋效果。请帮助我。我正在使用 cabasicanimation 来旋转微调器。这是我的代码。

`class ViewController: UIViewController,CAAnimationDelegate {

@IBOutlet weak var spinnerImageView: UIImageView!
@IBOutlet weak var pointsLabel: UILabel!
var timer = Timer()
var touchBegan:CGPoint?
var touchEnded:CGPoint?
var beginTime = TimeInterval()
var endTime = TimeInterval()
var timeTaken = TimeInterval()
var speed:Float = 0
var totalDistance:CGFloat?


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.



}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    spinnerImageView.layer.removeAllAnimations()

    if let touch = touches.first {
        totalDistance = 0
        touchBegan = touch.location(in: spinnerImageView)
        beginTime = (event?.timestamp)!
       // print(beginTime)
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {



}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    if let touch = touches.first {

        touchEnded = touch.location(in: spinnerImageView)
        endTime = (event?.timestamp)!
       // print(endTime)
        let dx:CGFloat = (touchBegan?.x)! - (touchEnded?.x)!
        let dy:CGFloat = (touchBegan?.y)! - (touchEnded?.y)!
        totalDistance = sqrt(dx*dx + dy*dy)
        timeTaken = endTime - beginTime
        print("Total Distance In Km's : \(totalDistance!)")
        print("Time Difference Between touches : \(Float(timeTaken))")

        speed = Float(totalDistance!/1000)/Float(timeTaken)
        print("Speed in Meters : \(speed)")

        spinnerImageView.startSpin(speed: speed, duration: 1.0, completionDelegate: self)

        timer = Timer.scheduledTimer(timeInterval: 0, target: self, selector: #selector(self.startTimer), userInfo: nil, repeats: true)

}
}

@objc func startTimer() {

    spinnerImageView.layer.removeAnimation(forKey: "rotation")
    speed = speed - Float(timer.timeInterval)
    spinnerImageView.startSpin(speed: speed, duration: 1.0, completionDelegate: self)

}

让 kanimationKey = "旋转"

扩展 UIView{

func startSpin(speed: Float ,duration:CFTimeInterval, completionDelegate: CAAnimationDelegate? = nil)  {


        let basicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        basicAnimation.fromValue = 0
        basicAnimation.toValue = CGFloat(Double.pi * 2)
        basicAnimation.duration = duration
        basicAnimation.repeatCount = Float(CGFloat.infinity)
        basicAnimation.speed = speed
        basicAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
       // basicAnimation.isRemovedOnCompletion = false
      //  basicAnimation.fillMode = kCAFillModeForwards
       // basicAnimation.autoreverses = false
    if let delegate: CAAnimationDelegate = completionDelegate {
        basicAnimation.delegate = delegate
    }
       // self.layer.removeAnimation(forKey: kanimationKey)
        self.layer.add(basicAnimation, forKey: kanimationKey)

}

func stopSpin() {

    if self.layer.animation(forKey: kanimationKey) != nil {

        self.layer.removeAnimation(forKey: kanimationKey)
    }


}`

【问题讨论】:

标签: ios swift cabasicanimation


【解决方案1】:

您可以使用带有 SpringAnimation + 高阻尼的 Facebook Pop (https://github.com/facebook/pop)。 Facebook pop 允许您为自定义属性设置动画,就像他们的自述文件中的那样:

if let prop = POPAnimatableProperty.property(withName: "com.foo.radio.volume", initializer: { prop in
    guard let prop = prop else {
        return
    }
    // read value
    prop.readBlock = { obj, values in
        guard let obj = obj as? Volumeable, let values = values else {
            return
        }

        values[0] = obj.volume
    }
    // write value
    prop.writeBlock = { obj, values in
        guard var obj = obj as? Volumeable, let values = values else {
            return
        }

        obj.volume = values[0]
    }
    // dynamics threshold
    prop.threshold = 0.01
}) as? POPAnimatableProperty {
    anim.property = prop
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多