Drag and Drop UITableView

I am working on an iPhone application where I want to drag a table view (not a cell) to a specific point on the screen. I have a table sitting in the lower half of the screen, and I have an image in the upper half.

When I look at the table to see the rows below, the table should actually move up above the image (y pos decreases and the height increases). The table will rise until it fills the entire screen, leaving a few pixels for the image, and it will be there as a docking station, and from that moment, the table should scroll. The same behavior is expected for scrolling down.

This may seem a little strange, but this is the new iOS template that is appearing.

I managed to complete the first part using the code snippet below.

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanForScrollView:)]; panGestureRecognizer.maximumNumberOfTouches = 1; panGestureRecognizer.delegate = self; panGestureRecognizer.minimumNumberOfTouches = 1; [self.tableView addGestureRecognizer:panGestureRecognizer]; - (void)handlePanForScrollView:(UIPanGestureRecognizer *)gesture { switch (gesture.state) { case UIGestureRecognizerStateBegan: break; case UIGestureRecognizerStateChanged: { CGPoint movement = [gesture translationInView:self.view]; CGRect frame = self.tableView.frame; frame.origin.y += movement.y; if(frame.origin.y >= Y_OFFSET && frame.origin.y < self.original_frame.origin.y){ frame.size.height -= movement.y; self.tableView.frame = frame; [gesture setTranslation:CGPointZero inView:self.view]; } break; } case UIGestureRecognizerStateEnded: case UIGestureRecognizerStateCancelled:{ //NSLog(@"End"); break; } default: ; break; } } 

This works fine, and the table moves up and docks below Y_OFFSET. But, as you could imagine, the table no longer scrolls.

So i tried this

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if(self.tableView.frame.origin.y <= Y_OFFSET || self.tableView.frame.origin.y >= self.original_frame.origin.y){ NSLog(@"Here"); return YES; } return NO; } 

A message is printed here on the console, but the table does not scroll.

I even tried using panGestureRecognizer for a UITableView instead of my UIPanGestureRecognizer.

 self.tableView.panGestureRecognizer addTarget:self action:@selector(handlePanForScrollView:) 

But now the table scrolls and moves up.

"I want the table not to scroll, but to move to a certain point on the screen, and then stop moving and start scrolling."

What is the best way to handle this?

Greetings

+1
source share
3 answers

I think you are doing it well. You just need to stop the UIGestureRecognizer from processing any additional touches after a certain point. So, what would I do (with minimal modification to the existing code), just overrides the delegate function:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 

In this function, analyze the position of the table view and if the table view has already reached a certain height, return NO. This will stop the UIPanGestureRecognizer from accepting the touch event and pass it to the chain, which should go directly to the UIScrollView table.

Alternatively, if I had the time to figure this out, I will try to do this with the image as a table header (self.tableView.tableHeaderView). Thus, all scrolling is performed inside the scrolling in the table, and the table frame should not be moved. If you want to add some cool effects to tableHeaderView, you can always override the function UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView and slightly shift the image in the header, since the table scrolls the initial half-screen (this is the same effect as for Path ) However, this will not allow you to snap the image to the very top of the table view ... the whole image will scroll. Only my 2 cents. :)

+2
source

If I understand the question correctly, the easiest solution is to programmatically set the tableView header for the desired image. (I think this is similar to what Path does to set the location, except that they use a map, not an image.)

 UIImage *image = [UIImage imageNamed:@"image_name"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [self.tableView setTableHeaderView:imageView]; 

This is equivalent to dragging a UIImageView with the corresponding image into the tableView header in the storyboard.

Note: the presentation of the table title is different from the section title. See Documentation for more details.

0
source

Can you try this code to move the tableview

  [UIView animateWithDuration:1.5 animations:^ { //ADD CODES HERE FOR STARTING STATE (FRAME FOR UITABLEVIEW ) // eg: table.frame=CGRectMake(0,320,320,200); tableview lying on bottom of viewcontroller with 200px height } completion:^(BOOL finished) { // ADD CODES HERE TO MAKE YOUR FINAL FRAME OF UITABLEVIEW // eg: table.frame=CGRectMake(0,0,320,200); tableview moving to top of viewcontroller with 200px height }]; 

try this let me know the results

PLEASE BEFORE LABELING, LET VOICE, LET ME KNOW THE REASONS BECAUSE THESE CODES WORK FOR ME

-2
source

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


All Articles