加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

animation – 在drawRect()Swift中绘制bezier路径的动画

发布时间:2020-12-14 05:29:41 所属栏目:百科 来源:网络整理
导读:我有这个形状,我在drawRect()中绘制 var rectanglePath = UIBezierPath()override func drawRect(rect: CGRect) { rectanglePath = UIBezierPath(rect: self.bounds) rectanglePath.fillWithBlendMode(kCGBlendModeMultiply,alpha: 0.7) layer.shouldRasteri
我有这个形状,我在drawRect()中绘制
var rectanglePath = UIBezierPath()

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

当调用prepareForEditing函数时,我想为rectanglePath设置动画.我试过了

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

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

要为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)
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读