ios – Swift:UIBezierPath中风动画

前端之家收集整理的这篇文章主要介绍了ios – Swift:UIBezierPath中风动画前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在用 Swift制作一个简单的UIBezierPath动画.该路径包括创建带有彩色边框的圆角矩形.动画必须是彩色边框的绘图.为此,我创建了一个带有UIBezierPath(roundedRect :,cornerRadius:)的CAShapeLayer
let layer = CAShapeLayer()
var viewPrueba = UIView()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view,typically from a nib.

    viewPrueba = UIView(frame: CGRectMake(self.view.frame.width/2-100,self.view.frame.height/2 - 100,200,200))
    self.view.addSubview(viewPrueba)
    let path = UIBezierPath(roundedRect: CGRectMake(0,200),cornerRadius: 40.0)
    layer.path = path.CGPath
    layer.fillColor = UIColor.clearColor().CGColor
    layer.strokeColor = UIColor.blueColor().CGColor
    layer.strokeStart = 0.0
    layer.strokeEnd = 0.0
    layer.lineWidth = 4.0
    layer.lineJoin = kCALineJoinRound
    viewPrueba.layer.addSublayer(layer)
    let tapGR = UITapGestureRecognizer(target: self,action: #selector(ViewController.anim))
    self.view.addGestureRecognizer(tapGR)
}

func anim() {
    let anim1 = CABasicAnimation(keyPath: "strokeEnd")
    anim1.fromValue         = 0.0
    anim1.toValue           = 1.0
    anim1.duration          = 4.0
    anim1.repeatCount       = 0
    anim1.autoreverses      = false
    anim1.removedOnCompletion = false
    anim1.additive = true
    anim1.fillMode = kCAFillModeForwards
    self.layer.addAnimation(anim1,forKey: "strokeEnd")
}`

它工作得很好唯一的问题是动画从广场的左上角开始,而不是从顶部中心开始.我怎样才能做到这一点?

为了实现这一点,我发现唯一的事情是用一个圆圈而不是一个矩形,这不是我们想要的.

谢谢

解决方法

CoreAnimate动画与UIBezierPath绘制的顺序相同.
系统方法
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;

返回从左上方绘制的UIBezierPath,因此您的动画从左上角开始.
但是您可以创建自己的UIBezierPath绘制表格中心:

func centerStartBezierPath(frame:CGRect,cornerRadius:CGFloat) -> UIBezierPath {
    let path = UIBezierPath()
    path.moveToPoint(CGPointMake(frame.width/2.0,0))
    path.addLineToPoint(CGPointMake(frame.width-cornerRadius,0))
    path.addArcWithCenter(CGPointMake(frame.width-cornerRadius,cornerRadius),radius: cornerRadius,startAngle: CGFloat(-M_PI/2),endAngle: 0,clockwise: true)
    path.addLineToPoint(CGPointMake(frame.width,frame.height-cornerRadius))
    path.addArcWithCenter(CGPointMake(frame.width-cornerRadius,frame.height-cornerRadius),startAngle: 0,endAngle: CGFloat(M_PI/2),clockwise: true)
    path.addLineToPoint(CGPointMake(cornerRadius,frame.height))
    path.addArcWithCenter(CGPointMake(cornerRadius,startAngle: CGFloat(M_PI/2),endAngle: CGFloat(M_PI),clockwise: true)
    path.addLineToPoint(CGPointMake(0,cornerRadius))
    path.addArcWithCenter(CGPointMake(cornerRadius,startAngle: CGFloat(M_PI),endAngle: CGFloat(M_PI*3/2),clockwise: true)
    path.closePath()

    path.applyTransform(CGAffineTransformMakeTranslation(frame.origin.x,frame.origin.y))

    return path;
}

它的工作原理如下:您也可以更改代码,并从任何点开始.

原文链接:https://www.f2er.com/iOS/335683.html

猜你在找的iOS相关文章