只绘制一半圆圈

时间:2016-08-20 12:38:52

标签: swift

我想只绘制圆圈​​的左半部分或右半部分。

我可以毫无问题地从0到0.5(右侧)绘制圆圈。但是从0.5到1(左侧)绘制圆圈不起作用。

呼叫:

import UIKit
extension UIColor {
    /// UIColor(hexString: "#cc0000")
    internal convenience init?(hexString:String) {
        guard hexString.characters[hexString.startIndex] == Character("#") else {
            return nil
        }
        guard hexString.characters.count == "#000000".characters.count else {
            return nil
        }
        let digits = hexString.substringFromIndex(hexString.startIndex.advancedBy(1))
        guard Int(digits,radix:16) != nil else{
            return nil
        }
        let red = digits.substringToIndex(digits.startIndex.advancedBy(2))
        let green = digits.substringWithRange(Range<String.Index>(digits.startIndex.advancedBy(2)..<digits.startIndex.advancedBy(4)))
        let blue = digits.substringWithRange(Range<String.Index>(digits.startIndex.advancedBy(4)..<digits.startIndex.advancedBy(6)))
        let redf = CGFloat(Double(Int(red, radix:16)!) / 255.0)
        let greenf = CGFloat(Double(Int(green, radix:16)!) / 255.0)
        let bluef = CGFloat(Double(Int(blue, radix:16)!) / 255.0)
        self.init(red: redf, green: greenf, blue: bluef, alpha: CGFloat(1.0))
    }
}

class CircleView: UIView {

    var circleLayer: CAShapeLayer!
    var from : CGFloat = 0.0;


    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clearColor()

        // Use UIBezierPath as an easy way to create the CGPath for the layer.
        // The path should be the entire circle.
        let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)

        // Setup the CAShapeLayer with the path, colors, and line width
        circleLayer = CAShapeLayer()
        circleLayer.path = circlePath.CGPath
        circleLayer.fillColor = UIColor.clearColor().CGColor

        //I'm going to change this in the ViewController that uses this. Not the best way, I know but alas.
        circleLayer.strokeColor = UIColor.redColor().CGColor

        //You probably want to change this width
        circleLayer.lineWidth = 3.0;

        // Don't draw the circle initially
        circleLayer.strokeEnd = 0.0

        // Add the circleLayer to the view's layer's sublayers
        layer.addSublayer(circleLayer)

    }

    func setStrokeColor(color : CGColorRef) {
        circleLayer.strokeColor = color
    }


    // This is what you call if you want to draw a full circle.
    func animateCircle(duration: NSTimeInterval) {
        animateCircleTo(duration, fromValue: 0.0, toValue: 1.0)
    }

    // This is what you call to draw a partial circle.
    func animateCircleTo(duration: NSTimeInterval, fromValue: CGFloat, toValue: CGFloat){
        // We want to animate the strokeEnd property of the circleLayer
        let animation = CABasicAnimation(keyPath: "strokeEnd")

        // Set the animation duration appropriately
        animation.duration = duration

        // Animate from 0 (no circle) to 1 (full circle)
        animation.fromValue = 0
        animation.toValue = toValue

        // Do an easeout. Don't know how to do a spring instead
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)

        // Set the circleLayer's strokeEnd property to 1.0 now so that it's the
        // right value when the animation ends.
        circleLayer.strokeEnd = toValue

        // Do the actual animation
        circleLayer.addAnimation(animation, forKey: "animateCircle")
    }


    // required function
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

这是我的代码:

table

圆视图类

flexbox

3 个答案:

答案 0 :(得分:2)

您需要设置:

circleLayer.strokeStart = fromValue

在animateCircleTo(duration ...)函数中。

您设置了笔画的结尾,但不是开头。因此,所有圆形动画都从0.0开始,即使您打算在中风的后期开始它们。

像这样:

// This is what you call to draw a partial circle.
func animateCircleTo(duration: NSTimeInterval, fromValue: CGFloat, toValue: CGFloat){
    // We want to animate the strokeEnd property of the circleLayer
    let animation = CABasicAnimation(keyPath: "strokeEnd")

    // Set the animation duration appropriately
    animation.duration = duration

    // Animate from 0 (no circle) to 1 (full circle)
    animation.fromValue = 0
    animation.toValue = toValue

    // Do an easeout. Don't know how to do a spring instead
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)

    // Set the circleLayer's strokeEnd property to 1.0 now so that it's the
    // right value when the animation ends.
    circleLayer.strokeStart = fromValue
    circleLayer.strokeEnd = toValue

    // Do the actual animation
    circleLayer.addAnimation(animation, forKey: "animateCircle")
}

答案 1 :(得分:1)

最简单的方法是掩盖或剪掉你不想要的圆圈的一半。

class HalfCircleView : UIView {
    override func draw(_ rect: CGRect) {
        let p = UIBezierPath(rect: CGRect(x: 50, y: 0, width: 50, height: 100))
        p.addClip()
        let p2 = UIBezierPath(ovalIn: CGRect(x: 10, y: 10, width: 80, height: 80))
        p2.lineWidth = 2
        p2.stroke()
    }
}

enter image description here

当然,你放置剪辑的地方是圆圈的一半会出现。当你有半圆视图时,你可以旋转它等。

答案 2 :(得分:0)

在快速ui中。绘制顺时针圆弧,并显示进度:

enter image description here

这是实现:

    struct ArcShape: Shape {
    var width: CGFloat
    var height: CGFloat
    var progress: Double

    func path(in rect: CGRect) -> Path {

        let bezierPath = UIBezierPath()
        let endAngle = 360.0 * progress - 90.0
        bezierPath.addArc(withCenter: CGPoint(x: width / 2, y: height / 2),
                          radius: width / 3,
                          startAngle: CGFloat(-90 * Double.pi / 180),
                          endAngle:   CGFloat(endAngle * Double.pi / 180),
                          clockwise: true)

        return Path(bezierPath.cgPath)
    }
}

用法:

            ArcShape(width:  geometry.size.width, height: geometry.size.height, progress: 0.75)
            .stroke(lineWidth: 5)
            .fill(Color(R.color.colorBlue.name))
            .frame(width: geometry.size.width , height: geometry.size.height)