0

I've looked absolutely everywhere for hours and can't find a solution that works for me. All I'm trying to do is make my app do something when the position of the content in the uiscrollview is above a certain position and below another position. I hope that makes sense. Here is the code I thought would work:

- (void)viewDidLoad
{
    if (self.scrollView4.contentOffset.y >= 100 && self.scrollView4.contentOffset.y <=     200)
    {
         [self.scrollView4 setContentOffset:CGPointMake(100,0)];
    }
}

Any help would be greatly appreciated!

EDIT: If forgot to mention that if I make an "if" statement to say that if the contentOffset is equal to the contentOffset that it starts off with, then the action is executed. It would seem that the contentOffset remains at the value that it was given when the app loaded. I should mention that earlier in the app, I set the contentOffset to a value but I don't know if that affects anything.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Invalid Memory
  • 717
  • 7
  • 23

2 Answers2

0

UIScrollViewDelegate has a method scrollViewDidScroll (which executes whenever the associated scrollview scrolls). You can implement your logic in there.

bengoesboom
  • 2,119
  • 2
  • 23
  • 27
0

This works fine for me. If it doesn't work for you I would suggest that your problem lies elsewhere

[self.scrollview4 setDelegate:self];
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= 100 && scrollView.contentOffset.y <= 200)
    {
         NSLog(@"Caught");
    }
}
zimmryan
  • 1,099
  • 10
  • 19
  • Thanks. It was the [self.scrollview4 setDelegate:self]; that did it. As I'm new to programming, could you just tell briefly why it didn't work when I was missing the "setDelegate"? – Invalid Memory May 17 '13 at 06:52
  • Think of a delegate as someone the computer asks or tells when it is about to do something, so you are simply saying to your scrollview, tell me before you scroll, after you scroll, if you are going to scroll etc. So your scrollview wanted to send the messages, but you never told it who to send them to. They are an important part to iOS so you can read more about them here https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html – zimmryan May 17 '13 at 13:59
  • Thanks, makes more sense now and I'll have a read of that documentation. – Invalid Memory May 18 '13 at 06:44