如何识别特定视图中的触摸

发布于 2024-10-20 10:21:15 字数 165 浏览 2 评论 0原文

如何接触特定视图。

我正在使用

CGPoint Location = [[touches anyObject] locationInView:self.view ];

,但只想在单击特定子视图时触发该操作。
如何做到这一点。

How to get touch on a particular view.

I am using

CGPoint Location = [[touches anyObject] locationInView:self.view ];

but want to trigger the action only if an particular subView is clicked.
How to do this.

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

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

发布评论

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

评论(6

趁年轻赶紧闹 2024-10-27 10:21:15

我自己得到了答案......但感谢其他人在这方面帮助我,

这是

UITouch *touch ;
touch = [[event allTouches] anyObject];


    if ([touch view] == necessarySubView)
{
//Do what ever you want
}

I got the answer myself...but thanks other which helped me on this

here it is

UITouch *touch ;
touch = [[event allTouches] anyObject];


    if ([touch view] == necessarySubView)
{
//Do what ever you want
}
有深☉意 2024-10-27 10:21:15

试试这个

//here enable the touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(yoursubview_Name.frame, touchLocation)) {
        //Your logic
        NSLog(@" touched");
    }
}

Try this

//here enable the touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(yoursubview_Name.frame, touchLocation)) {
        //Your logic
        NSLog(@" touched");
    }
}
错々过的事 2024-10-27 10:21:15

您应该创建 UIView 的子类(或创建类别)并覆盖

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

将消息重定向到适当的委托的位置。

You should create a subclass (or create a category) of UIView and override the

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

where redirect the message to the appropriate delegate.

沦落红尘 2024-10-27 10:21:15
// here Tiles is a separate class inherited from UIView Class
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([[touch view] isKindOfClass:[Tiles class]]) {
        NSLog(@"[touch view].tag = %d", [touch view].tag);
    }
}

像这样你可以发现视图或子视图被触摸

// here Tiles is a separate class inherited from UIView Class
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([[touch view] isKindOfClass:[Tiles class]]) {
        NSLog(@"[touch view].tag = %d", [touch view].tag);
    }
}

like this you can find view or subview is touched

电影里的梦 2024-10-27 10:21:15

这是一个不带多点触控的 swift3 版本:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first, self.bounds.contains(touch.location(in: self)) {
        // Your code
    }
}

Heres a swift3 version without multitouch:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first, self.bounds.contains(touch.location(in: self)) {
        // Your code
    }
}
不弃不离 2024-10-27 10:21:15

你尝试过吗

CGPoint Location = [[touches anyObject] locationInView:necessarySubView ];

Did u try

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