美文网首页
Swift小记:利用mask遮罩,剪出一个定位头像

Swift小记:利用mask遮罩,剪出一个定位头像

作者: 齐舞647 | 来源:发表于2019-07-06 20:48 被阅读0次

数学知识:

  • 圆点坐标:(x0,y0)
  • 半径:r
  • 角度:a0
  • 则圆上任一点为:(x1,y1)
    x1 = x0 + r * cos(ao * 3.14 /180 )
    y1 = y0 + r * sin(ao * 3.14 /180 )

效果如图:

代码如下:

class ViewController: UIViewController {
    
    var headView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let circleCenter: CGPoint = CGPoint(x: 75.0, y: 75.0)
        let circleRadius: CGFloat = 50.0
        let startAngle: Double = -75.0  // 开始角度
        let endAngle: Double = 255.0    // 结束角度
        let arrowDistance: CGFloat = 10.0 // 箭头衍生距离
        
        let PI = Double.pi
        let path:UIBezierPath = UIBezierPath.init()
        path.addArc(withCenter: circleCenter, radius: circleRadius, startAngle:  CGFloat(startAngle/180 * PI) , endAngle: CGFloat(endAngle/180 * PI), clockwise: true)
        path.move(to: CGPoint(x: circleCenter.x, y: circleCenter.y - circleRadius - arrowDistance))
        path.addLine(to: CGPoint(x: circleCenter.x + CGFloat(Double(circleRadius) * cos(startAngle/180 * PI)), y: circleCenter.y + CGFloat(Double(circleRadius) * sin(startAngle/180 * PI))))
        path.addLine(to: CGPoint(x: circleCenter.x + CGFloat(Double(circleRadius) * cos(endAngle/180 * PI)), y: circleCenter.y + CGFloat(Double(circleRadius) * sin(endAngle/180 * PI))))
        
        let shapeLayer: CAShapeLayer = CAShapeLayer.init()
        shapeLayer.path = path.cgPath
        
        headView = UIView.init(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
        headView.center = self.view.center
        headView.backgroundColor = .green
        headView.layer.mask = shapeLayer
        self.view.addSubview(headView)
    }
}

相关文章

网友评论

      本文标题:Swift小记:利用mask遮罩,剪出一个定位头像

      本文链接:https://www.haomeiwen.com/subject/zipmhctx.html