2011-03-04 41 views

回答

8

我得到了自己的答案......但由於其他這讓我對這個

這裏是

UITouch *touch ; 
touch = [[event allTouches] anyObject]; 


    if ([touch view] == necessarySubView) 
{ 
//Do what ever you want 
} 
1

你應該創建一個子類(或創建一個類別)的UIView和覆蓋

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

其中重定向消息到相應的委託。

+0

不,你不必這樣做...檢查我自己的答案,我發現僅幾分鐘以前... – 2011-03-04 11:13:27

+0

看來我錯誤地理解了你的問題。 – Max 2011-03-04 11:15:12

+0

+1 ..良好的替代,如果大的工作要在子視圖上完成,謝謝 – 2011-03-04 11:19:16

0

難道ü嘗試

CGPoint Location = [[touches anyObject] locationInView:necessarySubView ]; 
+0

沒有這給位置在一個視圖,而不是被感動的視圖.... – 2011-03-04 11:10:17

7

試試這個

//here enable the touch 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    // get touch event 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    if (CGRectContainsPoint(yoursubview_Name.frame, touchLocation)) { 
     //Your logic 
     NSLog(@" touched"); 
    } 
} 
+0

+1好答案,但也請檢查我的一個。 ... – 2011-03-04 11:16:28

+0

雅..太棒了。+ 1。 – Sat 2011-03-04 11:25:39

1
// here Tiles is a separate class inherited from UIView Class 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    if ([[touch view] isKindOfClass:[Tiles class]]) { 
     NSLog(@"[touch view].tag = %d", [touch view].tag); 
    } 
} 

這樣你可以找到視圖或子視圖觸摸

1

繼承人swift3版本,而多點觸控:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first, self.bounds.contains(touch.location(in: self)) { 
     // Your code 
    } 
} 
相關問題