iPhone SDK:检测被点击的视图

发布于 2024-10-18 14:39:28 字数 112 浏览 5 评论 0原文

在我的应用程序中,我有很多小的 UIImageView 和侧面的视图。我需要的是侧面的视图,以便能够检测到哪个图像视图被点击,并为我提供有关它的所有信息(很像 (id)sender)及其坐标。最好的方法是什么?

In my application I have a lot of little UIImageViews and a view off to the side. What I need is that view off to the side to be able to detect which image view was tapped and give me all the information about it (much like with (id)sender) as well as its coordinates. What is the best way to do this?

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

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

发布评论

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

评论(3

仅此而已 2024-10-25 14:39:29

这是我的项目的内部类。我想你明白了。
作为一个选项,您可以为所有者(或委托人)创建协议。
获取坐标:

-[UITouch locationInView: someView]

您可以使用以下代码

@interface _FaceSelectView : UIImageView {
@protected
    VIFaceSelectVC* _owner;
}
-(id) initWithOwner:(FaceSelectVC*) owner;
@end

@implementation _FaceSelectView

-(id) initWithOwner:(FaceSelectVC*) owner {
    if( self = [super init] ) {
        _owner = owner;
        self.userInteractionEnabled = YES;
    }

    return self;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_owner _touchesBeganIn: self withTouch: (UITouch*)[touches anyObject]];
}

-(void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event {
    [_owner _touchesEndedIn: self withTouch: (UITouch*)[touches anyObject]];
}

-(void) touchesMoved:(NSSet*) touches withEvent:(UIEvent*) event {
    [_owner _touchesMovedIn: self withTouch: (UITouch*)[touches anyObject]];
}

@end

This is the internal class from my project. I think you get the idea.
As an option you can create protocol for the owner (or delegate).
You can obtain coords using

-[UITouch locationInView: someView]

Here is the code:

@interface _FaceSelectView : UIImageView {
@protected
    VIFaceSelectVC* _owner;
}
-(id) initWithOwner:(FaceSelectVC*) owner;
@end

@implementation _FaceSelectView

-(id) initWithOwner:(FaceSelectVC*) owner {
    if( self = [super init] ) {
        _owner = owner;
        self.userInteractionEnabled = YES;
    }

    return self;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_owner _touchesBeganIn: self withTouch: (UITouch*)[touches anyObject]];
}

-(void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event {
    [_owner _touchesEndedIn: self withTouch: (UITouch*)[touches anyObject]];
}

-(void) touchesMoved:(NSSet*) touches withEvent:(UIEvent*) event {
    [_owner _touchesMovedIn: self withTouch: (UITouch*)[touches anyObject]];
}

@end
时光无声 2024-10-25 14:39:29

我将继承 UIImageView 并为您想要发送消息的目标视图添加一个新属性。然后重新启用userInteractionEnabled并向touchesUpInside添加一个操作。

在自定义子类的操作方法中调用目标视图上的方法,您还可以在其中提供自定义子视图的对象。基本上,您使用委托并传入委托调用您需要的所有参数。

I would subclass UIImageView and add a new property for the target view you would like to message. Then reenable userInteractionEnabled and add a action to touchesUpInside.

In the action method of the custom subclass call a method on the target view in which you also give the object of the custom subview. Basically you use delegation and pass in the delegate call all parameters you need.

谎言 2024-10-25 14:39:29

将标签添加到 UIImageView,如 myUIImageView.tag = intNumber;
在 side TouchsBegan 中,或者您可以为所有 UIImageView 调用任何通用方法,并使用标签来识别哪个视图被点击。

Add tag to UIImageView like myUIImageView.tag = intNumber;
and in side touchesBegan or you can call any common method for all your UIImageView and use tag to identify which view is tapped.

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