我想创建一个圆圈消失的动画,我做了以下代码(基于我在互联网上找到的其他代码)。
class CircleView: UIView {
private var circleLayer: CAShapeLayer?
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
func commonInit() {
self.tintColor = UIColor.lightGrayColor()
self.backgroundColor = UIColor.clearColor()
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
self.drawCircle()
self.addCircleAnimation()
}
func drawCircle() {
let size:CGFloat = CGFloat(100)
self.circleLayer?.removeFromSuperlayer()
self.circleLayer = CAShapeLayer()
self.circleLayer?.frame = self.bounds
let radius = size / 2;
let path = UIBezierPath(arcCenter: CGPointMake(size / 2, size / 2), radius: radius, startAngle: -CGFloat(M_PI) / 4, endAngle: 2 * CGFloat(M_PI) - CGFloat(M_PI) / 4, clockwise: true)
self.circleLayer?.path = path.CGPath
self.circleLayer?.fillColor = UIColor.clearColor().CGColor
self.circleLayer?.strokeColor = self.tintColor.CGColor
self.circleLayer?.lineWidth = CGFloat(2.0)
self.circleLayer?.rasterizationScale = 2.0 * UIScreen.mainScreen().scale
self.circleLayer?.shouldRasterize = true
self.layer.addSublayer(self.circleLayer!)
}
func addCircleAnimation() {
let animation = CABasicAnimation(keyPath: "strokeStart")
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = CFTimeInterval(floatLiteral: 1)
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeBackwards
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
animation.beginTime = CACurrentMediaTime()
animation.delegate = self
self.circleLayer?.addAnimation(animation, forKey:"strokeStart")
}
}
然而,当动画结束时,它会离开原始圆圈,即使我用animationDidStop
检测到它仍然有闪烁效果。这段代码出了什么问题?
答案 0 :(得分:0)
删除圆圈图层的代码:
localArray.count