Cómo detectar movimientos de dedos dentro o fuera de mi UIView personalizado

Tengo una UIView personalizada

@interface EColumn: UIView

Tengo muchas instancias de esta EColumna en su súper vista.

¿Cómo puedo detectar cuándo el dedo se mantiene y se mueve en el área de esta UIView y cuándo se mueve?

No me refiero a un gesto de tap, puedo detectar el gesto de tocar usando esto:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taped:)];
        [self addGestureRecognizer:tapGesture];
0

2 Respuestas

@implementation EColumn

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *view = [self touchedViewWithTouches:touches andEvent:event];
    NSLog(@"%@",view);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *view = [self touchedViewWithTouches:touches andEvent:event];
    NSLog(@"%@",view);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *view = [self touchedViewWithTouches:touches andEvent:event];
    NSLog(@"%@",view);
}

- (UIView *)touchedViewWithTouches:(NSSet *)touches andEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:touch.view];

    UIView *touchedView;

    for (UIView *view in self.subviews)
    {
        if(CGRectContainsPoint(view.frame, touchLocation))
        {
            touchedView = view;
            break;
        }
    }

    return touchedView;
}

@end
0
agregado

You can detect the finger hold for specific time with the UILongPressGestureRecognizer . For this, you can also specify minimumPressDuration and numberOfTouchesRequired

UILongPressGestureRecognizer *longPressRecognizer = 
         [[UILongPressGestureRecognizer alloc]
         initWithTarget:self 
         action:@selector(longPressDetected:)];
    longPressRecognizer.minimumPressDuration = 3;
    longPressRecognizer.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:longPressRecognizer];

For detecting moves, you can use UIPanGestureRecognizer

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [self addGestureRecognizer:panRecognizer];
0
agregado