当用户单击此自定义控件时,我正在尝试使用 UIBezierPath 为圆弧设置动画。贝塞尔曲线路径简单地向前捕捉,没有任何动画。
我覆盖 drawrect 的原因是因为我想在界面生成器中显示这个自定义控件。有没有一种好的方法可以在界面生成器中显示循环进度并使进度动画化?
这是我的代码:
import Foundation
import UIKit
import QuartzCore
@IBDesignable class CircleControl:UIControl {
@IBInspectable var progressTotal:CGFloat = 100
@IBInspectable var progressCounter:CGFloat = 20
{
willSet(newProgressCounter) {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = 0.5;
animation.toValue = newProgressCounter;
animation.delegate = self;
layer.addAnimation(animation, forKey: "strokeEnd")
}
didSet {
setNeedsDisplay()
}
}
override func drawRect(rect: CGRect) {
let center = CGPointMake(bounds.size.width / 2, bounds.size.height / 2)
let radius = bounds.size.width / 2;
let pi = CGFloat(M_PI)
let sliceAngle:CGFloat = (2 * pi) / progressTotal
let progressAngle:CGFloat = sliceAngle * progressCounter
let startAngle:CGFloat = CGFloat(-M_PI_2) + sliceAngle
let endAngle:CGFloat = startAngle + progressAngle;
let path = UIBezierPath()
path.addArcWithCenter(center, radius: radius - 5, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.lineWidth = 5
path.stroke()
}
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
println("touch")
self.progressCounter = 80
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
self.progressCounter = 20
}
}
最佳答案
您不能在 IB 内部制作动画。您也许可以在 Playground 上执行此操作,但 IB 仅用于布局 - 它不执行动画。您必须在模拟器或 playgrounds 中预览。
关于ios - 使用 Swift 动画循环进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24893216/