我有一个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
中的某些内容,我该如何撰写此内容?
我是否需要将textureView
和controlView
设置为setUserInteractionEnabled:TRUE
?
谢谢!
答案 0 :(得分:6)
其他方法:
gestureStartPoint = [touch locationInView:self.view];
if(CGRectContainsPoint(textureView.frame, gestureStartPoint)){
...
}
if(CGRectContainsPoint(objectView.frame, gestureStartPoint)){
...
}
答案 1 :(得分: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
}
}
答案 2 :(得分:1)
是的,您需要这样做,如果您想要检测需要setUserInteractionEnabled:TRUE的任何视图的触摸,或者在IB中启用(如果您要从IB添加视图)。
答案 3 :(得分:1)
您可以尝试单身人士方法
例如,你将UIImageView作为TextureView中的子视图
创建一个类例如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"];
它对我有用:)希望它可以帮助某人:)