如何在滚动视图中使用滚动视图而不是uibutton添加图像?

时间:2011-07-22 10:54:21

标签: iphone xcode uiview uiscrollview uiimageview

在这段代码中我想进行更改,比如代替按钮我想在图像视图中使用滚动视图创建图像,放大并缩小图像上的事件。每个图像视图上有两个或三个按钮,这样我就可以在按钮上实现一些事件。怎么样?

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

}

1 个答案:

答案 0 :(得分: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];