【问题标题】:Making Image Slider on Storyboard Issue在情节提要问题上制作图像滑块
【发布时间】:2015-02-18 11:15:58
【问题描述】:

我制作了一个基本的图像滑块,通过添加子视图可以正常工作。但是我应该在故事板中制作它。我正在运行几乎相同的代码,但它没有运行。

我只想制作 3 张图片滑块。

那么请谁能告诉我问题出在哪里?

- (void)run{

pageNumber =3;

_scrollview = [[UIScrollView alloc] init];
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.pagingEnabled = true;
_scrollview.bounces = false;
_scrollview.delegate = self;
_scrollview.showsHorizontalScrollIndicator = false;
_scrollview.layer.cornerRadius = 2;

_pgcontrol = [[UIPageControl alloc] init];
_pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
_pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
_pgcontrol.numberOfPages = pageNumber;
_pgcontrol.currentPage = 0;
[_pgcontrol sizeToFit];

UIImageView*im_ = [[UIImageView alloc] init];
im_.contentMode = UIViewContentModeScaleAspectFit;
im_.clipsToBounds = true;
im_.image = [UIImage imageNamed:@"31900.png"];

UIImageView*im1_ = [[UIImageView alloc] init];
im1_.contentMode = UIViewContentModeScaleAspectFit;
im1_.clipsToBounds = true;
im1_.image = [UIImage imageNamed:@"31901.png"];

UIImageView*im2_ = [[UIImageView alloc] init];
im2_.contentMode = UIViewContentModeScaleAspectFit;
im2_.clipsToBounds = true;
im2_.image = [UIImage imageNamed:@"31902.png"];

[_scrollview addSubview:im_];
[_scrollview addSubview:im1_];
[_scrollview addSubview:im2_];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

int page_ = (int)round(scrollView.contentOffset.x / scrollView.frame.size.width);

if (page_== pageNumber-3) {
    NSLog(@"page3");
}

else if (page_ == pageNumber-2){
    NSLog(@"page2");
}

else if (page_ == pageNumber-1){
    NSLog(@"page3");
}
_pgcontrol.currentPage = page_;
}

///// 旧代码 /////

- (void)imageCreater{

self.view.backgroundColor = [UIColor whiteColor];
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

CGRect svrect_ = CGRectZero;
svrect_.size.height = self.view.bounds.size.height/3.1*2;
svrect_.size.width = self.view.bounds.size.width/3*2;

CGPoint svcenter_ = CGPointZero;
svcenter_.x = self.view.center.x;
svcenter_.y = self.view.center.y+65;

CGSize svconsize = CGSizeZero;
svconsize.height = svrect_.size.height;
svconsize.width = svrect_.size.width * 3;

CGPoint pgconcenter_ = CGPointZero;
pgconcenter_.x = self.view.center.x;
pgconcenter_.y = svcenter_.y + (svrect_.size.height/2) + 40;

CGRect btnrect_ = CGRectZero;
btnrect_.size.width = 250;
btnrect_.size.height = 50;

CGPoint btncenter_ = CGPointZero;
btncenter_.x = self.view.center.x;
btncenter_.y = self.view.bounds.size.height-65;

_backgroundimageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_backgroundimageview];

_scrollview = [[UIScrollView alloc] initWithFrame:svrect_];
_scrollview.center = svcenter_;
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.contentSize = svconsize;
_scrollview.pagingEnabled = true;
_scrollview.bounces = false;
_scrollview.delegate = self;
_scrollview.showsHorizontalScrollIndicator = false;
_scrollview.layer.cornerRadius = 2;
[self.view addSubview:_scrollview];

_pgcontrol = [[UIPageControl alloc] initWithFrame:CGRectZero];
_pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
_pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
_pgcontrol.numberOfPages = 3;
_pgcontrol.currentPage = 0;
[_pgcontrol sizeToFit];
_pgcontrol.center = pgconcenter_;
[self.view addSubview:_pgcontrol];

UIImageView*iv_ = [[UIImageView alloc] initWithFrame:svrect_];
iv_.contentMode = UIViewContentModeScaleAspectFit;
iv_.clipsToBounds = true;
iv_.image = [UIImage imageNamed:@"31900.png"];

