Call this method from your viewDidLoad. This method will disable table scrolling to prevent manual scrolling. Assuming you have 100 rows 30 in height and myview is the view you have to scroll through. In my view, scroll gestures are added, it is important to set the direction of movement
- (invalid) initialSetup {
[self.tableView setScrollEnabled:NO]; int numberOfRows=100; int rowHeight=30; myview=[[UIView alloc]initWithFrame:CGRectMake(280, 0, 30, numberOfRows*rowHeight)]; [myview setBackgroundColor:[UIColor yellowColor]]; [self.view addSubview:myview]; UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(motion:)]; [swipeDown setDirection:UISwipeGestureRecognizerDirectionDown]; [myview addGestureRecognizer:swipeDown]; UISwipeGestureRecognizer *swipeUp=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(motion:)]; [swipeUp setDirection:UISwipeGestureRecognizerDirectionUp]; [myview addGestureRecognizer:swipeUp];}
The following method will be called when the swipe gesture is either swipeUp or swipeDown. When the method receives the swipeUp gestures, it scrolls the last row currently visible in the table view at the top. When the method receives swipeDown, it scrolls the first row that is currently visible at the bottom
- (void) motion: (UISwipeGestureRecognizer *) sender {
if (sender.direction==UISwipeGestureRecognizerDirectionUp) { [self.tableView scrollToRowAtIndexPath:[self.tableView.indexPathsForVisibleRows objectAtIndex:[self.tableView.indexPathsForVisibleRows count]-1] atScrollPosition:UITableViewScrollPositionTop animated:YES]; }else if(sender.direction==UISwipeGestureRecognizerDirectionDown) { [self.tableView scrollToRowAtIndexPath:[self.tableView.indexPathsForVisibleRows objectAtIndex:0 ] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; }
}
source share