开发者

UIScrollView with a large number of UIButtons

What I want is a UIView with lots of UIButtons in it. They get placed and arranged according to data stored in an NSArray. Because there are quite a lot of buttons they don't fit on the screen all at once. The user should be able to zoom out to see all the buttons or to zoom in to see details (the label on them) and to easily select them.

I tried two different approaches:

1) I constructed a UIView subclass, put the buttons in it and an instance of this View inside a UIScrollview.

Effect: I can access all Buttons via their tag and scrolling and zooming works fine. BUT I can't get the buttons to handle any events (press on them)...

2) I wrote a UIViewController with exactly the same functionality and added an instance of it to the UIScrollView.

Effect: I can press the buttons now, but scrolling and zooming have stopped to work.

Here the relevant Code of the View:

    - (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;

}

And the Code for the 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];
}

}

And the code for the 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:[matr开发者_运维技巧ixController view]];

How can I get this to work??


I'm able to get a scroll view containing multiple buttons to pan and zoom just fine, with the buttons still handling touch events:

- (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];
}

EDIT: I said not to rely on tag in my comment below, yet I'm doing in here. :) It's merely so I could log the button number, so that part should be ignored.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