问题
I'm trying to fire an action when UIScrollView is pulled down to a certain 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 works, until I end my touch event and then UIScrollView returns to it's original position. Any idea how to solve this?
回答1:
Check for range instead of single pixel as scrollView doesn't scroll back pixel by pixel. Add check like below-
if (scrollView.contentOffset.y < -100.0 && scrollView.contentOffset.y > -110.0)
{
//call your method
}
回答2:
I could not find the source For scrolling and holding the position of scrollview here is the code.
For the view where you want to set the scrollview and hold do like this ..
Viewcontroller.h
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;//from interface builder
@property (weak, nonatomic) IBOutlet UIView *contentView;//from interface
Finally the interface hierarchy should be like this view-->scrollView-->contentView-->view Contents viewController.m
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.scrollView layoutIfNeeded];
self.scrollView.contentSize = self.contentView.bounds.size;
}
//To dismiss the scrollView when return key pressed ..you can place this code anywhere where you want
(BOOL)textFieldShouldReturn:(UITextField *)textField {
self.scrollView.contentSize = self.view.frame.size;
return YES;
}
For more info check this..http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/ I hope this helps someone..
来源:https://stackoverflow.com/questions/14311576/pull-down-uiscrollview-and-hold-y-position