如何检测子视图中的触摸事件或当我们触摸子视图时如何使父视图被触摸?

发布于 2024-11-27 16:47:05 字数 181 浏览 1 评论 0原文

我有 2 个 UIView。

第一个是父视图,

第二个是子视图,

我们如何检测子视图何时被触摸?

或者我想在用户触摸子视图时使父视图被触摸,任何代码可以帮助我做到这一点?可以这样做吗?

因为我有一个 Something Function,当其中一个被触摸时它就会调用。

I have 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剩余の解释 2024-12-04 16:47:05

这对我有用:(

链接 xib 或 Storyboard 中的子视图)

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

This worked for me:

(Link the subview in xib or storyboard)

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
潇烟暮雨 2024-12-04 16:47:05

问题已经解决了。我认为有人给出了很好的答案,但我忘记了如何回答。

这就是我所做的。 XIB中有一个选项。有一个复选框(标题为“用户交互已启用”)指定子视图是否处理用户事件。

取消选中该复选框,对子视图的所有触摸都会转到父视图或其后面的任何其他视图。

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.

Uncheck that checkboxes and all touch to the subview goes to the parent view or whatever other view behind it.

秋意浓 2024-12-04 16:47:05

要检测触摸事件,您需要在子视图或超级视图(您想要触摸的位置)中添加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] );
}

希望它能给您带来想法。

to detect touch event you need to add UITapGestureRecognizer in your sub view or super view (in which you want get touch).

- (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

- (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..

嘿咻 2024-12-04 16:47:05

这可能有帮助:

UITouch *touch = [event.allTouches anyObject];
CGPoint touchPoint = [touch locationInView:YOUR VIEW NAME];

This might help:

UITouch *touch = [event.allTouches anyObject];
CGPoint touchPoint = [touch locationInView:YOUR VIEW NAME];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文