Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

ios - When using scrollView.zoom I get different results from pressing a button to calling a function

I am trying to zoom to a button on a UIScrollView. The first function is one that I run if I segue to the vc, the second is run from a button in the view.

When running the second function it goes perfectly and centres correctly

On the first however, despite being identical code, it has an offset in the x of about 40-50 pixels (Shows a point left of the button, not any part of it) although this offset changes dependent on the button.

func moveScrollviewToRoom(){
    scrollView.zoom(to: artButton.frame, animated: true)
}

@IBAction func moveScrollview(_ sender: Any) {
    scrollView.zoom(to: artButton.frame, animated: true)
}

The function was called here:

override func viewDidLoad() {
    super.viewDidLoad()
    
    if moveToRoom != "" {
        moveScrollviewToRoom()
       }

Any help would be greatly appreciated

Cheers!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Two points...

1 - Views and subviews have not yet been positioned by auto-layout in viewDidLoad(), so the artButton.frame will not be correct.

2 - Calling any function that animates a view from viewDidLoad() won't give the desired results, because the view is not even visible yet.

Move your call to viewDidAppear():

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if moveToRoom != "" {
        moveScrollviewToRoom()
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...