在drawRect()中绘制的Bezier路径如何进行动画处理(Swift)

22

我在drawRect()中绘制了一个形状。

var rectanglePath = UIBezierPath()

override func drawRect(rect: CGRect) {
    rectanglePath = UIBezierPath(rect: self.bounds)
    rectanglePath.fillWithBlendMode(kCGBlendModeMultiply, alpha: 0.7)
    layer.shouldRasterize = true
}

当调用prepareForEditing函数时,我想要对矩形路径进行动画。我尝试过。

  func prepareForEditing(editing:Bool){
        UIView.animateWithDuration(0.5,
            animations: {
              self.rectanglePath = makeNewShape()
            }
       )
  }

什么都没有发生。您能告诉我我的代码有什么问题吗?

2个回答

21

要让CGPath动起来,您不能使用UIView.animation方法。我创建了一个自定义的UIView子类来展示如何动画化CGPaths形状,请参考注释并按照您的要求进行修改:

class MyView: UIView {

let shapeLayer = CAShapeLayer()
let maskLayer = CAShapeLayer()
var rectanglePath = UIBezierPath()

override func didMoveToSuperview() {
    super.didMoveToSuperview()

    backgroundColor = UIColor.clear

    // initial shape of the view
    rectanglePath = UIBezierPath(rect: bounds)

    // Create initial shape of the view
    shapeLayer.path = rectanglePath.cgPath
    shapeLayer.strokeColor = UIColor.black.cgColor
    shapeLayer.fillColor = UIColor.clear.cgColor
    layer.addSublayer(shapeLayer)

    //mask layer
    maskLayer.path = shapeLayer.path
    maskLayer.position =  shapeLayer.position
    layer.mask = maskLayer
}

func prepareForEditing(editing:Bool){

    let animation = CABasicAnimation(keyPath: "path")
    animation.duration = 2

    // Your new shape here
    animation.toValue = UIBezierPath(ovalIn: bounds).cgPath
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)

    // The next two line preserves the final shape of animation,
    // if you remove it the shape will return to the original shape after the animation finished
    animation.fillMode = kCAFillModeForwards
    animation.isRemovedOnCompletion = false

    shapeLayer.add(animation, forKey: nil)
    maskLayer.add(animation, forKey: nil)
  }
}

顺便说一下,fillWithBlend 模式能在 ShapeLayer 上使用吗?我尝试了这种方式,但混合并没有起作用。 - Dănuț Mihai Florian
我相信如果你将你的drawRect函数添加到MyView类中(就像你现在有的那三行代码一样),一切都应该是你想要的。 - Greg
你为什么在 didMoveToSuperview() 中绘制形状而不是 drawRect() - Dănuț Mihai Florian
2
如果您查看苹果的UIView文档,除非必须这样做,否则应避免覆盖此方法。这是因为它会影响性能。 - Greg

8

使用UIBezierPath在UIButton上绘制带有加载器动画的边框(Swift 5)

func animation(){
    CATransaction.begin()

    let layer : CAShapeLayer = CAShapeLayer()
    layer.strokeColor = UIColor.purple.cgColor
    layer.lineWidth = 3.0
    layer.fillColor = UIColor.clear.cgColor

    let path : UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.btn.frame.size.width + 2, height: self.btn.frame.size.height + 2), byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 5.0, height: 0.0))
    layer.path = path.cgPath

    let animation : CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = 0.0
    animation.toValue = 1.0

    animation.duration = 7.0

    CATransaction.setCompletionBlock{ [weak self] in
        print("Animation completed")
    }

    layer.add(animation, forKey: "myStroke")
    CATransaction.commit()
    self.btn.layer.addSublayer(layer)
}

注意:动画输出


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接