Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 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 Reply

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()
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...