Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6808759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:59:43+00:00 2026-05-26T19:59:43+00:00

I have a UIViewController that has both a UITable and a UIView on it.

  • 0

I have a UIViewController that has both a UITable and a UIView on it. I want to be able to pick up items that are displayed in the UIView and drop them on to a cell in the UITableView.I have had to revert to using touch events as opposed to the new UIGestureRecognisers to take a snap shot of the UIView tapped so that this snap shot is dragged over to the UITableView as opposed to the UIView touched. This works great using the following,

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *t =[touches anyObject];

    UIGraphicsBeginImageContext(t.view.bounds.size);
    [t.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.draggedView = [[UIImageView alloc] initWithImage:img];

    CGPoint centre = [[touches anyObject] locationInView:self.view];
    [self.draggedView setCenter:centre];
    [self.draggedView setAlpha:0.5];
    [self.view addSubview:self.draggedView];   
}

However, in the touchesEnded event when I try to evaluate which UIView the touch ended on I always get a UIView instead of the UITableView when I drop on it. Any ideas would be very welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:59:44+00:00Added an answer on May 26, 2026 at 7:59 pm

    I think I have come up with better solution to this problem using the latest GestureRecognisers. I use the following LongPress Gesture Recogniser inside my base TableView Controller.

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
        if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
            [gestureRecognizer addTarget:self action:@selector(longGestureAction:)];
        }
        return YES;
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
        return YES;
    }
    
    -(void)longGestureAction:(UILongPressGestureRecognizer *)gesture{
        UITableViewCell *cell= (UITableViewCell *)[gesture view];
    
        switch ([gesture state]) {
            case UIGestureRecognizerStateBegan:{          
                NSIndexPath *ip = [self.tableView indexPathForCell:cell];
                [self.tableView setScrollEnabled:NO];
                if(ip!=nil){
                    [self.draggableDelegate dragAndDropTableViewController:self  draggingGestureWillBegin:gesture forCell:cell];
                    UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self ];
                    //switch the view the gesture is associated with this will allow the dragged view to continue on where the cell leaves off from
                    [draggedView addGestureRecognizer:[[cell gestureRecognizers]objectAtIndex:0]]; 
                    [self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidBegin:gesture forCell:cell];
                }
            }
                break;
            case UIGestureRecognizerStateChanged:{
                [self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidMove:gesture];
            }
                break;
            case UIGestureRecognizerStateEnded:{
                UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self];
                if(draggedView==nil)
                    return;
    
                //this does not seem like the best way to do this yet you really don't want to fire one after the other I don't think
                [self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidEnd:gesture];
                [self.dropableDelegate dragAndDropTableViewController:self droppedGesture:gesture];           
    
                [self.tableView setScrollEnabled:YES];
                [self.tableView reloadData];
            }
                break;
    
    //        case UIGestureRecognizerStateCancelled:
    //        case UIGestureRecognizerStateFailed:
    //        case UIGestureRecognizerStatePossible:
    //            [self.dragAndDropDelegate dragAndDropTableViewController:self draggingGesture:gesture endedForItem:nil];
                break;
            default:
                break;
        }
    }
    

    In the TableViews that extend this base class I add the following to each cell in the cellForIndexPath TableViewDataSource,

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
    longPress.delegate = self;
    [cell addGestureRecognizer:longPress];
    

    and finally all you need to do is implement the delegate methods like so,

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
        if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
            [gestureRecognizer addTarget:self action:@selector(longGestureAction:)];
        }
        return YES;
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
        return YES;
    }
    
    -(void)longGestureAction:(UILongPressGestureRecognizer *)gesture{
        UITableViewCell *cell= (UITableViewCell *)[gesture view];
    
        switch ([gesture state]) {
            case UIGestureRecognizerStateBegan:{          
                NSIndexPath *ip = [self.tableView indexPathForCell:cell];
                [self.tableView setScrollEnabled:NO];
                if(ip!=nil){
                    [self.draggableDelegate dragAndDropTableViewController:self  draggingGestureWillBegin:gesture forCell:cell];
                    UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self ];
                    //switch the view the gesture is associated with this will allow the dragged view to continue on where the cell leaves off from
                    [draggedView addGestureRecognizer:[[cell gestureRecognizers]objectAtIndex:0]]; 
                    [self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidBegin:gesture forCell:cell];
                }
            }
                break;
            case UIGestureRecognizerStateChanged:{
                [self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidMove:gesture];
            }
                break;
            case UIGestureRecognizerStateEnded:{
                UIView *draggedView = [self.draggableDelegate dragAndDropTableViewControllerView:self];
                if(draggedView==nil)
                    return;
    
                //this does not seem like the best way to do this yet you really don't want to fire one after the other I don't think
                [self.draggableDelegate dragAndDropTableViewController:self draggingGestureDidEnd:gesture];
                [self.dropableDelegate dragAndDropTableViewController:self droppedGesture:gesture];           
    
                [self.tableView setScrollEnabled:YES];
                [self.tableView reloadData];
            }
                break;
    
    //        case UIGestureRecognizerStateCancelled:
    //        case UIGestureRecognizerStateFailed:
    //        case UIGestureRecognizerStatePossible:
    //            [self.dragAndDropDelegate dragAndDropTableViewController:self draggingGesture:gesture endedForItem:nil];
                break;
            default:
                break;
        }
    }
    

    You can find the source for this here. It took me a long time to get this right so I really hope this saves someone else the time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIViewController that I want to load from a NIB that has
I have UINavigation Controller that has a UIViewController pushed onto the stack and displayed.
So I have a UIViewController that has a UITabBar in the view. I want
In my iPhone app I have a UIViewController that has a UITableView and another
I have a UIViewController that has an UIPopoverController that has an UINavigationController then a
I'm trying to do some simple animation. I have a UIViewController that has two
I have an app where I have an imageview displayed within a UIView that
I have a root UIViewController subclass that has a UITabBar, (I'm not using UITabBarController)
So I have a UIViewController subclass called MyTabBarViewController that has a UIScrollView. Inside of
I have a UIViewController that is pushed onto a UINavigationController and is currently displayed.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.