2011-07-22 19 views
0

在这段代码中,我想要做的改变,而不是按钮我想在图像视图中创建图像与滚动视图放大和缩小图像上的事件。每个图像视图上有两个或三个按钮,以便我可以在按钮上实现一些事件。那是怎么回事?如何在滚动视图中使用滚动视图而不是uibutton添加图像?

- (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)]; 
      //awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1]; 
      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=460; 
      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

我还是不太明白你在问什么。你的句子没有意义。 – jzd

+0

我在说,在上面的代码中,我将uibutton添加到视图中。在这个现在我想添加滚动启用图像。 – ram

+0

请注意,请在您的帖子中使用正确的大小写。 – jzd

回答

2

您需要Image而不是Button吗?然后试试这个:

scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; 
    scroll.backgroundColor=[UIColor clearColor]; 
    scroll.pagingEnabled=YES; 
    scroll.contentSize = CGSizeMake(320*33, 300); 
    CGFloat x=0; 
    for(int i=1;i<34;i++) 
    {   
     UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x+0, 0, 320, 460)]; 
     [image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"page-%d.jpg",i]]]; 
     [scroll addSubview:image]; 
     [image release]; 
     x+=320; 
    } 
    [self.view addSubview:scroll]; 
    scroll.showsHorizontalScrollIndicator=NO; 
    [scroll release]; 
+0

如何应用放大和缩小功能? – ram

+0

将_UIPinchGestureRecognizer_应用于图像,以便您可以使用_UIPinchGestureRecognizer委托_缩放。 – Sisu