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

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的中心。

最佳答案

这是我用来使用户的个人资料图像变圆的代码:

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


imageView.clipsToBounds = true


在您的情况下,应使用circleView而不是imageView

关于swift - CAShapeLayer-UIVIew中的圆圈,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36047571/

10-16 14:08