gpt4 book ai didi

swift - 如何将之字形边框添加到圆形 ImageView

转载 作者:行者123 更新时间:2023-11-28 10:37:52 27 4
gpt4 key购买 nike

我有一个 ImageView ,我需要像图像一样向它添加锯齿形边框

Image wih zigzag border

我怎样才能做到这一点?感谢任何帮助。

谢谢查克舒阿罗拉

最佳答案

使用直线的简单示例(曲线更复杂,如果您需要的话,我建议您在绘图程序中创建 mask !)。

此代码假设 Storyboard中有一个 UIImageView...

class DemoViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
setImageMask()
}

func setImageMask() {

// Set the center and radius (pure circle rather than rectangle)
let center = CGPoint(x: imageView.bounds.midX, y: imageView.bounds.midY)
let radius = min(imageView.bounds.midX, imageView.bounds.midY)

// Inset radius is the amount that the inner points are inset by
let insetRadius: CGFloat = radius * 0.85

// Calculate the segment arc sizes
let numberOfPoints = 17
let segmentArcSize = 360.0 / CGFloat(numberOfPoints)
let arcMid = segmentArcSize / 2.0

// Start at the top of the circle, but subtract half an arc so the outer point is at the top
var angle = -90.0 - arcMid

// Create the path
let path = UIBezierPath()

// Move to the inner starting point
let startPoint = CGPoint(x: center.x + insetRadius * cos(angle.toRadians()) , y: center.y + insetRadius * sin(angle.toRadians()))
path.move(to: startPoint)

// Loop and draw the jagged points around the circlw
for _ in 0 ..< numberOfPoints {

// Outer point
let midAngle = angle + arcMid
let midPoint = CGPoint(x: center.x + radius * cos(midAngle.toRadians()), y: center.y + radius * sin(midAngle.toRadians()))
path.addLine(to: midPoint)

// Inner point
let endAngle = angle + segmentArcSize
let endPoint = CGPoint(x: center.x + insetRadius * cos(endAngle.toRadians()) , y: center.y + insetRadius * sin(endAngle.toRadians()))
path.addLine(to: endPoint)
angle += segmentArcSize
}

// End drawing
path.close()

// Mask the image view
let mask = CAShapeLayer()
mask.path = path.cgPath
imageView.layer.mask = mask
}

}


extension CGFloat {

// Convert degrees to radians

func toRadians() -> CGFloat {
return self * CGFloat.pi / 180.0
}
}

enter image description here

关于swift - 如何将之字形边框添加到圆形 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52400063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com