ChuckKelly
ChuckKelly

Reputation: 1740

IOS detect when user stops touching UIImageView

Any idea how to do this? I have a UIImageView inside of each cell in my UITableView and I want to disable scroll when the user starts touching the UIImageView and then enable it once the user stops dragging their finger on the photo.

Upvotes: 2

Views: 407

Answers (3)

Goppinath
Goppinath

Reputation: 10739

There are two ways to implement this, either include UITapGestureRecognizer or UIPanGestureRecognizer to your UIImageView and set the target and action where target is your custom UITableViewCell class or include

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

methods in the UIImageView custom class and set the delegate via protocol to your custom UITableViewCell.

for UIGestureRecognizer you can check its state property UIGestureRecognizer Class Reference to know the state with the help of switch

typedef enum {
   UIGestureRecognizerStatePossible,

   UIGestureRecognizerStateBegan,
   UIGestureRecognizerStateChanged,
   UIGestureRecognizerStateEnded,
   UIGestureRecognizerStateCancelled,

   UIGestureRecognizerStateFailed,

   UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} 

Finally, by toggling the UITableView's scrollEnabled property to stop and start scrolling where UITableView is a sub class of UIScrollView

Upvotes: 1

Warif Akhand Rishi
Warif Akhand Rishi

Reputation: 24248

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if ([touch.view isKindOfClass:[UIImageView class]]) {
        NSLog(@"self.tableView.scrollEnabled = NO");
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if ([touch.view isKindOfClass:[UIImageView class]]) {
        NSLog(@"self.tableView.scrollEnabled = YES");
    }
}

Upvotes: 1

user2289379
user2289379

Reputation:

I just give my logic.

Add UIPanGestureRecognizer to each cell of UITableView.

UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[cell addGestureRecognizer:panGestureRecognizer];

And in method name is handlePanFrom:

 - (void)handlePanFrom:(UIPanGestureRecognizer*)recognizer 
{

    CGPoint translation = [recognizer translationInView:recognizer.view];
    CGPoint velocity = [recognizer velocityInView:recognizer.view];

    if (recognizer.state == UIGestureRecognizerStateBegan) 
    {
      /// track began
      tableView.userInteractionEnabled = NO;    
    } 
    else if (recognizer.state == UIGestureRecognizerStateChanged) 
    {
        // track the movement
    } else if (recognizer.state == UIGestureRecognizerStateEnded) 
    {
        //  final position
        tableView.userInteractionEnabled = YES;
    }
 }

Make sure your UIIMageView must be set as a userInteractionEnabled = YES;. Because by default UIIMageView have to set userInteractionEnabled = NO;.

Upvotes: 3

Related Questions