How to round corners of UIImage with Hexagon mask

前端 未结 4 1520
轻奢々
轻奢々 2020-11-28 10:21

I\'m trying to add a hexagon mask to a UIImage which I has been successful in. However I am unable to round the sides of the hexagon mask. I thought adding cell.profilePic.l

4条回答
  •  心在旅途
    2020-11-28 10:40

    Here's a conversion of the roundedPolygon method for Swift.

    func roundedPolygonPathWithRect(square: CGRect, lineWidth: Float, sides: Int, cornerRadius: Float) -> UIBezierPath {
        var path = UIBezierPath()
    
        let theta = Float(2.0 * M_PI) / Float(sides)
        let offset = cornerRadius * tanf(theta / 2.0)
        let squareWidth = Float(min(square.size.width, square.size.height))
    
        var length = squareWidth - lineWidth
    
        if sides % 4 != 0 {
            length = length * cosf(theta / 2.0) + offset / 2.0
        }
    
        var sideLength = length * tanf(theta / 2.0)
    
        var point = CGPointMake(CGFloat((squareWidth / 2.0) + (sideLength / 2.0) - offset), CGFloat(squareWidth - (squareWidth - length) / 2.0))
        var angle = Float(M_PI)
        path.moveToPoint(point)
    
        for var side = 0; side < sides; side++ {
    
            let x = Float(point.x) + (sideLength - offset * 2.0) * cosf(angle)
            let y = Float(point.y) + (sideLength - offset * 2.0) * sinf(angle)
    
            point = CGPointMake(CGFloat(x), CGFloat(y))
            path.addLineToPoint(point)
    
            let centerX = Float(point.x) + cornerRadius * cosf(angle + Float(M_PI_2))
            let centerY = Float(point.y) + cornerRadius * sinf(angle + Float(M_PI_2))
    
            var center = CGPointMake(CGFloat(centerX), CGFloat(centerY))
    
            let startAngle = CGFloat(angle) - CGFloat(M_PI_2)
            let endAngle = CGFloat(angle) + CGFloat(theta) - CGFloat(M_PI_2)
    
            path.addArcWithCenter(center, radius: CGFloat(cornerRadius), startAngle: startAngle, endAngle: endAngle, clockwise: true)
    
            point = path.currentPoint
            angle += theta
        }
    
        path.closePath()
    
        return path
    }
    

提交回复
热议问题