【发布时间】: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)
}
}`
【问题讨论】:
-
请阅读stackoverflow.com/help/how-to-ask,并在您的问题中包含您已经尝试/测试过的任何正在进行的代码
标签: ios swift cabasicanimation