从自定义 UIImageView 子类通知 ViewController

发布于 2024-10-15 13:39:58 字数 1262 浏览 3 评论 0原文

我想定义一个可拖动图像的类,子类化 UIImageView,分离它们的外观(在子类上)以及用户界面对它们移动的位置(在视图控制器上)的反应

myType1Image.h

@interface myType1Image : UIImageView { } 

myType1Image.m

...
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
// Retrieve the initial touch point 
} 
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
// Move relative to the original touch point with some special effect
} 
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { 
// Notify the ViewController ... here is my problem...how?
// would call GotIt on viewController for example
}
...

viewcontroller 实现

....
  myType1Image *img = [[myType1Image alloc] initWithImage:[UIImage imageNamed:@"iOSDevTips.png"]];
  img.center = CGPointMake(110, 75);
  img.userInteractionEnabled = YES;
  [subview addSubview:img];
...

- (void) gotIt:(id) (myType1Image *)sender{
    if (CGRectContainsPoint( myimage.frame, [sender.center] )){
        NSLog(@"Got IT!!!");
    }
}

....

我无法弄清楚如何从 myType1Image 类通知 ViewController,用于 TouchEnded(例如)。 我在视图控制器上编写了所有代码,但我想使用子类来完成它,这样我就可以将事件处理和图像的可视化与界面的实际功能分开。 因此,如果我有 15 个可拖动图像,我不必猜测哪个图像正在触摸,也不必决定要应用的视觉效果。

是否可以?是错误的方法吗?

I want to define a class of draggable images subclassing UIImageView, separating how they look (on the subclass) and what the user interface react to where they move (on the viewcontroller)

myType1Image.h

@interface myType1Image : UIImageView { } 

myType1Image.m

...
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
// Retrieve the initial touch point 
} 
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
// Move relative to the original touch point with some special effect
} 
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { 
// Notify the ViewController ... here is my problem...how?
// would call GotIt on viewController for example
}
...

viewcontroller implementation

....
  myType1Image *img = [[myType1Image alloc] initWithImage:[UIImage imageNamed:@"iOSDevTips.png"]];
  img.center = CGPointMake(110, 75);
  img.userInteractionEnabled = YES;
  [subview addSubview:img];
...

- (void) gotIt:(id) (myType1Image *)sender{
    if (CGRectContainsPoint( myimage.frame, [sender.center] )){
        NSLog(@"Got IT!!!");
    }
}

....

I can't figure out how ViewController could be notified from the myType1Image class, for touchesEnded (for example).
I did it writing all code on the viewcontroller, but I want to do it using subclasses so I can separate the event handling and the visualization of the images from the real funcionality of my interface.
So if I have 15 draggable images, I haven't to guess what image is touching, and deciding the visual effects to apply.

Is it possible? Is an erroneus aproach?

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

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

发布评论

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

评论(1

早茶月光 2024-10-22 13:39:58

首先,类名应始终以大写字母开头 (MyType1Image)。

创建一个 MyType1ImageDelegate 协议,在其中声明图像视图发送到其委托(视图控制器)的委托方法。这些方法的第一个参数应始终为 MyType1Image * 类型(以告诉委托消息来自哪个对象)。

您的 MyType1Image 还需要一个 id委托 属性。

当视图控制器创建图像视图时,它将自身设置为图像视图的委托。每当图像视图想要向视图控制器发送消息时,您就可以将消息发送给委托。

First, class names should always begin with a capital letter (MyType1Image).

Create a MyType1ImageDelegate protocol in which you declare delegate methods that the image view sends to its delegate (the view controller). The first argument of these methods should always be of the type MyType1Image * (to tell the delegate from which object the message comes).

Your MyType1Image also needs a id <MyType1ImageDelegate> delegate property.

When your view controller creates an image view, it sets itself as the image view's delegate. Whenever the image view wants to send a message to the view controller, you have it send the message to the delegate.

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