• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 带有大量 UIButtons 的 UIScrollView

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:08:24 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想要的是一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap