swift - 将 CAShapeLayer 添加到 UIButton 不起作用

标签 swift uibutton uibezierpath cashapelayer

我正在开发一个 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/

相关文章:

iphone - IB 与以编程方式创建 UIButtons

ios - 如何在 UIBezierPath 弧中获取半径的中心点?

ios - 将字符串保存到 NSUserDefaults?

ios - 通过辅助方法呈现 View Controller

swift - Swift 中具有多个可访问标签和图像的按钮

ios - 隐藏或隐藏 UIButton 顺序更改的最快方法

objective-c - 在 UIImageView 的图像上方绘制贝塞尔曲线

ios - 如何在 iOS 中获取表情符号路径

swift - 格式化字符串后出现意外结果

swift - 每次呈现新场景时,SpriteKit 内存都会增加