gpt4 book ai didi

ios - 带有大量 UIButton 的 UIScrollView

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:18:21 24 4
gpt4 key购买 nike

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

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

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

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

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

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

这里是 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;

矩阵 Controller 的代码:

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

    [scrollView addSubview:[matrixController view]];

我怎样才能让它工作?

最佳答案

我能够获得一个 ScrollView ,其中包含多个用于平移和缩放的按钮,这些按钮仍在处理触摸事件:

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com