user4001326
user4001326

Reputation:

Trigger function when a variable has a certain value

I have a UIScrollView and I want to trigger a function whenever the content offset is equal to a certain value.

I tried

var defaultOffset: CGPoint = ...

func checkScroll() {
    for i = 1; i > 0; i {
        if ScrollView.contentOffset.x == defaultOffset.x {
            functionToTrigger()
            i = 0
        }
    }
}

but that doesn't work.

Help would be much appreciated!

Upvotes: 1

Views: 68

Answers (1)

Rafael Machado
Rafael Machado

Reputation: 655

I would you recommend a different approach. Set this class as the UIScrollViewDelegate. Then just use scrollViewDidScroll method to compute your value, like so:

override func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.x == defaultOffset.x {
        functionToTrigger()
    }
}

Upvotes: 3

Related Questions