【发布时间】:2016-11-14 17:14:45
【问题描述】:
我正在开发一个 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,谁能建议我? 谢谢
【问题讨论】:
-
您应该能够将 CAShapeLayer 添加到 any CALayer 的子层。让它表现得完全是另一个问题。你想做什么? “圆形按钮”不需要任何新图层。掩蔽到界限也是我没有看到的。所以请提供更多关于什么是有效的和无效的细节,也许我可以提供帮助。
-
我正在尝试在按钮周围添加循环进度
-
有什么具体原因吗?
-
@SundeepSaluja “它不起作用”对您意味着什么?
-
我的意思是,它没有在确切的位置添加圆圈。它使圆圈远离按钮。对不起,我应该在我的问题中提到这一点。
标签: swift uibutton uibezierpath cashapelayer