0
보기에서 로딩 원을 그립니다. 초기 뷰 컨트롤러가있는 경우 뷰를 직접 열면 원이 애니메이션으로 표시되지만 이전 뷰 (tableview)에서 열면 선택됩니다. 그것은 끌기와 함께 제공되지만 움직이지 않습니다. 나는 setupd()를 호출하려고했으나 viewdidload에서 didawake 함수를 호출했지만 여전히 기회는 없었다. 작동하지 않습니다.원형 애니메이션이 작동하지 않습니다.
class SpinningView: UIView {
let circleLayer = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
@IBInspectable var lineWidth: CGFloat = 4 {
didSet {
circleLayer.lineWidth = lineWidth
setNeedsLayout()
}
}
func setup() {
circleLayer.lineWidth = lineWidth
circleLayer.lineWidth = 10
circleLayer.fillColor = nil
circleLayer.strokeColor = UIColor(red: 0.8078, green: 0.2549, blue: 0.2392, alpha: 1.0).cgColor
layer.addSublayer(circleLayer)
tintColorDidChange()
updateAnimation()
}
func updateAnimation() {
if animating {
circleLayer.add(strokeEndAnimation, forKey: "strokeEnd")
circleLayer.add(strokeStartAnimation, forKey: "strokeStart")
}
else {
circleLayer.removeAnimation(forKey: "strokeEnd")
circleLayer.removeAnimation(forKey: "strokeStart")
}
}
override func layoutSubviews() {
super.layoutSubviews()
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let radius = min(bounds.width, bounds.height)/2 - circleLayer.lineWidth/2
let startAngle = CGFloat(-M_PI_2)
let endAngle = startAngle + CGFloat(M_PI * 2)
let path = UIBezierPath(arcCenter: CGPoint.zero, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
circleLayer.position = center
circleLayer.path = path.cgPath
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setup()
}
override func tintColorDidChange() {
super.tintColorDidChange()
circleLayer.strokeColor = tintColor.cgColor
}
@IBInspectable var animating: Bool = true {
didSet {
updateAnimation()
}
}
let strokeEndAnimation: CAAnimation = {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.toValue = 1
animation.duration = 2
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
let group = CAAnimationGroup()
group.duration = 2.5
group.repeatCount = MAXFLOAT
group.animations = [animation]
return group
}()
let strokeStartAnimation: CAAnimation = {
let animation = CABasicAnimation(keyPath: "strokeStart")
animation.beginTime = 0.5
animation.fromValue = 0
animation.toValue = 1
animation.duration = 2
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
let group = CAAnimationGroup()
group.duration = 2.5
group.repeatCount = MAXFLOAT
group.animations = [animation]
return group
}()
}