2012-05-03 2 views
1

스 와이프를 수행 한 방향을 인식하고 내 뷰에서 어떤 객체를 보냈는지 알 필요가 있습니다. 지금 당장 해결 방법을 찾았지만 복잡하고 괴물처럼 보이므로 가장 간단한 해결책이 있을까요? 몇 마디 내 해결 방법 :UIButton 객체와 함께 스 와이프 방향을 인식합니다.

  1. 내 인식의 모든 UIButton 개체에 연결된 인식자를 첨부했습니다.
  2. 스 와이프를 수행하는 동안 어떤 버튼이 두드려 졌는지 표시하는 표시기가 있습니다.
  3. 이 표시기를 확인하고 정확한 결정을 내리기 위해 방향을 스 와이프해야합니다. 어떤 오브젝트와 방향으로 이동해야합니까?

감사합니다.

 
@synthesize swipeDirection; //label which shows swipe direction 
@synthesize buttonNumber;  //label which shows number of button which being tapped 

    - (void)viewDidLoad 
    { 
     UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  //creating button  
     [button setTitle:@"test1" forState:UIControlStateNormal];       
     button.frame = CGRectMake(100, 100, 100, 100); 
     [button addTarget:self           //adding selector which will update indicator which button been tapped, there isn't way to make swipe without tap button 
        action:@selector(updateButtonNumber:) 
     forControlEvents:UIControlEventTouchDown]; 
     [button setTag:1]; 
     [self.view addSubview:button];    //add button to view 
     [self beginListenToSwipes:button];   //send button to method which will add UISwipeGestureRecognizer for 4 directions 

     /*same task for second button*/ 
     UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
     [button1 setTitle:@"test2" forState:UIControlStateNormal];       
     button1.frame = CGRectMake(200, 200, 100, 100); 
     [button1 addTarget:self       
        action:@selector(updateButtonNumber:) 
     forControlEvents:UIControlEventTouchDown]; 
     [button1 setTag:2]; 
     [self.view addSubview:button1]; 
     [self beginListenToSwipes:button1]; 

    } 

    /*every time when we tap button (begin swipe this method is called and buttonNumber always points to current button*/ 
    -(void)updateButtonNumber:(UIButton*)sender 
    { 
     self.buttonNumber.text = [NSString stringWithFormat:@"%d",sender.tag]; 
    } 

    /*method receives UIButton and add to it recognizers*/ 
    - (void) beginListenToSwipes:(UIButton*)sender 
    { 
     UISwipeGestureRecognizer *recognizer; 
     recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
     [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; 
     [sender addGestureRecognizer:recognizer]; 

     recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
     [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; 
     [sender addGestureRecognizer:recognizer]; 

     recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
     [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; 
     [sender addGestureRecognizer:recognizer]; 

     recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
     [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; 
     [sender addGestureRecognizer:recognizer]; 
    } 

    /*method which called when swipe performed and shows in which direction it was performed*/ 
    -(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer 
    { 
     int i = recognizer.direction; 
     self.swipeDirection.text = [NSString stringWithFormat:@"%d",i]; 
    } 

답변

1

,있는 UIButton에 대한 touchUpoutside 이벤트에 대한

  1. 등록.
  2. 버튼에서 손가락을 움직이면 손가락이 움직입니다.
  3. touchesEnded 메소드에서 터치 포인트를 찾아 클래스 레벨 변수에 저장합니다.
  4. 이동 방향을 찾으려면 touchupoutside 메서드에서 단추 프레임과 접촉 된 점을 비교하십시오.
+0

우리가 touchUpoutside로 등록한다면, 어떻게 uibutton에서 single press (touchUpInside)가 작동하는가? 어떤 방식 으로든 작업을 시도한 사람이 없습니다. @vignesh – Sanju

0

나는 당신이 당신의 버튼 인스턴스 변수 또는 속성을 만들 싶어, 제스처는 분명히 문제 (의 제어를 통해 발생한 경우도보고 다음 self.view에 제스처 인식기를 넣어 수 있다고 생각하여 viewcontroller 그래서 당신은 어떤 것입니다 추적 할 수 있습니다), 예

CGPoint point = [sender locationInView:self.view]; 
if (CGRectContainsPoint(button1.frame, point)) 
    // do whatever you want 

그것은 당신이 UIGestureRecognizerStateBegan 또는 UIGestureRecognizerStateEnded 또는 둘 모두에서이 논리를 할 수 있는지에 최대 당신입니다.

업데이트 : 그런데

, 당신은 당신이 뭔가를 이동하고 방향을 감지하는 경우, 당신은 또한 하나의 제스처 인식기에서 사방을 처리하는 (그리고이기 때문에 UIPanGestureRecognizer를 사용하여 찬찬히 수 있습니다 연속적으로 UI 애니메이션에서 사용자의 실시간 피드백을 제공 할 수 있습니다). 내 생각 일 것