我正在尝试创建一个圆形UIView,中间有一个洞,然后将该视图与另一个视图的顶部相结合。但是,我不确定我是否正确地采取行动
到目前为止,这就是我在操场上的表现:
import Foundation
import UIKit
import CoreGraphics
extension UIColor {
static func getRandomColor() -> UIColor {
//Generate between 0 to 1
let red:CGFloat = CGFloat(drand48())
let green:CGFloat = CGFloat(drand48())
let blue:CGFloat = CGFloat(drand48())
return UIColor(red:red, green: green, blue: blue, alpha: 1.0)
}
}
func makeCircularView(x: Int = 0 , y : Int = 0 , width: Double, height : Int, color: UIColor) -> UIView {
let view : UIView = UIView(frame:CGRect(x: x, y: y, width: Int(width), height: height))
view.alpha = 0.5
view.layer.cornerRadius = view.bounds.size.width / 2
view.backgroundColor = color
view.layer.masksToBounds = true
return view
}
let uiView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
uiView.backgroundColor = .red
uiView.clipsToBounds = true
uiView.layer.shadowRadius
uiView.layer.cornerRadius = 5
uiView
let cir : UIView
let circle2 : UIView
cir = makeCircularView(width : 30 , height : 30, color: UIColor.gray)
circle2 = makeCircularView( x: Int(7.5) , y : Int(7.5), width: 15, height: 15, color: UIColor.clear)
cir.addSubview(circle2)
uiView.addSubview(cir)
cir.topAnchor.constraint(equalTo: uiView.topAnchor, constant: -20).isActive = true
cir.leadingAnchor.constraint(equalTo: uiView.leadingAnchor, constant: -10).isActive = true
答案 0 :(得分:0)
使用此func并传递角落视图。
func createView(_ myview : UIView) {
myview.backgroundColor = UIColor.clear
myview.layer.cornerRadius = myview.frame.width/2 // assuming myview is squre
myview.layer.borderColor = UIColor.white.cgColor
myview.layer.borderWidth = 4
}
<强>输出强>
修改
对于集成图像,您可以执行以下任何操作
UIImageview