的我要的是一个UIView
有很多在它UIButtons
。他们根据存储在NSArray
中的数据进行放置和排列。因为有很多按钮,它们一次不能放在屏幕上。用户应该能够缩小以查看所有按钮或放大以查看详细信息(标签)并轻松选择它们。的UIScrollView有大量UIButtons
我尝试两种不同的方法:
1)I构成的子类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];
OR
[scrollView addSubview:[matrixController view]];
我怎样才能得到这个工作?
滚动视图被拦截触地事件。方法1)应该已经工作了,您应该选择触摸按钮的内部事件。 – Tom
即使触碰事件不是他想要的,它仍然应该触发事件。 – LucasTizma