0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

もちろん俺らは抵抗するで?技術で🤜🤛Advent Calendar 2017

Day 4

UIViewに角丸枠線のUIBizierPath作る

Last updated at Posted at 2017-12-03

この一年間やたらとUIBezierPathを使ったけど、稀によくあるUIViewいっぱいいっぱいに角丸枠線つけたい時に使うExtension

UIBezierPath+roundedCorner

extension UIBezierPath {
    
    static func roundedCorners(rect: CGRect, topLeft: CGFloat, topRight: CGFloat, bottomRight: CGFloat, bottomLeft: CGFloat, lineWidth: CGFloat) -> UIBezierPath {
        
        func radian(_ angle: CGFloat) -> CGFloat {
            return angle * CGFloat(Double.pi) / 180
        }
        
        let path = UIBezierPath()
        
        path.lineWidth = lineWidth
        
        let halfWidth = lineWidth/2
        
        path.move(to: CGPoint(x: topLeft + halfWidth, y: halfWidth))
        
        path.addLine(to: CGPoint(x: rect.maxX - halfWidth - topRight, y: halfWidth))
        path.addArc(withCenter: CGPoint(x: rect.maxX - halfWidth - topRight, y: topRight + halfWidth),
                    radius: topRight,
                    startAngle: radian(-90),
                    endAngle: radian(0),
                    clockwise: true)
        
        path.addLine(to: CGPoint(x: rect.maxX - halfWidth, y: rect.maxY - halfWidth - bottomRight))
        path.addArc(withCenter: CGPoint(x: rect.maxX - halfWidth - bottomRight, y: rect.maxY - halfWidth - bottomRight),
                    radius: bottomRight,
                    startAngle: radian(0),
                    endAngle: radian(90),
                    clockwise: true)
        
        path.addLine(to: CGPoint(x: bottomLeft + halfWidth, y: rect.maxY - halfWidth))
        path.addArc(withCenter: CGPoint(x: bottomLeft + halfWidth, y: rect.maxY - halfWidth - bottomLeft),
                    radius: bottomLeft,
                    startAngle: radian(90),
                    endAngle: radian(180),
                    clockwise: true)
        
        path.addLine(to: CGPoint(x: halfWidth, y: topLeft + halfWidth))
        path.addArc(withCenter: CGPoint(x: topLeft + halfWidth, y: topLeft + halfWidth),
                    radius: topLeft,
                    startAngle: radian(180),
                    endAngle: radian(270),
                    clockwise: true)
        
        return path
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?