开发者

iPhone:How to find the control interaction under Touch?

开发者 https://www.devze.com 2022-12-24 18:59 出处:网络
I have a UILabel control in a view. I want to detect touch event occurred when this label touched. I added the fol开发者_如何学Clowing code, which should work whenever touch happens on the view.

I have a UILabel control in a view. I want to detect touch event occurred when this label touched. I added the fol开发者_如何学Clowing code, which should work whenever touch happens on the view.

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

{

// This should be called only when the label is touched, not all the time..

[ [UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.mywebsite.com"] ];

}

Whenever that particular label is touched my code should do the process further, not when the touch is happening anywhere in the view. How do i find out the particular label (or) any control is touched under touchesEnded function ?

Could someone guide me on this?

Thank you.


You can do this by performing a hitTest on the label:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:label];
    if([label hitTest:point withEvent:nil]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.mywebsite.com"]];
    } else {
        [super touchesEnded:touches withEvent:event];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号