如何查找图像视图的半径?

时间:2016-10-13 17:53:33

标签: ios swift uiimageview uibezierpath cashapelayer

我目前有一个包含圆形图像的图像视图。

我已经这样设置了:

profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2
profileImageView.clipsToBounds = true

我尝试使用UIBezierPath在圆周上绘制圆弧,我想传递半径参数的图像视图的半径。

let circlePath = UIBezierPath(arcCenter: CGPoint(x: profileImageView.frame.size.width/2, y: profileImageView.frame.size.height/2), radius: IMG_VIEW_RADIUS, startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

我将如何做到这一点?

2 个答案:

答案 0 :(得分:3)

  

Swift 3.0

另一种方式

我刚添加了像这样的imageView

let imageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
imageView.backgroundColor = UIColor.green
imageView.layer.cornerRadius = imageView.frame.size.width / 2
imageView.clipsToBounds = true
self.view.addSubview(imageView)

执行圆形贝塞尔曲线路径

let circlePath = UIBezierPath(arcCenter: CGPoint(x: imageView.frame.size.width/2,y: imageView.frame.size.height/2), radius: CGFloat((imageView.frame.size.width/2) - 3.5), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath

//fill color
shapeLayer.fillColor = UIColor.clear.cgColor

//stroke color
shapeLayer.strokeColor = UIColor.white.cgColor

//line width
shapeLayer.lineWidth = 2.0

//finally adding the shapeLayer to imageView's layer
imageView.layer.addSublayer(shapeLayer)

现在使用相同的概念创建外部边框

let outerCirclePath = UIBezierPath(arcCenter: CGPoint(x: imageView.frame.size.width/2,y: imageView.frame.size.height/2), radius: CGFloat(imageView.frame.size.width/2 ), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
let outerLayer = CAShapeLayer()
outerLayer.path = outerCirclePath.cgPath

//fill color
outerLayer.fillColor = UIColor.clear.cgColor
//stroke color
outerLayer.strokeColor = UIColor.blue.cgColor
//line width
outerLayer.lineWidth = 15.0
imageView.layer.addSublayer(outerLayer)

现在更改为内层创建的形状图层的zPosition,因为它的半径小于外层,应该在顶部添加它以便可见

shapeLayer.zPosition = 2

你需要用第一个内层的半径调整一下。在我的情况下,我只是用3.5

减去半径

enter image description here

答案 1 :(得分:0)

只使用边框宽度和边框颜色

    profileImageView?.layer.cornerRadius = 5.0
        profileImageView?.layer.borderColor = UIColor.white.cgColor