2013-12-17 14 views
1

手指的移動,有一個定製的UIView我如何檢測或縮小我定製的UIView

@interface EColumn : UIView

我有這個EColumn的許多情況下,它的超視圖。

我該如何檢測手指何時在此UIView區域中移動以及何時移出。

我的意思不是敲擊手勢,我可以利用這個檢測點觸手勢:

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

回答

4
@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

您可以檢測特定的時間與UILongPressGestureRecognizer手指保持。對於這一點,你也可以指定minimumPressDurationnumberOfTouchesRequired

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

爲了檢測動作,您可以用UIPanGestureRecognizer

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