繁体   English   中英

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

[英]how identify touch in a particular view

如何触摸特定视图。

我在用

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

但是只有在单击特定的子视图时才想触发操作。
这个怎么做。

我自己得到了答案......但感谢其他帮助我的人

这里是

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


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

尝试这个

//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");
    }
}

这是没有多点触控的swift3版本:

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

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

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

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

// 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);
    }
}

像这样你可以找到视图或子视图被触及

你试过吗

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM