This is my code : import 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
}()
}
This is my code which I create a CAShapeLayer which will rotate and thus craete a loader effect, but when I add the animation of rotationAnimation
, it rotate the whole CAShapeLayer around a center point but not the CAShapeLayer itself. Why do this happen? How can I solve it? Thank you