2010-08-07 153 views
0

我有一個很大的問題,因爲最近2天。我正在使用支持多點觸控的視圖。我的UIViewController有8-10 imageview。我想單獨檢測每個視圖的觸摸,但一次檢測多個視圖。在所有圖像視圖上檢測到觸摸,但問題在這裏 -第二個UITouch事件觸發器第一個UITouch事件爲什麼?

假設我點擊圖像視圖並按住此圖像視圖,然後點擊另一個圖像視圖,第二個圖像視圖被檢測到觸摸成功,但它也觸發先前觸摸並按住的第一個圖像視圖。但我不想要它。所以請任何人幫助我。代碼級別的幫助表示讚賞。

注意:我的UIViewController也實現了用於滑動目的的TouchesMoved方法。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for(UITouch *touch in event.allTouches) { 
     if(CGRectContainsPoint([imView1 frame], [touch locationInView:self.view])){ 
      NSLog(@"imView 1 touched"); 
     } 
     if(CGRectContainsPoint([imView2 frame], [touch locationInView:self.view])){ 
      NSLog(@"imView 2 touched"); 
     } 
    } 
} 


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for(UITouch *touch in event.allTouches) { 
     if(CGRectContainsPoint([imView1 frame], [touch locationInView:self.view])){ 
      NSLog(@"imView 1 touch moved"); 
     } 
     if(CGRectContainsPoint([imView2 frame], [touch locationInView:self.view])){ 
      NSLog(@"imView 2 touch moved"); 
     } 
    } 
} 

回答

0

嘗試繼承UIImageView並處理該子類中的觸摸代碼。如果您出於某種原因需要將觸摸傳遞給父級,則可以處理該觸摸,然後將其發送給父級,這將是您的視圖控制器。

編輯:

有你的自定義類這樣

@interface CustomImageView : UIImageView {}@end 
@implementation CustomImageView 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"touch began %d",self.tag); 
    [self.nextResponder touchesBegan:touches withEvent:event]; 
} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"touch moved %d",self.tag); 
    [self.nextResponder touchesMoved:touches withEvent:event]; 
} 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"touch ended %d",self.tag); 
    [self.nextResponder touchesEnded:touches withEvent:event]; 
} 
@end 

,有你的parrent代碼是這樣的

@implementation trashmeTouchIpadViewController 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"parent Touch Began"); 
     //do parent stuff 
} 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"parent Touch ended"); 
     //do parent stuff 
} 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.view setMultipleTouchEnabled:YES]; 
    UIImage *i1 = [UIImage imageNamed:@"img1.jpg"]; 
    CustomImageView *v1 = [[CustomImageView alloc] initWithImage:i1]; 
    v1.frame = CGRectMake(100, 100, 200, 200); 
    v1.userInteractionEnabled = YES; 
    v1.multipleTouchEnabled = YES; 
    v1.tag = 111; 
    [self.view addSubview:v1]; 
    [v1 release]; 
    UIImage *i2 = [UIImage imageNamed:@"img2.jpg"]; 
    CustomImageView *v2 = [[CustomImageView alloc] initWithImage:i2]; 
    v2.frame = CGRectMake(500, 100, 200, 200); 
    v2.userInteractionEnabled = YES; 
    v2.multipleTouchEnabled = YES; 
    v2.tag = 999; 
    [self.view addSubview:v2]; 
    [v2 release]; 
} 
+0

感謝您的快速反應。你能解釋一下嗎?它將如何工作? – user413866 2010-08-07 16:15:38

+0

而不是使用UIImageView,創建一個新的NSObject和子類UIImageView,使其看起來像 @interface MyImageView:UIImageView {} @ end ,並且在實現(.m文件)中,您將擁有@implementation,然後觸摸代碼。在子類中,你可以做任何你需要做的事情,如果你需要調用父類的東西,那麼你可以做到這一點。發送觸摸到父母,你會做類似 [self.nextResponder touchesMoved:touches withEvent:event]; – AtomRiot 2010-08-07 17:29:39

+0

感謝您的建議。如果你給我一個源代碼示例,那麼它會保存我的2天。請給我一個示例項目,它有超過2個imageview,並分別檢測每個圖像視圖上的滑動,並解決了這個問題。再次感謝您的回覆。 – user413866 2010-08-09 17:08:34