2011-09-21 57 views
2

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

我尝试两种不同的方法:

1)I构成的子类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]; 

OR

[scrollView addSubview:[matrixController view]]; 

我怎样才能得到这个工作?

+0

滚动视图被拦截触地事件。方法1)应该已经工作了,您应该选择触摸按钮的内部事件。 – Tom

+0

即使触碰事件不是他想要的,它仍然应该触发事件。 – LucasTizma

回答

3

我能够得到一个包含多个按钮平移和缩放就好了,用按钮还是处理触摸事件滚动视图:

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

编辑:我说不要依赖于标签在我的评论下面,但我在这里做。 :)这仅仅是我可以记录按钮号码,所以这部分应该被忽略。

+0

缩放和触摸按键似乎很好地工作,但我怎么能更改其属性之一吗?它们位于视图内的ScrollView内的视图内。这:'[[[self.view viewWithTag:0] viewWithTag:0] viewWithTag:buttonTag]'不可能是答案...此外 - 它不工作... – user852303

+0

你真的不应该依赖'标签这样的财产。这不是很灵活。相反,您应该坚持自己的指向您感兴趣对象的指针集合并对它们进行类似索引(例如在数组中)。 – LucasTizma

+0

感谢您的回答,这对我很有用:)只是一个问题:在纵向模式下工作正常,但在横向模式下,滚动视图在设备高度(iPad的768px)水平“切割”。我该如何解决这个问题? –

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