如何在滚动视图中添加带有滚动视图的图像而不是 uibutton?

发布于 2024-11-25 09:15:17 字数 2134 浏览 1 评论 0原文

在此代码中,我想要进行更改,例如 as 而不是按钮,我想在图像视图中使用滚动视图创建图像,并在图像上放大和缩小事件。每个图像视图上有两个或三个按钮,以便我可以在按钮上实现一些事件。怎么办呢?

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

}

In this code I want to do changes like as instead of button I want to create images in image view with scroll view with zoom in and zoom out event on image. And two or three button on every image view so that I can implement some event on button. How do that?

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2024-12-02 09:15:17

您需要图像而不是按钮,对吗?然后试试这个:

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

You need Image instead of Button right? Then Try This:

    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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文