svrect_.origin.x += iv_.frame.size.width;

UIImageView*iv1_ = [[UIImageView alloc] initWithFrame:svrect_];
iv1_.contentMode = UIViewContentModeScaleAspectFit;
iv1_.clipsToBounds = true;
iv1_.image = [UIImage imageNamed:@"31901.png"];

svrect_.origin.x += iv1_.frame.size.width;

UIImageView*iv2_ = [[UIImageView alloc] initWithFrame:svrect_];
iv2_.contentMode = UIViewContentModeScaleAspectFit;
iv2_.clipsToBounds = true;
iv2_.image = [UIImage imageNamed:@"31902.png"];

[_scrollview addSubview:iv_];
[_scrollview addSubview:iv1_];
[_scrollview addSubview:iv2_];
}

【问题讨论】:

  • 您在使用情节提要时遇到了什么问题?您是否将 UIScrollView 放在用户界面文件上并将其链接到文件所有者?
  • @AdeelUrRehman 我的问题是我没有得到任何图像。是的,我已经链接了界面链接。
  • 让我为您发布答案。给我一分钟。
  • 你需要水平滚动还是垂直滚动?
  • 我需要它,就像我认为是水平的分页一样。

标签: ios uiscrollview uiimageview uipagecontrol


【解决方案1】:

试试这个:

- (void)run
{
    // If you are using story board and user interface then you don't need to initialize scroll view.
    _scrollview.backgroundColor = [UIColor whiteColor];
    _scrollview.pagingEnabled = true;
    _scrollview.bounces = false;
    _scrollview.delegate = self;
    _scrollview.showsHorizontalScrollIndicator = false;
    _scrollview.layer.cornerRadius = 2;
    _scrollview.contentSize = CGSizeMake(320*3, _scrollview.frame.size.height);

    // Same for Page control you dont need to initialize it
    _pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
    _pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
    _pgcontrol.numberOfPages = pageNumber;
    _pgcontrol.currentPage = 0;

    UIImageView *im_ = [[UIImageView alloc] initWithFrame:CGRectMake((_scrollview.frame.size.width*0),0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
    im_.contentMode = UIViewContentModeScaleAspectFit;
    im_.clipsToBounds = true;
    im_.image = [UIImage imageNamed:@"31900.png"];

    UIImageView*im1_ = [[UIImageView alloc] initWithFrame:CGRectMake((_scrollview.frame.size.width*1),0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
    im1_.contentMode = UIViewContentModeScaleAspectFit;
    im1_.clipsToBounds = true;
    im1_.image = [UIImage imageNamed:@"31901.png"];

    UIImageView*im2_ = [[UIImageView alloc] initWithFrame:CGRectMake((_scrollview.frame.size.width*2),0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
    im2_.contentMode = UIViewContentModeScaleAspectFit;
    im2_.clipsToBounds = true;
    im2_.image = [UIImage imageNamed:@"31902.png"];

    [_scrollview addSubview:im_];
    [_scrollview addSubview:im1_];
    [_scrollview addSubview:im2_];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page_ = (int)round(scrollView.contentOffset.x / scrollView.frame.size.width);

    if (page_== pageNumber-3) 
    {
        NSLog(@"page3");
    }

    else if (page_ == pageNumber-2)
    {
        NSLog(@"page2");
    }

    else if (page_ == pageNumber-1)
    {
        NSLog(@"page3");
    }
    _pgcontrol.currentPage = page_;
}

【讨论】:

  • 很抱歉它不起作用。这也是垂直的而不是水平的。没有图片
  • 这不是垂直的,因为我正在更改图像的 x 坐标并将它们放入 UIScrollView。如果很容易分享代码,那么请这样做。解决问题真的很容易。
  • 你的意思是我分享这个项目吗?
  • 是的,如果可以的话,因为我已经尝试了上面的代码并且它对我来说工作正常。
  • 在你的旧代码中我注意到你正在将矩形分配给你在滚动视图中添加的图像,但你没有像在旧代码中那样在新代码中设置它.
猜你喜欢
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 2018-07-01
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多