how to use values returned by Swift function

前端 未结 5 408
不思量自难忘°
不思量自难忘° 2021-01-26 01:36

I am trying to use a Swift function to place a circle in the centre of a view so it is always on centre regardless of screen size. I can draw the circle at a point defined by a

5条回答
  •  鱼传尺愫
    2021-01-26 01:58

    First of all, UIViews have a center attribute, so you don't need to calculate it.

    Second, the behavior may be inconsistent if changing it with viewDidLoad, and won't react to any changes to size/rotation.

    I would recommend making a subClass of UIView that manages drawing the circle and doing something like this:

    class ViewController: UIViewController {
    
        var circle: CircleView?
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            centerCircle()
        }
    
        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            centerCircle()
        }
    
        func centerCircle() {
            circle?.center = self.view.center
        }
    }
    

提交回复
热议问题