xcode 如何检测子视图中的事件触摸或如何在触摸子视图时触摸父视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6907669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to detect event touch in subview or how to make parent view was touched when we touch subview?
提问by user4951
I have 2 UIView.
我有 2 个 UIView。
the first one is a parent view
第一个是父视图
the second one is a subview,
第二个是子视图,
how can we detect when a subview was touched?
我们如何检测子视图何时被触摸?
or I want to make parent view was touched when user touch subview, any code can help me to do it? is it possible to do this?
或者我想让父视图在用户触摸子视图时被触摸,任何代码都可以帮助我做到这一点?是否有可能做到这一点?
because I have a Something Function, that will call when one of them was touched.
因为我有一个东西函数,当其中一个被触摸时会调用。
回答by justinkoh
This worked for me:
这对我有用:
(Link the subview in xib or storyboard)
(链接xib或storyboard中的子视图)
ViewController.h
视图控制器.h
@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIView *subview;
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
@end
ViewController.m
视图控制器.m
@implementation ViewController
@synthesize subview;
@synthesize tapRecognizer;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
[subview addGestureRecognizer:tapRecognizer];
}
- (IBAction)handleTap:(UITapGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateEnded){
//code here
NSLog(@"subview touched");
}
}
@end
回答by user4951
The problem has been solved. I think someone gave a great answer but I forget how.
问题已经解决。我认为有人给出了很好的答案,但我忘记了答案。
This is what I did. There is an option in XIB. There is a checkbox (titled "User interaction enabled") that specifies whether a subview handle user event or not.
这就是我所做的。XIB 中有一个选项。有一个复选框(标题为“启用用户交互”)指定子视图是否处理用户事件。
Uncheck that checkboxes and all touch to the subview goes to the parent view or whatever other view behind it.
取消选中复选框,所有对子视图的触摸都会转到父视图或它后面的任何其他视图。
回答by Maulik
to detect touch event you need to add UITapGestureRecognizer
in your sub view or super view (in which you want get touch).
要检测触摸事件,您需要UITapGestureRecognizer
在子视图或超级视图中添加(您希望在其中进行触摸)。
- (void)viewDidLoad
{
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[super viewDidLoad];
}
then you can add delegate methods of UITapGestureRecognizer
然后你可以添加委托方法 UITapGestureRecognizer
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
// here you can get your touch
NSLog(@"Touched view %@",[touch.view class] );
}
hope it gives you idea..
希望它给你想法..
回答by Muzammil
This might help:
这可能有帮助:
UITouch *touch = [event.allTouches anyObject];
CGPoint touchPoint = [touch locationInView:YOUR VIEW NAME];