2011-11-17 67 views
0

我有一個子視圖網格的超級視圖。當我拖出/移出子視圖時,我想改變它的屬性(類似iPhone上的UIKeyboard)。子視圖是UIButton子類。iOS - 將手指拖動到網格視圖上

我在想我需要從superview做一些觸摸轉發,但我不清楚它是如何工作的。這些方法的正確組合是什麼?

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

hitTest:withEvent:

回答

1

我認爲你可以使用:

– touchesBegan:withEvent: 
– touchesMoved:withEvent: 
– touchesEnded:withEvent: 
– touchesCancelled:withEvent: 

你的上海華。

一種可能的方法是在touchesMoved中識別哪個子視圖當前「處於觸摸之下」(即觸摸位置在哪)並相應地改變其狀態。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 
    if(CGRectContainsPoint(subview1.frame, location)) { 
     ... 
    } 
} 

touchesBegantouchesEnded不會在其中發揮了重要作用;他們只會有用於開始和結束您在touchesMoved中所做的「跟蹤」。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSSet *allTouches = [event allTouches]; 
    <save initial touch if you need it> 
} 


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    <do whatever> 
} 
+0

感謝您的回覆。 hitTest會適合從touchesMoved獲取子視圖嗎? – jimj

+0

當然可以;儘管hitTest橫跨了所有的子視圖層次結構。如果你有任何形式的你的子視圖的規則佈局,你可能會更好的某種數學,給你受影響的子視圖... – sergio