【发布时间】:2011-11-21 19:01:57
【问题描述】:
我想要的是一个UIView,里面有很多UIButtons。它们根据存储在NSArray 中的数据进行放置和排列。因为有很多按钮,它们不能一次全部放在屏幕上。用户应该能够缩小以查看所有按钮或放大以查看详细信息(按钮上的标签)并轻松选择它们。
我尝试了两种不同的方法:
1) 我构建了一个UIView 子类,将按钮放入其中,并将此视图的实例放入UIScrollview。
效果:我可以通过它们的标签访问所有按钮,并且滚动和缩放工作正常。但我无法获得处理任何事件的按钮(按下它们)...
2) 我写了一个具有完全相同功能的UIViewController,并将它的一个实例添加到UIScrollView。
效果:我现在可以按下按钮了,但是滚动和缩放已经停止工作了。
这里是视图的相关代码:
- (UIView *)initWithArray:(NSArray *)nArray{
self = [super init];
if (self) {
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:(__some Code to place the Button__);
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
}
}
return self;
}
以及 matrixController 的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *nArray = [[NSMutableArray alloc] __some code___];
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:CGRectMake(__some Code to place the Button__];
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
}
还有ScrollViewController的代码:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 970)];
[self.view addSubview:scrollView];
[scrollView setBackgroundColor:[UIColor blackColor]];
//Zooming
[scrollView setMinimumZoomScale:0.25];
[scrollView setMaximumZoomScale:4.0];
[scrollView setDelegate:self];
// constructing the view
[scrollView addSubview:chartView];
[scrollView bringSubviewToFront:chartView];
或
[scrollView addSubview:[matrixController view]];
我怎样才能让它工作??
【问题讨论】:
-
滚动视图正在拦截触屏事件。方法 1) 应该有效,您应该为按钮选择 touch up inside 事件。
-
即使触地事件不是他想要的,它仍然应该触发该事件。
标签: ios cocoa-touch uiview uiscrollview uibutton