Wie erkenne ich Fingerbewegungen in meine benutzerdefinierte UIView hinein oder heraus?

Ich habe eine angepasste UIView

@interface EColumn: UIView

Ich habe viele Instanzen dieser EColumn in seiner Superansicht.

Wie kann ich erkennen, wenn der Finger im Bereich dieser UIView gehalten und bewegt wird und wenn er sich herausbewegt.

Ich meine nicht eine Geste mit dem Tippen, ich kann tippen Geste erkennen, indem ich Folgendes benutze:

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

2 Antworten

@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
hinzugefügt

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
hinzugefügt