CAShapeLayer - UIVIew内的圈子

时间:2016-03-16 21:45:50

标签: swift uiview geometry cashapelayer

我正努力在UIView中添加一个圆圈,添加到我的主视图中,在Xcode 7中使用Swift 2.0。

UIView已在故事板中创建,并在代码中声明为IBOutlet:

@IBOutlet var circleView: UIView!

我已经使用在线教程中的代码及时绘制了一个圆圈到倒数计时器:

func createCircle(){

    let bounds = CGRect(x: 0, y: 0, width: circleView.frame.width / 2, height: circleView.frame.width / 2)

    // Create CAShapeLayer
    let rectShape = CAShapeLayer()
    rectShape.bounds = bounds
    rectShape.position = circleView.center
    rectShape.cornerRadius = bounds.width / 2
    view.layer.addSublayer(rectShape)

    rectShape.path = UIBezierPath(ovalInRect: rectShape.bounds).CGPath
    rectShape.lineWidth = (bounds.width) / 10 // chnage this to change line width
    rectShape.strokeColor = UIColor.whiteColor().CGColor
    rectShape.fillColor = UIColor.clearColor().CGColor

    rectShape.strokeStart = 0
    rectShape.strokeEnd = 0

    let start = CABasicAnimation(keyPath: "strokeStart")
    start.toValue = 0
    let end = CABasicAnimation(keyPath: "strokeEnd")
    end.toValue = 1

    let group = CAAnimationGroup()
    group.animations = [start, end]
    group.duration = timerDuration
    group.autoreverses = false // use true to bounce it back
    group.repeatCount = 1 // can use the keyword HUGE to repeat forver
    rectShape.addAnimation(group, forKey: nil)

}

我一直在玩这个位置,但它似乎永远不会将圆圈置于circleView的中心。

1 个答案:

答案 0 :(得分:0)

以下是我用来制作用户个人资料图片的代码:

imageView.layer.cornerRadius = imageView.frame.size.width / 2


imageView.clipsToBounds = true

在您的情况下,您可以使用circleView代替imageView