6

如何接触特定视图。

我在用

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

但仅在单击特定子视图时才想触发操作。
这该怎么做。

4

6 回答 6

8

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

这里是

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


    if ([touch view] == necessarySubView)
{
//Do what ever you want
}
于 2011-03-04T11:12:33.130 回答
8

试试这个

//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");
    }
}
于 2011-03-04T11:15:00.673 回答
1

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

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

在哪里将消息重定向到适当的代表。

于 2011-03-04T10:58:50.827 回答
1
// 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);
    }
}

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

于 2011-12-17T10:15:36.433 回答
1

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

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first, self.bounds.contains(touch.location(in: self)) {
        // Your code
    }
}
于 2017-01-24T15:30:09.883 回答
0

你试过了吗

CGPoint Location = [[touches anyObject] locationInView:necessarySubView ];
于 2011-03-04T11:02:13.323 回答