我正在开发一个 Swift 项目并在 UIButton 周围创建一个圆圈,使用 CAShapeLayer 并创建一个圆形 UIBezeir 路径。问题是如果我将此 CAShapLayer 作为子层添加到 UIbutton,它不起作用。然而,在 UiView 上添加这个子层会创建圆圈。
下面是我的代码。
let ovalPath = UIBezierPath(arcCenter: lockButton.center, radius:
CGFloat(lockButton.frame.size.width/2), startAngle: CGFloat(startAngle), endAngle: CGFloat(sentAngel), clockwise: true)
UIColor.grayColor().setFill()
ovalPath.fill()
let circleLayer = CAShapeLayer()
circleLayer.path = ovalPath.CGPath
view.layer.addSublayer(circleLayer)
circleLayer.strokeColor = UIColor.blueColor().CGColor
circleLayer.fillColor = UIColor.clearColor().CGColor
circleLayer.lineWidth = 10.0
let animation = CABasicAnimation(keyPath: "strokeEnd")
// Set the animation duration appropriately
animation.duration = 0.5
// Animate from 0 (no circle) to 1 (full circle)
animation.fromValue = 0
animation.toValue = 1
// Do a linear animation (i.e. the speed of the animation stays the same)
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
// Set the circleLayer's strokeEnd property to 1.0 now so that it's the
// right value when the animation ends.
circleLayer.strokeEnd = 3.0
// Do the actual animation
circleLayer.addAnimation(animation, forKey: "animateCircle")
如果我们可以将 CAShapeLayer 添加到 UIButton,谁能建议我? 谢谢
最佳答案
您的代码有一个非常明显的问题是您的circleLayer
没有框架。任何东西都出现了,这真是太神奇了。您需要设置 circleLayer
的框架,使其具有大小并相对于其超层正确定位。
关于swift - 将 CAShapeLayer 添加到 UIButton 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40594270/