
为此,我正在绘制一个CAShapeLayer:
func drawTheLayer() -> CAShapeLayer { let linewidth: CGfloat = borderWIDth * bounds.size.wIDth / standardSizeWIDth let cornerRadiusResized: CGfloat = cornerRadiusRanged * bounds.size.wIDth / standardSizeWIDth let insetRect = CGRectInset(bounds,linewidth/2.0,linewidth/2.0) let apath = ShapeDraw.createRoundedCornerPath(insetRect,cornerRadius: cornerRadiusResized,percent: percentageRanged) let apathLayer = CAShapeLayer() apathLayer.frame = bounds apathLayer.bounds = insetRect apathLayer.path = apath apathLayer.strokecolor = Appcolor.Oncolor.CGcolor apathLayer.fillcolor = nil apathLayer.linewidth = linewidth apathLayer.lineJoin = kCAlineJoinRound apathLayer.lineCap = kCAlineCapRound apathLayer.geometryFlipped = true let fliptransform = CGAffinetransformMakeScale(1,-1) apathLayer.setAffinetransform(fliptransform) return apathLayer} 要为绘图设置动画:
func animatetoLevel() { if percentage <= 0 { return } pathLayer?.removeFromSuperlayer() pathLayer?.removeAllAnimations() animating = true let apathLayer = drawTheLayer() layer.addSublayer(apathLayer) let pathAnimation = CABasicAnimation(keyPath: "strokeEnd") pathAnimation.delegate = self pathAnimation.duration = 0.5 pathAnimation.fromValue = 0.0 pathAnimation.setValue("levelAnimation",forKey: "animationID") apathLayer.addAnimation(pathAnimation,forKey: nil) pathLayer = apathLayer} 与这个动画无关,还有另一个动画可以发生.缩放矩形的superVIEw.当我开始绘制路径时出现问题是根据小尺寸的超级视图绘制的,然后当帧变大时,绘图动画的路径保持相同,这是预期但是有一个逻辑上没有Hacky解决方案吗?或者我必须在drawRect中执行此 *** 作
对于supervIEw,动画在UIVIEw动画中更改UILayout高度常量:
heightOfContainerConstraint.constant = 100 // or 400 when it is expandedUIVIEw.animateWithDuration(animated ? animationDuration : 0) { self.vIEw.layoutIfNeeded()} 创建圆角的代码:
import UIKitclass ShapeDraw {static func createRoundedCornerPath(rect: CGRect,cornerRadius: CGfloat,percent: CGfloat) -> CGMutablePathref { let piNumber: CGfloat = CGfloat(M_PI) // get the 4 corners of the rect let topleft = CGPointMake(rect.origin.x,rect.origin.y) let topRight = CGPointMake(rect.origin.x + rect.size.wIDth,rect.origin.y) let bottomright = CGPointMake(rect.origin.x + rect.size.wIDth,rect.origin.y + rect.size.height) let bottomleft = CGPointMake(rect.origin.x,rect.origin.y + rect.size.height) // Set 4 corner arc starting angles let startAngletopRight: CGfloat = 3 * piNumber/2 let startAngleBottomright: CGfloat = 0 let startAngleBottomleft: CGfloat = piNumber / 2 let startAngletopleft: CGfloat = piNumber let slices = (CGRectGetWIDth(rect) / cornerRadius) * 4 let partValue: CGfloat = 100 / slices // %100 is total -> 1 pIEce is 100/16 percent let wayToGoline = CGRectGetWIDth(rect) - 2 * cornerRadius let linePartValue = partValue * (slices/4 - 2) var remainingPercent: CGfloat = percent let path = CGPathCreateMutable() // move to top left CGPathMovetoPoint(path,nil,topRight.x/2,topRight.y) // add top right half line remainingPercent = addline(path,x: topRight.x/2 + (wayToGoline/2 * getConstantForThis(remainingPercent,partValue: linePartValue/2)),y: topRight.y,remainingPercent: remainingPercent,currentPartPercent: linePartValue/2) // add top right curve let endingAngletopRight = endingAngleForThis(startAngletopRight,partValue: partValue) remainingPercent = addArc(path,x: topRight.x - cornerRadius,y: topRight.y + cornerRadius,radius: cornerRadius,startAngle: startAngletopRight,endingAngle: endingAngletopRight,currentPartPercent: partValue * 2) // add right line remainingPercent = addline(path,x: bottomright.x,y: topRight.y + cornerRadius + (wayToGoline * getConstantForThis(remainingPercent,partValue: linePartValue)),currentPartPercent: linePartValue) // add bottom right curve let endingAngleBottomright = endingAngleForThis(startAngleBottomright,partValue: partValue) remainingPercent = addArc(path,x: bottomright.x - cornerRadius,y: bottomright.y - cornerRadius,startAngle: startAngleBottomright,endingAngle: endingAngleBottomright,currentPartPercent: partValue * 2) // add bottom line remainingPercent = addline(path,x: bottomright.x - cornerRadius - (wayToGoline * getConstantForThis(remainingPercent,y: bottomleft.y,currentPartPercent: linePartValue) // add bottom left curve let endingAngleBottomleft = endingAngleForThis(startAngleBottomleft,x: bottomleft.x + cornerRadius,y: bottomleft.y - cornerRadius,startAngle: startAngleBottomleft,endingAngle: endingAngleBottomleft,currentPartPercent: partValue * 2) // add left line remainingPercent = addline(path,x: topleft.x,y: bottomleft.y - cornerRadius - (wayToGoline * getConstantForThis(remainingPercent,currentPartPercent: linePartValue) // add top left curve let endingAngletopleft = endingAngleForThis(startAngletopleft,x: topleft.x + cornerRadius,y: topleft.y + cornerRadius,startAngle: startAngletopleft,endingAngle: endingAngletopleft,currentPartPercent: partValue * 2) // add top left half line remainingPercent = addline(path,x: topleft.x + cornerRadius + (wayToGoline/2 * getConstantForThis(remainingPercent,currentPartPercent: linePartValue/2) return path}static func endingAngleForThis(startAngle: CGfloat,remainingPercent: CGfloat,partValue: CGfloat) -> CGfloat { return startAngle + (CGfloat(M_PI) * getConstantForThis(remainingPercent,partValue: partValue * 2) / 2)}static func getConstantForThis(percent: CGfloat,partValue: CGfloat) -> CGfloat { let percentConstant = percent - partValue > 0 ? 1 : percent / partValue return percentConstant}static func addline(path: CGMutablePath?,x: CGfloat,y: CGfloat,currentPartPercent: CGfloat) -> CGfloat { if remainingPercent > 0 { CGPathAddlinetoPoint(path,x,y) return remainingPercent - currentPartPercent } return 0}static func addArc(path: CGMutablePath?,radius: CGfloat,startAngle: CGfloat,endingAngle: CGfloat,currentPartPercent: CGfloat) -> CGfloat { if remainingPercent > 0 { CGPathAddArc(path,y,radius,startAngle,endingAngle,false) return remainingPercent - currentPartPercent } return 0}}解决方法 基本上,我们有两个动画: >按照矩形路径指定特定百分比
>缩放矩形超视图或更改边界的动画
目标是:这些动画必须在同一时间(一组)一起工作,而不是顺序.
下面的代码只是一个示例,不遵循确切的属性或自定义目标,我想解释在这种情况下我会做什么:
// follow the rectangle path let pathAnimation = CABasicAnimation(keyPath: "strokeEnd") let cornerRadiusResized: CGfloat = cornerRadiusRanged * bounds.size.wIDth / standardSizeWIDth let apath = ShapeDraw.createRoundedCornerPath(insetRect,percent: percentageRanged) pathAnimation.tovalue = apath // scaling of the rectangle supervIEw let newBounds = CGRectMake(self.vIEw.bounds.origin.x,self.vIEw.bounds.origin.y,self.vIEw.bounds.wIDth,self.vIEw.bounds.height) let boundsAnimation = CABasicAnimation(keyPath: "bounds") boundsAnimation.tovalue = NSValue(CGRect:newBounds) // The group var theGroup: CAAnimationGroup = CAAnimationGroup() theGroup.animations = [pathAnimation,boundsAnimation] theGroup.duration = 2.0 theGroup.repeatCount = 1 theGroup.fillMode = kCAFillModeForwards apathLayer.addAnimation(theGroup,forKey: "theGroup")
编辑:
如果您需要第三个动画,当您在评论中发言时,要更改UIbutton尺寸,您还可以添加:
var buttonAnimation:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")pulseAnimation.duration = 2.0pulseAnimation.tovalue = NSNumber(float: 0.5)pulseAnimation.timingFunction = camediatimingFunction(name: kcamediatimingFunctionEaseInEaSEOut)
并将其添加到数组中:
theGroup.animations = [pathAnimation,buttonAnimation,boundsAnimation]总结
以上是内存溢出为你收集整理的swift – 当superView的比例改变时,绘制路径的CABasicAnimation不会缩放全部内容,希望文章能够帮你解决swift – 当superView的比例改变时,绘制路径的CABasicAnimation不会缩放所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)