【发布时间】:2016-03-12 06:56:45
【问题描述】:
这是我的代码: 导入 UIKit
class circularLoaderView: UIView {
let circlePathLayer = CAShapeLayer()
let circleRadius: CGFloat = 20.0
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
configure()
}
func configure() {
circlePathLayer.frame = bounds
circlePathLayer.lineWidth = 2
circlePathLayer.fillColor = UIColor.clearColor().CGColor
circlePathLayer.strokeColor = UIColor.redColor().CGColor
layer.addSublayer(circlePathLayer)
backgroundColor = UIColor.whiteColor()
}
func circleFrame() -> CGRect {
var circleFrame = CGRect(x: 0, y: 0, width: 2*circleRadius, height: 2*circleRadius)
circleFrame.origin.x = 20
circleFrame.origin.y = 20
return circleFrame
}
func circlePath() -> UIBezierPath {
let startAngle = CGFloat(-M_PI_2)
let endAngle = startAngle + CGFloat(M_PI * 2)
return UIBezierPath(arcCenter: CGPoint(x: circleRadius, y: circleRadius), radius: circleRadius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
}
override func layoutSubviews() {
super.layoutSubviews()
circlePathLayer.frame = bounds
circlePathLayer.path = circlePath().CGPath
circlePathLayer.strokeEnd = 0.25
circlePathLayer.addAnimation(rotationAnimation, forKey: "transform.rotation")
}
let rotationAnimation: CAAnimation = {
let animation = CABasicAnimation(keyPath: "transform.rotation")
animation.fromValue = 0
animation.toValue = M_PI * 2
animation.duration = 4
animation.repeatCount = MAXFLOAT
return animation
}()
}
这是我的代码,我创建了一个 CAShapeLayer,它将旋转并因此产生加载器效果,但是当我添加 rotationAnimation 的动画时,它会围绕一个中心点旋转整个 CAShapeLayer,而不是 CAShapeLayer 本身。为什么会这样?我该如何解决?谢谢
【问题讨论】:
标签: ios swift cashapelayer