2011-06-27 107 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"]; 

它为我工作:)希望它可以帮助别人:)