动画UIView时,UIView中CALayer上的动画被取消

时间:2018-08-28 17:01:24

标签: ios swift uiview calayer caanimation

我是Swift(和软件开发)的新手,所以请放轻松!

我为“ orb”(甜甜圈形状)制作了一个自定义类,该类将以无限脉冲方式进行动画处理:

let layerOne = CAShapeLayer()
let layerTwo = CAShapeLayer()
let layerThree = CAShapeLayer()


override init(frame: CGRect) {
    super.init(frame: frame)
    setupView()
}


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupView()
}

private func setupView() {
    backgroundColor = UIColor.clear
    layerOne.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: frame.width, height: frame.height), cornerRadius: frame.width/2).cgPath
    layerOne.lineWidth = 3
    layerOne.strokeColor = UIColor.white.cgColor
    layerOne.fillColor = UIColor.clear.cgColor
    layer.addSublayer(layerOne)

    layerTwo.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: frame.width, height: frame.height), cornerRadius: frame.width/2).cgPath
    layerTwo.fillColor = UIColor.clear.cgColor
    layerTwo.lineWidth = 9
    layerTwo.opacity = 0.5

    layerTwo.strokeColor = UIColor.white.cgColor
    layer.addSublayer(layerTwo)

    layerThree.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: frame.width, height: frame.height), cornerRadius: frame.width/2).cgPath
    layerThree.fillColor = UIColor.clear.cgColor
    layerThree.lineWidth = 15
    layerThree.opacity = 0.3
    layerThree.strokeColor = UIColor.white.cgColor
    layer.addSublayer(layerThree)

    animateOrb()
}

func animateOrb() {
    let layer3Animation = CABasicAnimation(keyPath: "opacity");
    layer3Animation.fromValue = 0
    layer3Animation.toValue = 0.3
    layer3Animation.duration = 0.25;
    layer3Animation.autoreverses = true
    layer3Animation.repeatCount = Float.infinity 
    layerThree.add(layer3Animation, forKey: "opacity");
}

我的视图控制器上有一个UIView,其自定义类设置为orbView类的自定义类。 UIView的起始Alpha为0,并在需要时使用此func将其淡入视图:

func fade(endAlpha: CGFloat, view: UIView, duration: Double){

    UIView.animate(withDuration: duration) {
        view.alpha = endAlpha
    }
}

但是,通过使用此功能对UIView进行动画处理,似乎会杀死视图中我图层的动画。如何阻止这种情况的发生?

理想情况下,无论UIView发生了什么变化(拉伸,淡入淡出,重新放置等),我都希望UIView中的动画层保持脉动

在此先感谢您的帮助!

0 个答案:

没有答案