Cocoa touch 从自定义UIImageView子类通知ViewController
我想定义一类可拖动的图像,将UIImageView子类化,分离它们的外观(在子类上)和用户界面对它们移动的位置(在viewcontroller上)的反应 myType1Image.hCocoa touch 从自定义UIImageView子类通知ViewController,cocoa-touch,ios,uiimageview,subclass,Cocoa Touch,Ios,Uiimageview,Subclass,我想定义一类可拖动的图像,将UIImageView子类化,分离它们的外观(在子类上)和用户界面对它们移动的位置(在viewcontroller上)的反应 myType1Image.h @interface myType1Image : UIImageView { } myType1Image.m ... - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // Retrieve the initial touc
@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,例如touchesEnded。
我在viewcontroller上编写了所有代码,但我想使用子类来完成,这样我就可以将事件处理和图像的可视化与界面的实际功能分离开来。
所以,如果我有15个可拖动的图像,我就不必猜测什么图像是触动的,并决定要应用的视觉效果
可能吗?erroneus是一种错误吗?首先,类名应始终以大写字母开头(
MyType1Image
)
创建MyType1ImageDelegate
协议,在该协议中声明图像视图发送给其委托(视图控制器)的委托方法。这些方法的第一个参数的类型应始终为MyType1Image*
(用于告诉委托消息来自哪个对象)
您的MyType1Image
还需要一个id委托
属性
当视图控制器创建图像视图时,它将自身设置为图像视图的代理。每当图像视图想要向视图控制器发送消息时,您都可以让它将消息发送给代理。谢谢。代表现在对我来说很重要。我假设协议中有多个消息类型..对于我调查的@protocol MyType1ImageControllerDelegate-(void)MyType1ImageViewControllerMoved:(MyType1ImageViewController*)MyType1ImageViewController;-(void)MyType1ImageViewController引用:(MyType1ImageViewController*)MyType1ImageViewController@当然,协议可以包含任意多个方法。请记住命名约定:类和协议的名称以大写字母开头,方法名称以小写字母开头。