【问题标题】:Fix next image in scroll view like in page view修复滚动视图中的下一个图像,就像在页面视图中一样
【发布时间】:2015-10-25 11:13:20
【问题描述】:

我创建了一个滚动视图,但我想让它看起来像一个页面视图。

- (void)setupScrollView:(UIScrollView*)scrMain {
            for (int i=1; i<=11; i++) {
                image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
                      if (i==1) {
                    imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 2, 289, 470)];
                }
                 else
                    imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*350, 2, 289, 470)];
                imgV.contentMode=UIViewContentModeScaleToFill;
                [imgV setImage:image];
                imgV.tag=i+1;
                [imgV bringSubviewToFront:self.aleatoire];
                [scrMain addSubview:imgV];
                UIImageView *aleatoire=[[UIImageView alloc]initWithFrame:CGRectMake (self.aleatoire.frame.origin.x-19,self.aleatoire.frame.origin.y,self.aleatoire.frame.size.width,self.aleatoire.frame.size.height)];
                aleatoire.image=self.aleatoire.image;
                [imgV addSubview:aleatoire];
                [imgV bringSubviewToFront:aleatoire];
                UIButton *rand = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [rand addTarget:self
                         action:@selector(randomBtn:)
                  forControlEvents:UIControlEventTouchUpInside];

                rand.frame=aleatoire.frame;

                rand.backgroundColor=[UIColor clearColor];
                rand.userInteractionEnabled = YES;


                imgV.userInteractionEnabled=YES;
                [imgV addSubview:rand];
                [imgV bringSubviewToFront:rand];




            }
            [scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*11, scrMain.frame.size.height)];

        }

但我有这个问题:

下一张图片不在中间。任何想法我错了?

【问题讨论】:

标签: ios objective-c uiscrollview


【解决方案1】:

您设置UIScrollView paging pagingEnabled 属性启用以创建类似pageView 的滚动行为。

试试这个代码:

scrMain.pagingEnabled=YES;
scrMain.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0); // set your size to scroll content

UIScrollView中创建pagerView的简单方法

int x = 0;

    for (int i=1; i<=11; i++) {
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height)];
        img.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];

        [self.scrollView addSubview:img];
        x = x + [[UIScreen mainScreen] bounds].size.width;
    }
    self.scrollView.contentSize = CGSizeMake(x, [[UIScreen mainScreen] bounds].size.height-64);
    self.scrollView.pagingEnabled = YES;

    self.scrollView.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0);

【讨论】:

  • 我设置了 scrMain.pagingEnabled=YES; scrMain.contentOffset = CGPointMake(scrMain.frame.size.width, 0);但它滚动得太快直到结束
  • 你的想法可行,谢谢,只有一个问题。为什么是从第二张图片开始?
  • 你设置了 x=0 吗?和 scrollVIew x=0, y=0, width=height=screen size @AdinaMarin
  • 图像帧我这样设置: UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, [[UIScreen mainScreen] bounds].size.width,self.card. frame.size.height)];
  • 是的,正确设置了框架,我一直使用此代码并正常工作。我认为滚动中的问题你检查一下。 @AdinaMarin
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多