user5867477
user5867477

Reputation:

Can't update the zoomScale of UIScrollView

I'm trying to assign newScale to zoomScale of a UIScrollView if zoomScale == oldScale, and the assignment zoomScale = newScale doesn't work.

Here's the test code in my playground.

import UIKit

let scrollView = UIScrollView()
let contentView = UIView()

scrollView.frame.size = CGSizeMake(200, 200)
contentView.frame.size = CGSizeMake(400, 400)

var zoomScale: CGFloat {
    get { return scrollView.zoomScale }
    set { scrollView.zoomScale = newValue }
}

var minimumZoomScale: CGFloat {
    get { return scrollView.minimumZoomScale }
    set { scrollView.minimumZoomScale = newValue }
}

func updateMinimumZoomScale(withScale newScale: CGFloat) {
    let oldScale = minimumZoomScale
    minimumZoomScale = newScale

    if zoomScale == oldScale {
        // zoomScale = 1, newScale = 0.1 //
        zoomScale = newScale
        zoomScale == newScale   // false
    }
}

updateMinimumZoomScale(withScale: 0.1)

And here's a screenshot of the result.

Screenshot of the playground result

In the screenshot, the result of zoomScale == newScale is false right after the assignment zoomScale = newScale.

I have really no idea why this is happening.

Upvotes: 3

Views: 1281

Answers (2)

Justin Vallely
Justin Vallely

Reputation: 6089

For me, the problem was with the Xcode auto-conversion to Swift 3. The delegate was set properly, but the delegate methods weren't properly formatted.

Upvotes: 0

Vishnu gondlekar
Vishnu gondlekar

Reputation: 3956

Even i tried out your code in playground and actual project but it didn't seem to work. Problem is you need to set delegate for your scroll view before setting zoom scale. Delegate is set this way

scrollView.delegate = self

When you try to set zoom scale for scroll view -viewForZoomingInScrollView method is called, and if your delegate is nil then that method won't be called. Hence it won't set zoom scale. You can try same sample in Xcode project with setting delegate before changing zoom scale and it will work.

Upvotes: 3

Related Questions