【问题标题】:UIScrollView with a large number of UIButtons带有大量 UIButtons 的 UIScrollView
【发布时间】:2011-11-21 19:01:57
【问题描述】:

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

    [scrollView addSubview:[matrixController view]];

我怎样才能让它工作??

【问题讨论】:

  • 滚动视图正在拦截触屏事件。方法 1) 应该有效,您应该为按钮选择 touch up inside 事件。
  • 即使触地事件不是他想要的,它仍然应该触发该事件。

标签: ios cocoa-touch uiview uiscrollview uibutton


【解决方案1】:

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

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

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

【讨论】:

  • 按钮的缩放和触摸似乎可以正常工作,但是如何更改它们的属性之一?它们位于视图内的 ScrollView 内的视图内。这个:[[[self.view viewWithTag:0] viewWithTag:0] viewWithTag:buttonTag] 不可能是答案……此外——它不起作用……
  • 你真的不应该像这样依赖tag 属性。它不是很灵活。相反,您应该保留自己的指向感兴趣对象的指针集合并以类似方式对它们进行索引(例如在数组中)。
  • 感谢您的回答,这对我很有用:) 只是一个问题:它在纵向模式下工作正常,但在横向模式下,滚动视图在设备的高度水平“剪切”(768px for iPad)。我该如何解决这个问题?
  • 您需要为滚动视图设置自动调整大小的掩码。这些决定了子视图在其父视图的框架发生变化时如何调整大小和定位。在文档中查找。
【解决方案2】:
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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 2012-07-05
    相关资源
    最近更新 更多