IOS开发篇UI之重用scrollView

1.scrollView的介绍

scrollView是UI中的基础视图,他有着至关重要的作用,也是我们在UI中常用的控件。他的代理有很多我们需要用,这里我们就不再一一介绍了。

2.简单scrollView的使用

(1)scrollView的使用方法非常简单,首先先声明scrollView对象,定义对象的坐标和大小,需要用到代理的时候就声明一下代理scrollView.delegate = self;然后将其加入视图中。

(2)在将需要在scrollView中需要先向的控件声明,定义坐标及大小。然后加入scroll中

(3)假如我们scroll中的控件在一个屏幕中不够显现,这是后句需要扩展scroll。用多少扩展多少

(4)scroll在Masonry(第三方库)使用约束时候需要注意,滚动的方向的两边约束不能对比scroll,否则scrollView滚动不了

3.重用scrollView

(1)当我们放入scrollView中的控件所占内存比较大时,容易造成手机卡死。那么我们就需要减少scrollView中的内存。所以我们想到了重用scrollView。

(2)步骤

  【1】重用scroll我们一般需要三个屏幕宽度,当前显示在中间,两边各有一张备用屏幕(先不考虑最左边和最右边)。

  【2】使用scrollView中的一个代理:作用是滑动之后会调用这个代理。

  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

  假设我们的滚动视图为横向滚动,使用:

  int page = (scrollView.contentOffset.x)/scrollView.frame.size.width;的到当前的是第几页

  然后重用scrollView,将当前页数设为scrollView三页中的最中间的页数

  【3】还要考虑最左边和最右边,判断一下是不是在最左边或者是最右边。如果是则

  假如是最左边

     我们就不要左边的一张图,并且在代理个函数中写当为第一页时,不支持像左边滑动

  假如是最右边

    我们就不要右边的一张图,且不支持向右滑动

4.代码DEMO

   int n = 3;
    int m = 0;
    if (self.withimage == self.allarr.count - 1 || self.withimage == 0) {
        n = 2;
    }
    //如果只有一张图片
    if(self.allarr.count == 1)
    {
        self.addscroll = [[addTapandImageGToScroll alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
        UIImage * image = self.allarr[self.withimage];
        self.addscroll.iamgeV.frame = self.frame;
        NSLog(@"%@",NSStringFromCGRect(self.frame));
        [self.Scroller addSubview:self.addscroll];
        [UIView animateWithDuration:1 animations:^{
            CGSize imagesize = image.size;
            double pror = WIDTH/imagesize.width;
            self.addscroll.iamgeV.frame = CGRectMake(0, (HEIGHT - pror * HEIGHT)/2, WIDTH, pror * HEIGHT);
        }];
    }
    for (int i = 0; i < n; i ++)
    {
        self.addscroll = [[addTapandImageGToScroll alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        UIImage * image ;
        if (self.withimage == 0) {
            image = self.allarr[self.withimage + i];
        }
        else
        {
            image = self.allarr[self.withimage - 1 + i];
        }

        NSLog(@"++++++++++++++++++++++%d",self.withimage - 1 + i);
        self.addscroll.iamgeV.tag = i;
        self.addscroll.iamgeV.image = image;
        int s;
        if (self.withimage == 0) {
            s = 0;
        }
        else
        {
            s = 1;
        }
        
        if (i == s && firstShowAnimateimage == 0) {
            self.addscroll.iamgeV.frame = self.frame;
            NSLog(@"%@",NSStringFromCGRect(self.frame));
            [self.Scroller addSubview:self.addscroll];
            [UIView animateWithDuration:1 animations:^{
                CGSize imagesize = image.size;
                double pror = WIDTH/imagesize.width;
                self.addscroll.iamgeV.frame = CGRectMake(0, (HEIGHT - pror * HEIGHT)/2, WIDTH, pror * HEIGHT);
            }];
            firstShowAnimateimage = firstShowAnimateimage + 1;
        }
        else
        {
            [self.Scroller addSubview:self.addscroll];
            CGSize imagesize = image.size;
            double pror = WIDTH/imagesize.width;
            self.addscroll.iamgeV.frame = CGRectMake(0, (HEIGHT - pror * HEIGHT)/2, WIDTH, pror * HEIGHT);
        }
      
        //设置下面的属性
        self.addscroll.userInteractionEnabled = YES;
        self.addscroll.iamgeV.userInteractionEnabled = YES;
        //设置点击一次事件
        self.addscroll.block = ^(addTapandImageGToScroll *add)
        {
            if (tapnumber == 0)
            {
                weakself.viewBack.hidden = NO;
                tapnumber = 1;
            }
            else
            {
                weakself.viewBack.hidden = YES;
                tapnumber = 0;
            }
        };
    }
    
    self.Scroller.contentSize = CGSizeMake(WIDTH * (n - m), 0);
    NSLog(@"%d",self.index);
    if (self.withimage == 0) {
        self.Scroller.contentOffset = CGPointMake(0, 0);
    }
    else
    {
        self.Scroller.contentOffset = CGPointMake(WIDTH * 1, 0);
    }

    self.nowimagedex = 2;
    saveimage = self.allarr[self.withimage];
    [self addSome];
    
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //关于手势
    tapnumber = 0;
    
    if (self.allarr.count == 1) {
        return;
    }
    
    int page = (scrollView.contentOffset.x)/scrollView.frame.size.width;
    NSLog(@"%d",page);
    
    if (self.withimage == 0 && page == 0) {
        return;
    }
    
    self.nowimagedex = page;
    self.labeltitle.text = self.allname[ page];
    [self.labeltext setDefaultStyleBy:self.alltext[page]];
    CGFloat height = [self.labeltext heightForStringByWidth:WIDTH];
    self.scrollText.contentSize = CGSizeMake(0, height);
    if (height+60>HEIGHT*0.5){
        self.viewBack.frame = CGRectMake(0, HEIGHT*0.5, WIDTH, HEIGHT*0.5);
    }else
    {
        self.viewBack.frame = CGRectMake(0, HEIGHT-height-90, WIDTH, height+90);
    }
    saveimage = self.allarr[page];
    
    if (page == 0)
    {
        self.withimage = self.withimage - 1;
        [self showImageWithScroll];
    }
    if (page == 2)
    {
        self.withimage = self.withimage + 1;
        [self showImageWithScroll];
    }
    if (page == 1 && self.withimage == 0) {
        self.withimage = self.withimage + 1;
        [self showImageWithScroll];

    }
}

 

      

 

你可能感兴趣的:(IOS开发篇UI之重用scrollView)