OStack程序员社区-中国程序员成长平台

标题: ios - 带有大量 UIButtons 的 UIScrollView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:08
标题: ios - 带有大量 UIButtons 的 UIScrollView

我想要的是一个 UIView,里面有很多 UIButtons。它们根据存储在 NSArray 中的数据进行放置和排列。因为有很多按钮,它们不能一次全部放在屏幕上。用户应该能够缩小以查看所有按钮或放大以查看详细信息(按钮上的标签)并轻松选择它们。

我尝试了两种不同的方法:

1) 我构建了一个 UIView 子类,将按钮放入其中,并将此 View 的实例放入 UIScrollview

效果:我可以通过它们的标签访问所有按钮,并且滚动和缩放工作正常。但我无法获得处理任何事件的按钮(按下它们)...

2) 我写了一个功能完全相同的 UIViewController 并将它的一个实例添加到 UIScrollView

效果:我现在可以按下按钮了,但是滚动和缩放已经停止工作了。

这里是 View 的相关代码:

    - (UIView *)initWithArrayNSArray *)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 actionselector(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 actionselector(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]];

我怎样才能让它工作??



Best Answer-推荐答案


我能够获得一个包含多个按钮的 ScrollView ,可以很好地平移和缩放,而这些按钮仍在处理触摸事件:

- (void)didTapButtonUIButton *)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 actionselector(didTapButton forControlEvents:UIControlEventTouchUpInside];

        [zoomView addSubview:button];
    }

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

- (UIView *)viewForZoomingInScrollViewUIScrollView *)scrollView
{
    return [scrollView.subviews objectAtIndex:0];
}

编辑:我在下面的评论中说过不要依赖标签,但我在这里做。 这只是为了让我可以记录按钮编号,因此应该忽略该部分。

关于ios - 带有大量 UIButtons 的 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7497590/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4