2011-07-25 32 views
0

enter image description here我如何使用按鈕創建圖像視圖?

我想創建32個視圖像上面的看法。所以我修改我的代碼,所以我得到這個觀點?我之前在for循環圖所示

- (void)loadView { 
[super loadView]; 
self.view.backgroundColor = [UIColor redColor]; 
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
scroll.pagingEnabled = YES; 
NSInteger numberOfViews = 33; 
[btnMenu setTag:0 ]; 
for (int i = 1; i < numberOfViews; i++) { 
    CGFloat yOrigin = i * self.view.frame.size.width; 
    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
      btnMenu = [UIButton buttonWithType:UIButtonTypeCustom]; 
    //NSData *data =UIImageJPEGRepresentation(, 1); 
    [btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"page-%d.jpg",i]] forState:UIControlStateNormal]; 
    CGRect frame = btnMenu.frame; 
    frame.size.width=320; 
    frame.size.height=420; 
    frame.origin.x=0; 
    frame.origin.y=0; 
    btnMenu.frame=frame; 
    [btnMenu setTag:i]; 
    btnMenu.alpha = 1; 
    [btnMenu addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    [awesomeView addSubview:btnMenu]; 

    [scroll addSubview:awesomeView]; 
    [awesomeView release]; 
} 
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height); 
[self.view addSubview:scroll]; 
[scroll release];} 

-(IBAction)btnSelected:(id)sender{ 
UIButton *button = (UIButton *)sender; 
int whichButton = button.tag; 
NSLog(@"Current TAG: %i", whichButton); 
if(whichButton==1) 
{ 
    first=[[FirstImage alloc]init]; 
    [self.navigationController pushViewController:first animated:YES]; 
     } 
    } 
+0

你想做什麼,如果你想添加這33個圖像作爲滾動視圖的子視圖,那麼你必須相應地設置滾動視圖的內容大小,然後在for循環中設置每個圖像的框架和它們的子視圖滾動視圖。 –

+0

是的,我想在每個視圖圖像中添加32個視圖,一個按鈕將在那裏。 – ram

回答

0
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height); 

寫代碼在這行代碼。

相關問題