2012-05-03 5 views
1

スワイプを実行する方向を認識し、ビューのどのオブジェクトがそれを送信したかを知る必要があります。私は今、回避策を見つけましたが、複雑で怪物に見えますので、最も単純な解決方法はありますか? 私の回避策はいくつかの言葉で:UIButtonオブジェクトと一緒にスワイプ方向を認識する

  1. 私の視点のすべてのUIButtonオブジェクトに添付された認識器。
  2. スワイプの実行中にタップされたボタンを示すインジケータがあります。
  3. このインジケータとスワイプ方向を確認して正しい決定をするために、どのオブジェクトをどの方向に移動する必要があるかを確認する必要があります。

アドバイスありがとうございました。

私はあなたにもself.viewであなたのジェスチャー認識を入れ、その後、ジェスチャーは、あなたのあなたのボタンアイバーズまたはプロパティを作りたいと思い、明らかに(問題の制御を介して行われたかどうかを見ることができると思う
 
@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

どのようなものかを追跡することができます)

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

それはあなたがUIGestureRecognizerStateBeganまたはUIGestureRecognizerStateEndedまたはその両方でこのロジックを実行するかどうかについてはあなた次第です。

更新:ところで

、あなたが何かを移動しているどの方向を検出している場合、あなたはまた、単一のジェスチャ認識装置内のすべての4つの方向を扱う(そして、それはだからUIPanGestureRecognizerを、使用して考えることができます継続的に、あなたはユーザーの動きをアニメートするUIでリアルタイムのフィードバックを提供することができます)。

関連する問題