简体   繁体   中英

Creating oval in Swift with a Progress bar around it

I am trying to design something like this image where there is an oval, and then a progress bar around it. I've been able to draw a wacky oval with the border, but I am completely lost on the progress bar part of it, any help would be appreciated. Here is my code for the oval and what the output is

First image is what the code outputs, second image is the goal

 func setupOvalView(){
                
        let path = UIBezierPath(ovalIn: CGRect(x: self.ovalView.frame.origin.x, y: self.ovalView.frame.origin.y, width: self.ovalView.frame.width, height: self.ovalView.frame.height))
        
        
        let rect = CGRect(x: self.ovalView.frame.origin.x + 20, y: self.ovalView.frame.origin.y + 20, width: self.ovalView.frame.width - 40, height: self.ovalView.frame.height - 40)
        
        let circlePath = UIBezierPath(ovalIn: rect)
        path.append(circlePath)
        path.usesEvenOddFillRule = true
        
        
        
        let fillLayer = CAShapeLayer()
        fillLayer.path = path.cgPath
        
        fillLayer.fillRule = .evenOdd
        fillLayer.fillColor = UIColor.lightGray.cgColor
        fillLayer.opacity = 0.5
        view.layer.addSublayer(fillLayer)
    }

在此处输入图片说明

在此处输入图片说明

This is how you can draw a part of an oval similar to the one from the picture you attached:

    let path = UIBezierPath(ovalIn: CGRect(x: 10, y: 10, width: 100, height: 200))
    let layer = CAShapeLayer()
    layer.path = path.cgPath
    layer.fillColor = UIColor.clear.cgColor
    layer.strokeColor = UIColor.red.cgColor
    layer.strokeStart = 0.5
    layer.strokeEnd = 0.8
    layer.lineWidth = 10
    layer.lineCap = .round
    view.layer.addSublayer(layer)

strokeStart and strokeEnd properties are used to set the start and end points of the progress line

I got this pretty easily:

在此处输入图片说明

That seems to be much like what you're trying to draw. It's just three shape layers. You've already got one, so you'd add two more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM