Scrolling UITableView programmatically with acceleration and deceleration

I am trying to scroll through a UITableView programmatically by clicking a button. This should be the same effect when doing this manually, or even slightly longer scroll time.

At first I tried to use scrollToRowAtIndexPath: atScrollPosition: animated: however it is too fast and fixed in time. Also it has no acceleration and deceleration.

Any ideas on how to scroll a UITableView programmatically since it scrolls manually? I thought to send some touches to him. Do you think this might work?

Any ideas and help are greatly appreciated! Thanks!

+6
source share
3 answers

UITableview is a subclass of UIScrollview. Like UIScrollview, you can programmatically set the scroll position of a UITableview by setting the contentOffset property.

You change this property value inside the animation block so that you have more time control. You will need to calculate the position of the Y point. Suppose you want to scroll to the 50th row, it could be 50 * rowHeight (assuming a uniform height).

For instance:

CGFloat calculatedPosY = 50 * rowHeight // do you own calculation of where you'd like it. [UIView animateWithDuration:0.2 animations:^{theTableview.contentOffset = CGPointMake(0.0, calculatedPosY);} completion:^(BOOL finished){ }]; 

Another way you can use also provides more options

Take a look at this method:

 + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion 
+9
source

Set your button in the cell subcategory and use this code to scroll the table programmatically when you click the button. And if you do not set the button as the subzone of the table cell, then change this code as necessary.

  CGPoint point = [button.superview convertPoint:button.frame.origin toView:self.tableView]; CGPoint contentOffset = self.tableView.contentOffset; contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need [self.tableView setContentOffset:contentOffset animated:YES]; 
+6
source

Well, these are just a couple of answers from the head, but you can try using this method (in UIScrollView , of which UITableView is a subclass):

 -(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated 

Once -(CGRect) rectForRowAtIndexPath: gives you the coordinates of the cell, you can animate the location of the cell. But instead of calling it once, name it a couple of times with a smaller offset, then with larger and larger offsets, and then with smaller ones. I am worried that it will be difficult to make it smooth.

However, if this does not work, the contentOffset value inside the UIView animateWithDuration:delay:options:animations:completion block UIView animateWithDuration:delay:options:animations:completion . Perhaps if you use UIViewAnimationOptionCurveEaseInOut, you may get an effect that you want to just call it once. If not, start several animations in each part of the path and cover large and large distances before slowing down.

+2
source

Source: https://habr.com/ru/post/899105/


All Articles