【发布时间】:2011-10-19 11:10:01
【问题描述】:
我打开了 30-40 个标签,我找到了答案。但我希望改进。 该代码确实有效,我只是不确定它是否有效
以下代码显示我有 2 个小图像(一个使用 UIImageView,另一个使用 UIView),其中我已附加到 UIScrollView,该 UIScrollView 附加到 UIViewController 附带的 UIView,该 UIViewController 附加到 UIWindow。很基本。
我还在 UIViewController 上附加了一个按钮,以确保缩放和滚动正常工作。 (通过测试按钮的静态位置和大小)
@interface Wire_TestViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollview;
}
@end
实施:
- (void)test:(UIButton *)sender { }
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return scrollview; }
- (void)viewDidLoad {
[super viewDidLoad];
scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollview.delegate = self;
scrollview.contentSize = CGSizeMake( 320*2, 480*2 );
scrollview.minimumZoomScale = .1;
scrollview.maximumZoomScale = 10;
[self.view addSubview:scrollview];
UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(320, 320, 20, 20)];
view.image = [UIImage imageNamed:@"Node.png"];
view.clearsContextBeforeDrawing = NO;
[scrollview addSubview:view];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 10, 100, 30);
button.tag = 51;
[button setTitle:@"new button" forState:(UIControlState)UIControlStateNormal];
[button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 30, 30)];
view2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"WireActive.png"]];
[scrollview addSubview:view2];
TestSubclass *test = [[TestSubclass alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
test.userInteractionEnabled = TRUE;
test.image = [UIImage imageNamed:@"Node.png"];
[scrollview addSubview:test];
}
您会在底部附近看到我制作了一个 TestSubclass 类。 代码如下:
@interface TestSubclass : UIImageView { }
@end
@implementation TestSubclass
- (id)init {
[super init];
self.userInteractionEnabled = TRUE;
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
printf("Being touched...\n");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
printf("Touches Ended...\n");
}
@end
令我满意的部分是我有一个功能齐全的 UIImageView 子类,它就像一个 UIButton 一样工作。 但!没有 7 个 backgroundImageViews(以及每个标签的开销),没有 6 个标签(以及每个标签的开销),没有 6 个 UIImageViews(“...”),最后是不需要的东西的数量UIButton 提供。我只想要触摸功能..但是有效。 (TouchUpInside、拖动等)
但是,现在在我的研究过程中(经过大量测试以完成这项工作),我在文档和一些示例中遇到了 UIView 中的 addGestureRecognizer。这似乎有点——但对我的需求并不完全有效。
我正在寻求建议,我知道我的代码中有几个问题(重写了 init,但没有在上面的代码中使用它.. 没有发布.. 可能是一些子类化错误(子类化的新手(说实话))。但是任何事情都非常感谢!!而且,我希望其他人也能得到帮助!
【问题讨论】:
标签: iphone objective-c c uiimageview touch