2011-09-21 42 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); 
} 
相關問題