我正在寫一個計時器,類似於水平版本的時鐘應用程序中可用的計時器,只是它開始已滿並且向下運行爲空。因爲我使用drawRect方法進行動畫製作,所以我實現了計時器,它適用於較大的時間,但它無法快速進行動畫快速動畫處理(我的動畫時間最快爲400ms)。爲了說明這一點,這是我的計時器代碼:iOS動畫:CADisplayLink vs CAShapeLayer
class SSTimerView: UIView {
var timer: NSTimer?
var startAngle: CGFloat = (CGFloat) (M_PI * 1.5)
var endAngle: CGFloat = (CGFloat) (M_PI * 1.5) + (CGFloat) (M_PI * 2)
var percent: CGFloat? = 100
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
var bezierPath = UIBezierPath()
//Create our arc with the correct angles
bezierPath.addArcWithCenter(CGPointMake(rect.size.width/2, rect.size.height/2),
radius: 130,
startAngle: startAngle,
endAngle: (endAngle - startAngle) * (percent!/100.0) + startAngle,
clockwise: true
)
//Set the display for the path and stroke it.
bezierPath.lineWidth = 20
Slide.sharedInstance.currentInstruction!.colorFamily.lighterColor.setStroke()
bezierPath.stroke()
}
}
我一直在閱讀CADisplayLink了,我認爲這可能是一個可行的選擇,但我也聽說CADisplayLink並不適用於所有情況。這是他們中的一員嗎?如果CADisplayLink在這裏不合適,我應該使用哪個CA類?
我很喜歡你的建議。我最初使用它,但我對隱式動畫非常不滿。所以,我修改了你的解決方案,包括CADisplayLink,其中我正在計算和動畫計算每個幀的計時器應該具有的填充百分比。這對我來說工作得非常好,因爲我可以將控制邏輯添加到定時器(「如果定時器用完並滿足這些條件,則......」),暫停並重新啓動定時器,並且它更有效地運行CONSIDERABLY(< NSTimer的1/3 CPU使用率)。 –