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,這樣就實現了將視圖裁剪成圓形的效果。