2011-06-27 142 views
9

我有一個Viewcontroller兩個UIViews,一個名爲textureView和一個objectView檢測子視圖上的觸摸

當我只有一個觀點我使用這個檢測觸摸:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSArray *allTouches = [touches allObjects]; 

    for (UITouch *touch in allTouches) 
    { 
     gestureStartPoint = [touch locationInView:self.view]; 
      if([touch view] == controlUIImageview){ 
        NSLog(@"TOUCH DETECT"); 
       } 
     } 
} 

我怎樣寫這部分,如果我有一些事情我想在textureView有的在objectView與互動?

是否需要將textureViewcontrolView設置爲setUserInteractionEnabled:TRUE

謝謝!

+0

你有沒有嘗試設置textureView和對照olView to setUserInteractionEnabled:TRUE?當你嘗試時發生了什麼? –

回答

1

是的,如果你想檢測你需要的任何視圖的觸摸,你需要setUserInteractionEnabled:TRUE或者在IB中啓用(如果你是從IB添加視圖的話)。

3

嘗試添加您要添加​​的每個子視圖一個tag ....這樣的:

textureView.tag=0; 
objectView.tag=1; 

則:

for (UITouch *touch in allTouches) 
    { 
    if(touch.view.tag==0){ 
     //...do your stuff with textureView 
    }else{ 
     //...do your stuff with objectView 
    } 
} 
+0

是的,它可以檢測不同的視圖,但是如何檢測特定uiview中uiimageview的觸摸? – molle

6

其他方式做到這一點:

 gestureStartPoint = [touch locationInView:self.view]; 
     if(CGRectContainsPoint(textureView.frame, gestureStartPoint)){ 
      ... 
     } 
     if(CGRectContainsPoint(objectView.frame, gestureStartPoint)){ 
      ... 
     } 
+0

是的,這也適用,但如何檢測特定uiview中uiimageview的觸摸?因爲當uiimageview處於子視圖時,[touch view] == uiimageview不適用於我。 – molle

+0

它肯定是扭曲的,但你可以使用像這樣的東西:'CGRect rect1 = [self.view convertRect:textureView.controlUIImageview.frame toView:self.view];如果(CGRectContainsPoint(rect1,gestureStartPoint))...' – aknew

1

你可以試試單身方法

例如,你有作爲的UIImageView子視圖中TextureView

使類e.g object.h並聲明要與E.g

UIImageView *DisplayPicture 
UIView *View; 

一旦多數民衆贊成進行交互的子視圖。您可以爲每個的兩種觀點

TextureView.tag=0; 
Objectview.tag=1; 

的設置標籤不是用這種方法來獲得的查看和比子視圖

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

UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView:touch.view]; 


if (touch.view.tag > 0) { 


    NSLog(@"%d",[touch.view.subviews count]); 

    //Get Reference for Texture view 

    object.View=Touch.view; 

    //the index will the position at which u added the Subview(UIImageView) e.g If its the first time you addsubview and index of the subview will be O. 

    object.DipslayPicture=[touch.view.subviews objectAtIndex:0]; 

}else{ 
    //Similar for ObjectView 

} 
NSLog(@"tag=%@", [NSString stringWithFormat:@"%i", touch.view.tag]); 
i=touch.view.tag; 
} 

觸摸比你可以使用該對象做你喜歡如SetImage

[object.DisplayPicture setImage:@"happiness.png"]; 

它爲我工作:)希望它可以幫助別人:)