【发布时间】:2016-10-03 08:52:00
【问题描述】:
我有一个将矩形绘制到特定百分比的动画。
为此,我正在绘制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 的缩放比例。当我开始绘制路径是根据小尺寸的 superView 绘制时出现问题,然后当框架变大时,绘制动画的路径保持不变,这是预期的,但有没有一个合乎逻辑的不老套的解决方案?还是我必须在drawRect 中进行操作
对于 superview,动画正在改变 UIView 动画中的 UILayout 高度常量:
heightOfContainerConstraint.constant = 100 // or 400 when it is expanded
UIView.animateWithDuration(animated ? animationDuration : 0) {
self.view.layoutIfNeeded()
}
圆角创建代码:
import UIKit
class 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, remainingPercent: remainingPercent, partValue: partValue)
remainingPercent = addArc(path, x: topRight.x - cornerRadius, y: topRight.y + cornerRadius, radius: cornerRadius, startAngle: startAngleTopRight, endingAngle: endingAngleTopRight, remainingPercent: remainingPercent, currentPartPercent: partValue * 2)
// add right line
remainingPercent = addLine(path, x: bottomRight.x, y: topRight.y + cornerRadius + (wayToGoLine * getConstantForThis(remainingPercent, partValue: linePartValue)), remainingPercent: remainingPercent, currentPartPercent: linePartValue)
// add bottom right curve
let endingAngleBottomRight = endingAngleForThis(startAngleBottomRight, remainingPercent: remainingPercent, partValue: partValue)
remainingPercent = addArc(path, x: bottomRight.x - cornerRadius, y: bottomRight.y - cornerRadius, radius: cornerRadius, startAngle: startAngleBottomRight, endingAngle: endingAngleBottomRight, remainingPercent: remainingPercent, currentPartPercent: partValue * 2)
// add bottom line
remainingPercent = addLine(path, x: bottomRight.x - cornerRadius - (wayToGoLine * getConstantForThis(remainingPercent, partValue: linePartValue)), y: bottomLeft.y, remainingPercent: remainingPercent, currentPartPercent: linePartValue)
// add bottom left curve
let endingAngleBottomLeft = endingAngleForThis(startAngleBottomLeft, remainingPercent: remainingPercent, partValue: partValue)
remainingPercent = addArc(path, x: bottomLeft.x + cornerRadius, y: bottomLeft.y - cornerRadius, radius: cornerRadius, startAngle: startAngleBottomLeft, endingAngle: endingAngleBottomLeft, remainingPercent: remainingPercent, currentPartPercent: partValue * 2)
// add left line
remainingPercent = addLine(path, x: topLeft.x, y: bottomLeft.y - cornerRadius - (wayToGoLine * getConstantForThis(remainingPercent, partValue: linePartValue)), remainingPercent: remainingPercent, currentPartPercent: linePartValue)
// add top left curve
let endingAngleTopLeft = endingAngleForThis(startAngleTopLeft, remainingPercent: remainingPercent, partValue: partValue)
remainingPercent = addArc(path, x: topLeft.x + cornerRadius, y: topLeft.y + cornerRadius, radius: cornerRadius, startAngle: startAngleTopLeft, endingAngle: endingAngleTopLeft, remainingPercent: remainingPercent, currentPartPercent: partValue * 2)
// add top left half line
remainingPercent = addLine(path, x: topLeft.x + cornerRadius + (wayToGoLine/2 * getConstantForThis(remainingPercent, partValue: linePartValue/2)), y: topRight.y, remainingPercent: 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, remainingPercent: CGFloat, currentPartPercent: CGFloat) -> CGFloat {
if remainingPercent > 0 {
CGPathAddLineToPoint(path, nil, x, y)
return remainingPercent - currentPartPercent
}
return 0
}
static func addArc(path: CGMutablePath?, x: CGFloat, y: CGFloat, radius: CGFloat, startAngle: CGFloat, endingAngle: CGFloat, remainingPercent: CGFloat, currentPartPercent: CGFloat) -> CGFloat {
if remainingPercent > 0 {
CGPathAddArc(path, nil, x, y, radius, startAngle, endingAngle, false)
return remainingPercent - currentPartPercent
}
return 0
}
}
【问题讨论】:
-
@AlessandroOrnano 我已经添加了代码,但我认为没有必要返回一个 CGMutablePathRef 并且函数本身并不重要。
-
我还添加了屏幕gif,希望有人现在回复:D @AlessandroOrnano
-
好,这是一个明确的问题。我不知道我是否理解正确,但我试图给出答案。
标签: swift calayer cashapelayer cabasicanimation