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;}
它的工作原理如下:您也可以更改代码,并从任何点开始.
总结以上是内存溢出为你收集整理的ios – Swift:UIBezierPath中风动画全部内容,希望文章能够帮你解决ios – Swift:UIBezierPath中风动画所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)