我有一个特定的cg点,我希望我的UIImageView以它为中心。我不知道怎么做。以下是我的尝试:
var firstDotView: UIView?
let dotSize: CGFloat = 20
@IBOutlet weak var imgPreViewOutlet: UIImageView!
override func viewDidLoad(){
firstDotView = UIView.init()
firstDotView?.frame = CGRect.init(x: 60, y: 60, width: dotSize, height: dotSize)
firstDotView?.center = CGPoint.init(x: 60, y: 60)
drawFirstDot()
imgPreViewOutlet.addSubview(firstDotView!)
}
// DRAW ROUND CIRCLE
func drawFirstDot() -> Void {
let layer = CAShapeLayer.init()
layer.path = UIBezierPath.init(roundedRect: firstDotView!.bounds, cornerRadius: 50).cgPath
layer.fillColor = UIColor.blue.cgColor
layer.backgroundColor = UIColor.yellow.cgColor
firstDotView?.layer.addSublayer(layer)
}
最佳答案
就这样做吧。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let firstDotView = UIView(frame: CGRect(x: imgPreViewOutlet.bounds.width/2 - dotSize/2,
y: imgPreViewOutlet.bounds.height/2 - dotSize/2,
width: dotSize,
height: dotSize))
firstDotView.layer.cornerRadius = dotSize/2
firstDotView.backgroundColor = UIColor.yellow
firstDotView.translatesAutoresizingMaskIntoConstraints = false
imgPreViewOutlet.addSubview(firstDotView)
}