Question

I have a viewController which contains a UITableView. This viewController implements UITableViewDelegate, and UITableViewDataSource, and I'm also trying to implement the following methods:

touchesBegan touchesMoved touchesEnded

However, these methods are not being called. I am trying to call these methods in response to the user touching a UIImageView that is inside a UITableView, but doing so only calls this method:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    [self.view bringSubviewToFront:_imageView];

    [UIView animateWithDuration:.3 animations:^{


        CGRect rect = [self.view convertRect:[tableView rectForRowAtIndexPath:indexPath] fromView:tableView];

        CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
        _imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);

    }];


    NSLog(@"Table row %d has been tapped", indexPath.row);


}

What I am hoping is to continue to call this method, but also call:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch view] == _imageView) {
        //need to make sure user can only move the UIImageView up or down
        CGPoint location = [touch locationInView:_imageView];
        //ensure UIImageView does not move outside UIITableView

        CGPoint pt = [[touches anyObject] locationInView:_imageView];
        CGRect frame = [_imageView frame];
        //Only want to move the UIImageView up or down, not sideways at all

        frame.origin.y += pt.y - _startLocation.y;

        [_imageView setFrame: frame];

        _imageView.center = location;

        return;
    }
}

when the user is dragging the UIImageView inside the UITableView.

Can anyone see what I am doing wrong?

Thanks in advance to all who reply

Was it helpful?

Solution

I had a similar problem to this except I was trying to implement touch detection to an UIWebView. I eventually got over it by adding a UIPanGestureRecognizer to the UIWebView's .view property.

Implement this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }

and then add the UIPanGestureRecognizer to the UITableView like so:

UIPanGestureRecognizer *panGesture; panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)]; panGesture.maximumNumberOfTouches = 1; panGesture.minimumNumberOfTouches = 1; panGesture.delegate = self; [self.tableView addGestureRecognizer:panGesture];

and then implement the method

- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer { }

Now, every time the UIPanGestureRecognizer detects a pan gesture, it will call that method and you can get the location of the UIPanGestureRecognizer by calling the method [recognizer locationInView:self.tableView.view]; which will return a CGPoint. You can also get the state of the UIPanGestureRecognizer by calling [recognizer state] which returns a UIGestureRecognizerState.

OTHER TIPS

Have you set userInteractionEnabled on your view that the touches will be inside?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top