2012-07-29 8 views
3

화면의 오른쪽 하단부터 가운데까지 두 손가락 대각선 스 와이프를 감지하고 싶습니다. "UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft"로 설정된 방향으로 UISwipeGestureRecognizer를 추가하려고 시도했지만 화면 중간에서 왼쪽 상단으로 스 와이프를 시작하더라도 핸들러가 호출되기 때문에 아무런 조치도 취하지 않았습니다.UIView에서 대각선 스 와이프 제스처를 감지합니다.

하위 클래스 UIGestureRecognizer가 필요하거나 touchesBegan 및 touchesMoved를 사용하여 처리 할 수 ​​있습니까?

답변

0

고마워!

touchesBegan, touchesMoved 및 touchesEnded 및 ot의 맞춤 코드 작성이 끝났습니다.

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

    if ([touches count] == 2) { 
     CGPoint nowPoint = [[touches anyObject] locationInView:self]; 
     if(nowPoint.x >= ALLOWED_X) 
      swipeUp = YES; 
    } 

    [super touchesBegan:touches withEvent:event]; 
} 


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 

    if ([touches count] == 2 && swipeUp) { 

     CGPoint nowPoint = [[touches anyObject] locationInView:self]; 
     CGPoint prevPoint = [[touches anyObject] previousLocationInView:self]; 

     if(nowPoint.x <= prevPoint.x && nowPoint.y <= prevPoint.y){ 
     } 
     else { 
      swipeUp = NO; 
     } 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesEnded:touches withEvent:event]; 
    CGPoint nowPoint = [[touches anyObject] locationInView:self]; 

    if ([touches count] == 2 && swipeUp && nowPoint.x <= DELTA_X && nowPoint.y <= DELTA_Y) { 
     NSLog(@"Invoke Method"); 
    } 
    swipeUp = NO; 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesCancelled:touches withEvent:event]; 
    swipeUp = NO; 
} 
0

가능한 한 가지 해결책은 스 와이프가 시작될 수있는 iPhone 화면에서 좌표 그룹을 매핑하고 잠재적으로 끝낼 수있는 다른 좌표를 매핑하는 것입니다. 그런 다음 touchesBegan:touchesMoved: 방법에서 스 와이프의 시작 지점과 끝 지점을 비교하고 해당 항목이 대각선인지 여부를 확인할 수 있습니다.

1

원인의 터치 방법은 모든 제스처를 수행 할 수 있으며 제스처는 ios3.2부터 지원됩니다. 나는 간단한 코드를 줄 수있다. 직접 수정할 수도있다.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    isTwoFingersBegin = NO; 
    isTwoFingersEnd = NO; 
    UITouch *touch = [touches anyObject]; 
    UIView *touchView = [touch view]; 
    if ([touches count] == 2) 
    { 
     isTwoFingersBegin = YES; 
    } 

    touchStartPoint = [touch locationInView:self.view]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    isSwip = YES; 
} 
#define TOUCH_MOVE_EFFECT_DIST 30 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    UIView *touchView = [touch view]; 
    touchEndPoint = [touch locationInView:self.view]; 
    if ([touches count] == 2) 
    { 
     isTwoFingersEnd = YES; 
    } 
    CGPoint deltaVector = CGPointMake(touchEndPoint.x - touchStartPoint.x, touchEndPoint.y - touchStartPoint.y); 

    if (fabsf(deltaVector.x) > TOUCH_MOVE_EFFECT_DIST 
       &&fabsf(deltaVector.y) > TOUCH_MOVE_EFFECT_DIST 
       && isSwip &&isTwoFingersBegin&&isTwoFingersEnd) { 
       theSwipGesture=RightUpGesture; 
      } 
    else if (fabsf(deltaVector.x) <- TOUCH_MOVE_EFFECT_DIST 
       && fabsf(deltaVector.y) <- TOUCH_MOVE_EFFECT_DIST 
       && isSwip&&isTwoFingersBegin&&isTwoFingersEnd) { 
       theSwipGesture=LeftDownGesture; 
      } 

    isSwip = NO; 
} 
1

당신은 어쨌든 touchesBegantouchesMoved를 사용하도록 서브 클래스해야합니다. 나는 당신이 말한 것처럼 UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft을 할 것이고 두 개의 CGRect을 수락 할 수있는 시작점 하나와 수용 가능한 끝점 하나를 설정합니다. 그런 다음 UIGestureRecognizerDelegate 메서드 gestureRecognizer:shouldReceiveTouch:을 사용하고 CGRectContainsPoint()을 사용하여 터치 포인트가 허용되는 시작 rect에 있는지 확인하십시오. 그런 다음 UISwipeGestureRecognizer 서브 클래스에서 (내 생각에는) touchesEnded:withEvent:을 무시하고 끝 부분 터치가 허용되는 rect에 있는지 확인하십시오. 그렇지 않은 경우 상태를 UIGestureRecognizerStateCancelled (또는 사용자가 제스처를 취소해야 함)로 설정합니다. 또한 첨부 된보기의 속성이 multipleTouchEnabled인지 확인하십시오. 나는 실제로 이것을 시도하지는 않았지만, 그런 종류의 일이 그것을해야한다. 행운을 빕니다!

편집

사실, 당신은 허용 시작/끝 지점에 대한 특정 RECT 값에 대해 걱정할 필요가, 그것은 독립적 인 장치, 당신이 할 수있는 만들고 싶어하지 않은 경우 :

//swap in whatever percentage of the screen's width/height you want in place of ".75f" 

//set the origin for acceptable start points 
CGFloat startRect_x = self.view.frame.size.width * .75f; 
CGFloat startRect_y = self.view.frame.size.height * .75f; 

//set the size 
CGFloat rectWidth = self.view.frame.size.width - startRect_x; 
CGFloat rectHeight = self.view.frame.size.height - startRect_y; 

//make the acceptable start point rect 
CGRect startRect = CGRectMake(startRect_x, startRect_y, rectWidth, rectHeight); 

//set the origin for the accepable end points 
CGFloat endRect_x = self.view.center.x - rectWidth/2; 
CGFloat endRect_y = self.view.center.y - rectHeight/2; 

//make the acceptable end point rect 
CGRect endRect = CGRectMake(endRect_x, endRect_y, rectWidth, rectHeight); 
1

당신은 UIPanGestureRecognizer

- (void)viewPanned:(UIPanGestureRecognizer *)panGR 
{ 
    CGPoint translation = [panGR translationInView:self]; 

    CGFloat threshold = self.bounds.size.width/2; 

    // Detect 
    Direction direction = DirectionUnknown; 

    if (translation.x > threshold) { 
     if (translation.y > threshold) { 
      direction = DirectionBottomRight; 
     } else if (translation.y < -threshold) { 
      direction = DirectionTopRight; 
     } else { 
      direction = DirectionRight; 
     } 
    } else if (translation.x < -threshold) { 
     if (translation.y > threshold) { 
      direction = DirectionBottomLeft; 
     } else if (translation.y < -threshold) { 
      direction = DirectionTopLeft; 
     } else { 
      direction = DirectionLeft; 
     } 
    } else { 
     if (translation.y > threshold) { 
      direction = DirectionBottom; 
     } else if (translation.y < -threshold) { 
      direction = DirectionTop; 
     } 
    } 
} 
을 사용할 수 있습니다
관련 문제