open func point(inside point: CGPoint, with event: UIEvent?) -> Bool open func convert(_ point: CGPoint, to view: UIView?) -> CGPoint open func convert(_ point: CGPoint, from view: UIView?) -> CGPoint open func convert(_ rect: CGRect, to view: UIView?) -> CGRect open func convert(_ rect: CGRect, from view: UIView?) -> CGRect
override func viewDidLoad() { super.viewDidLoad() view.addSubview(myButton) let center2 = view.convert(myButton.center, to: UIApplication.shared.keyWindow) }
view.convert(myButton.center, to: UIApplication.shared.keyWindow)
btn.contentEdgeInsets = .zero btn.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 25) btn.titleEdgeInsets = UIEdgeInsets(top: 2, left: 12, bottom: 2, right: 2)
let btn = UIButton(type: .custom) btn.frame = CGRect(x: 100, y: 80, width: 40, height: 20) btn.setTitleColor(.white, for: .normal) btn.setTitle("jack", for: .normal) btn.titleLabel?.font = UIFont.systemFont(ofSize: 10) btn.setImage(UIImage.init(named: "loveHeat"), for: .normal) btn.backgroundColor = UIColor.black.withAlphaComponent(0.6) btn.layer.cornerRadius = 4; btn.layer.masksToBounds = true; btn.imageEdgeInsets = UIEdgeInsets(top: 3, left: 2, bottom: 1, right: 25) btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 0) btn.isUserInteractionEnabled = false
view.layer.mask = maskLayer
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) view.backgroundColor = .red let maskLayer = CAShapeLayer() let path = UIBezierPath(ovalIn: view.bounds) maskLayer.path = path.cgPath view.layer.mask = maskLayer
这里我们首先创建了一个红色的 UIView,然后创建了一个圆形的 CAShapeLayer,将其 path 属性设置为一个内切于 view 边界的圆形 UIBezierPath,最后将 view 的 mask 属性设置为这个 CAShapeLayer,这样就实现了将视图裁剪成圆形的效果。
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, let window = windowScene.windows.first(where: { $0.isKeyWindow }) { // 在这里使用当前正在使用的window对象 }