我有2个UIView。如何检测子视图中的事件触摸或如何在触摸子视图时触及父视图?
第一个是父视图
第二个是一个子视图,
我们如何能够检测何时子视图很感动?
或者我想让父视图在用户触摸子视图时被触动,任何代码都可以帮助我做到这一点?是否有可能做到这一点?
因为我有一个功能,当它们中的一个被触摸时,它会调用。
我有2个UIView。如何检测子视图中的事件触摸或如何在触摸子视图时触及父视图?
第一个是父视图
第二个是一个子视图,
我们如何能够检测何时子视图很感动?
或者我想让父视图在用户触摸子视图时被触动,任何代码都可以帮助我做到这一点?是否有可能做到这一点?
因为我有一个功能,当它们中的一个被触摸时,它会调用。
的问题已经解决。我认为有人给出了很好的答案,但我忘记了。
这就是我所做的。 XIB中有一个选项。有一个复选框(标题为“启用用户交互”),指定子视图是否处理用户事件。
取消选中该复选框和所有触摸到子视图转到父视图或任何其他视图。
这可能帮助:
UITouch *touch = [event.allTouches anyObject];
CGPoint touchPoint = [touch locationInView:YOUR VIEW NAME];
检测到您需要在您的子画面或超视图(要在其中获得触摸)添加UITapGestureRecognizer
触摸事件。
- (void)viewDidLoad
{
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[super viewDidLoad];
}
的UITapGestureRecognizer
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
// here you can get your touch
NSLog(@"Touched view %@",[touch.view class]);
}
那么你可以添加委托方法希望它让你的想法..
这为我工作:
(链路子视图在厦门国际银行或故事板)
ViewController.h
@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIView *subview;
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
@end
ViewController.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
在handleTap函数中,我添加了以下行来隐藏键盘。 [self.view endEditing:YES]; –
那个复选框的名字是什么? – MartinMoizard
我不再使用iPhone的代码,我忘记了。我认为这是UIControl和UIView的主要区别。 UIControl默认打开了这个复选框。 –