Slide UIScrollView and hold y position

I try to trigger an action when a UIScrollView is reduced to a specific point. This is what I have right now:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y == -100.0) { NSLog(@"Trigger"); [scrollView setContentOffset:CGPointMake(0.0, -200.0) animated:YES]; } } 

This kind of work until I finish my touch event, and then the UIScrollView returns to its original position. Any idea how to solve this?

+2
source share
2 answers

Check the range, not a single pixel, as scrollView does not scroll pixel by pixel. Add a check as shown below -

 if (scrollView.contentOffset.y < -100.0 && scrollView.contentOffset.y > -110.0) { //call your method } 
0
source

I could not find the source. To scroll and hold the scrollview position, here is the code.

To represent where you want to set the scroll and hold, do this.

Viewcontroller.h
@property (weak, non-atomic) IBOutlet UIScrollView * scrollView; // from the interface constructor @property (weak, non-nuclear) IBOutlet UIView * contentView; // from the interface

Finally, the interface hierarchy should be: view → scrollView → contentView → view Contents viewController.m

 -(void)viewDidLayoutSubviews 

{

 [super viewDidLayoutSubviews]; [self.scrollView layoutIfNeeded]; self.scrollView.contentSize = self.contentView.bounds.size; 

}

// Reject scrollView when you press the return key. You can place this code anywhere you want.

  • (BOOL) textFieldShouldReturn: (UITextField *) textField {

    self.scrollView.contentSize = self.view.frame.size;

    return YES;

}

Check this out for more information. http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/ Hope this helps someone ...

0
source

All Articles