通过降低速度逐渐减慢Fidget旋转器

时间:2017-10-25 06:18:14

标签: ios swift cabasicanimation

我正在开发Fidget Spinner。

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

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

我正在使用计时器降低速度,但它会使混蛋效果.plz,帮助我。我正在使用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)
    }


}`

1 个答案:

答案 0 :(得分:0)

您可以将Facebook Pop(https://github.com/facebook/pop)与SpringAnimation +高阻尼一起使用。 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
}