Swift CAShapeLayer-UIVIew中的圆

Swift CAShapeLayer-UIVIew中的圆,swift,uiview,geometry,cashapelayer,Swift,Uiview,Geometry,Cashapelayer,我正在努力使用Xcode 7中的Swift 2.0在添加到主视图的UIView中绘制一个圆圈 UIView已在storyboard中创建,并在代码中声明为IBOutlet: @IBOutlet var circleView: UIView! 我使用在线教程中的代码在倒计时计时器上画了一个时间圆: func createCircle(){ let bounds = CGRect(x: 0, y: 0, width: circleView.frame.width / 2, height:

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

UIView已在storyboard中创建,并在代码中声明为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

的中心居中,下面是我用来使用户的个人资料图像圆的代码:

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


imageView.clipsToBounds = true

在您的情况下,您可能会使用
circleView
而不是
imageView

将其作为子层添加到错误的视图中。尝试
circleView.layer.addSublayer(rectShape)
这一行不太可能正确:
rectShape.position=circleView.center