UIScrollView包含大量UIButtons

时间:2011-09-21 09:33:29

标签: ios cocoa-touch uiview uiscrollview uibutton

我想要的是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];

OR

    [scrollView addSubview:[matrixController view]];

我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:3)

我能够获得一个包含多个按钮的滚动视图,可以平移和缩放,按钮仍处理触摸事件:

- (void)didTapButton:(UIButton *)button
{
    NSLog(@"Button %ld", (long)button.tag);
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f, scrollView.frame.size.height * 3.0f);
    scrollView.maximumZoomScale = 3.0f;

    UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
    zoomView.backgroundColor = [UIColor whiteColor];

    for (NSInteger index = 0; index < 100; index++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
        button.tag = index;
        [button setTitle:[NSString stringWithFormat:@"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];

        [zoomView addSubview:button];
    }

    [scrollView addSubview:zoomView];
    [self.view addSubview:scrollView];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView.subviews objectAtIndex:0];
}
编辑:我说在下面的评论中不依赖标签,但我在这里做。 :)这仅仅是因为我可以记录按钮编号,因此应该忽略该部分。

答案 1 :(得分:0)

for (int i=0; i<10; i++)
{
    UIButton  *scrollingbutton_outlet = [[UIButton alloc] init];
    scrollingbutton_outlet = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [UIImage imageNamed:@"box_normal.png"];
    scrollingbutton_outlet.frame = CGRectMake(0, 100, img.size.width, img.size.height);
     [scrollingbutton_outlet setTitle:[NSString stringWithFormat:@"%d",i+1] forState: UIControlStateNormal];
    scrollingbutton_outlet.tag=i+1;
    [buttonArray addObject:[NSString stringWithFormat:@"%d",i+1]];
    buttonArray = [[NSMutableArray alloc] init];
    [scrollingbutton_outlet setBackgroundImage:img forState:UIControlStateNormal];
    [scrollingbutton_outlet addTarget:self
                               action:@selector(scrollbuttonpress:)
                         forControlEvents:UIControlEventTouchUpInside];

    scrollingbutton_outlet.frame = CGRectMake(img.size.width*i,0, img.size.width,      scrollviewoutlet.frame.size.height);
    [scrollviewoutlet addSubview:scrollingbutton_outlet];
    width = scrollingbutton_outlet.frame.size.width;
    height = scrollingbutton_outlet.frame.size.height;
    scrollviewoutlet.contentSize = CGSizeMake(width*i+scrollingbutton_outlet.bounds.size.width+5, height);
}