2013-03-26 130 views
1

我有兩個自定義的uibuttons。ios觸摸從一個按鈕移動到另一個按鈕

@interface myButton : UIButton 

我重寫幾個方法,如

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // do something 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 
    [self.nextResponder touchesMoved:touches withEvent:event]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchPoint = [touch locationInView:self]; 

    if (!CGRectContainsPoint(self.bounds, touchPoint)) { 
     [self touchesCancelled:touches withEvent:event]; 
    } 
    return; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // do something 
} 

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

我要的是,當我觸摸一個按鈕,我可以保持我的觸摸移動到另一個按鈕的事情。當我觸摸第一個按鈕的界限之外時,我厭倦了調用touchesCancelled。所以我「想」然後我移動到另一個按鈕,這將是一個新的觸摸事件。但它不這樣工作。

我做錯了什麼?或者touchesCancelled方法不是用於這樣的? 在此先感謝!

+0

我相信'當你移動你的手指從第一個按鈕的面積touchesCanceled'已經被調用。它不會在第二個按鈕上調用touchesBegan,因爲您沒有再次放下手指。爲什麼不使用手勢識別器呢? – 2013-03-26 16:01:42

+0

您正在將事件從一個按鈕傳遞到另一個按鈕。一個按鈕上的事件位置與其他按鈕不匹配,因此顯然與您的代碼一樣,甚至會在其他按鈕中取消。 – nkongara 2013-03-26 17:15:02

回答

2

您的暫停是正確的,但這不是打算如何使用touchesCancelled:withEvent:。從文檔:

當Cocoa Touch框架收到需要取消觸摸事件的系統中斷時調用此方法;爲此,它生成一個UITouchPhaseCancel階段的UITouch對象。中斷是可能導致應用程序不再處於活動狀態或將視圖從窗口中刪除的事情。

如果您的用戶收到來電,短信或他們的鬧鐘響起等,您的響應者將收到觸摸取消的事件。它應該用於清除在其他觸摸事件中建立的任何狀態信息。

看起來好像您要更改與觸摸事件中間觸摸相關聯的響應者,即當您從第一個按鈕的邊界拖動觸摸並輸入第二個按鈕時,它應該成爲接受觸摸的響應者事件。不幸的是,這不是響應者設計工作的方式。一旦UIView被識別爲響應者,如在hitTest:withEvent:中返回的那樣,這將是UIView來接收觸摸事件。

一個可能的鍛鍊,以實現你想要的是在包含兩個按鈕的超級視圖中處理觸摸事件(touchesBegan:withEvent:,touchesMoved:withEvent:等)。那麼你的上海華將接收觸摸事件,你可以根據這些按鈕的框架之內可以上採取行動:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) 
    { 
     CGPoint touchLocation = [touch locationInView:self.view]; 

     UIButton *button; 
     if (CGRectContainsPoint(self.button1.frame, touchLocation)) 
     { 
      button = self.button1; 
      NSLog(@"touchesMoved: First Button"); 
     } 
     else if (CGRectContainsPoint(self.button2.frame, touchLocation)) 
     { 
      button = self.button2; 
      NSLog(@"touchesMoved: Second Button"); 
     } 

     // Do something with button... 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) 
    { 
     CGPoint touchLocation = [touch locationInView:self.view]; 

     UIButton *button; 
     if (CGRectContainsPoint(self.button1.frame, touchLocation)) 
     { 
      button = self.button1; 
      NSLog(@"touchesMoved: First Button"); 
     } 
     else if (CGRectContainsPoint(self.button2.frame, touchLocation)) 
     { 
      button = self.button2; 
      NSLog(@"touchesMoved: Second Button"); 
     } 

     // Do something with button... 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) 
    { 
     CGPoint touchLocation = [touch locationInView:self.view]; 

     UIButton *button; 
     if (CGRectContainsPoint(self.button1.frame, touchLocation)) 
     { 
      button = self.button1; 
      NSLog(@"touchesEnded: First Button"); 
     } 
     else if (CGRectContainsPoint(self.button2.frame, touchLocation)) 
     { 
      button = self.button2; 
      NSLog(@"touchesEnded: Second Button"); 
     } 

     // Do something with button... 
    } 
} 
相關問題