加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

IOS开拓之UIScrollView实现图片轮播器的无穷转动

发布时间:2021-01-11 00:15:30 所属栏目:创业 来源:网络整理
导读:IOS开拓之UIScrollView实现图片轮播器的无穷转动 简介 在此刻的一些App中经常见到图片轮播器,一样平常用于展示告白、消息等数据,在iOS内并没有现成的控件直接实现这种成果,可是通过UIScrollView的应承分页配置,可以实现转动轮播的成果。 轮播道理 UIScrollVi

IOS开拓之UIScrollView实现图片轮播器的无穷转动

简介

在此刻的一些App中经常见到图片轮播器,一样平常用于展示告白、消息等数据,在iOS内并没有现成的控件直接实现这种成果,可是通过UIScrollView的应承分页配置,可以实现转动轮播的成果。

轮播道理

UIScrollView工具有pagingEnable成员,假如配置为YES,那么每一个scrollView尺寸这么大的地区就会被看成一页,在转动时会按照转动的比例自动计较应该切换到哪一页。

无穷转动道理

要实现无穷转动,必要特另外两张图片,假设我们的图片有五张,存在images数组中,那么我们在将图片插入到scrollView中时,在第一张图片前面插入一个最后一张图片作为帮助图片,在最后一张后头插入一个第一张图片作为帮助图片。这样,当转动到第一张前面一张时,在页面切换竣事后无动画的切换scrollView的偏移量为最后一张图片(不包括最后一张后头的第一张谁人帮助图片),这样就实现了由帮助图片到真实图片的过渡,之以是配置帮助图片是为了在转动中看到谁人真实图片。同理,当转动到最后一张的后头一张时,我们吧scrollView的偏移量配置为第一张图片即可。

详细的代码实现

这个代码是在开拓一个项目中所写的,已经封装称一个View,只必要挪用initWithFrame指定轮播器尺寸,然后通过配置images成员的值即可实现无穷转动的轮播。

// .h
//
// ESPicPageView.h
// 享技
//
// Created by 11 on 11/30/15.
// Copyright © 2015 soulghost. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ESPicPageView : UIView

@property (nonatomic,strong) NSArray *images;

@end
// --------------------------------------------
// .m
//
// ESPicPageView.m
// 享技
//
// Created by 11 on 11/30/15.
// Copyright © 2015 soulghost. All rights reserved.
//

#import "ESPicPageView.h"
#import "UIImageView+WebCache.h"

@interface ESPicPageView () <UIScrollViewDelegate>

@property (nonatomic,weak) UIScrollView *scrollView;
@property (nonatomic,weak) UIPageControl *pageControl;
@property (nonatomic,assign) CGFloat imgW;
@property (nonatomic,assign) CGFloat imgH;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) NSArray *imageViews;
@property (nonatomic,assign) NSInteger imageCount;

@end

@implementation ESPicPageView

- (instancetype)initWithFrame:(CGRect)frame{

  if (self = [super initWithFrame:frame]) {

    self.backgroundColor = [UIColor blueColor];
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    self.scrollView = scrollView;
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:scrollView];
    self.imgW = frame.size.width;
    self.imgH = frame.size.height;
    [self setNeedsLayout];

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(frame.size.width - 50,frame.size.height - 10,0)];
    self.pageControl = pageControl;
    self.pageControl.numberOfPages = 1;
    [self addSubview:pageControl];

    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

  }

  return self;

}

- (void)nextImage{
  NSInteger page = self.pageControl.currentPage;
  page = self.pageControl.currentPage + 1;
  CGPoint offset = CGPointMake((1 + page) * self.imgW,0);
  [self.scrollView setContentOffset:offset animated:YES];
}

- (void)setupImageViews{

  for (int i = 0; i < self.images.count + 2; i++) {
    UIImageView *imageView = [[UIImageView alloc] init];
    CGFloat imageX = i * self.imgW;
    CGFloat imageY = 0;
    CGFloat imageW = self.imgW;
    CGFloat imageH = self.imgH;
    imageView.frame = CGRectMake(imageX,imageY,imageW,imageH);
    [self.scrollView insertSubview:imageView atIndex:0];
  }

}

- (NSArray *)imageViews{

  if (_imageViews == nil) {
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < self.images.count + 2; i++) {
      UIImageView *imageView = [[UIImageView alloc] init];
      CGFloat imageX = i * self.imgW;
      CGFloat imageY = 0;
      CGFloat imageW = self.imgW;
      CGFloat imageH = self.imgH;
      imageView.frame = CGRectMake(imageX,imageH);
      [self.scrollView insertSubview:imageView atIndex:0];
      [arr addObject:imageView];
    }
    _imageViews = arr;
  }

  return _imageViews;

}

- (void)setImages:(NSArray *)images{

  _images = images;
  self.imageCount = images.count;
  self.pageControl.numberOfPages = self.imageCount;
  [self addPics];

}

- (void)addPics{

  for (int i = 0; i < self.images.count + 2; i++) {
    UIImageView *imageView = self.imageViews[i];
    if (i == 0) {
      imageView.image = [self.images lastObject];
    }else if(i == self.images.count + 1){
      imageView.image = [self.images firstObject];
    }else{
      imageView.image = self.images[i - 1];
    }
  }

}

- (void)layoutSubviews{

  [super layoutSubviews];
  self.scrollView.frame = self.bounds;
  self.scrollView.contentSize = CGSizeMake((self.imageCount + 2) * self.imgW,0);
  [self.scrollView setContentOffset:CGPointMake(self.imgW,0) animated:NO];

}

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

  [self.timer invalidate];
  self.timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

  self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
  [[NSRunLoop currentRunLoop ] addTimer:self.timer forMode:NSRunLoopCommonModes];

}

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

  if (scrollView.contentOffset.x == self.imgW * (self.imageCount + 1)) {
    [self.scrollView setContentOffset:CGPointMake(self.imgW,0) animated:NO];
  }else if(scrollView.contentOffset.x == 0){
    [self.scrollView setContentOffset:CGPointMake(self.imgW * (self.imageCount),0) animated:NO];
  }

  self.pageControl.currentPage = (self.scrollView.contentOffset.x + self.imgW * 0.5f) / self.imgW - 1;

}

@end

以上就是行使UIScrollView实现图片的轮播的成果,若有疑问各人可以留言,也可以到本站社区留言接头,感激阅读,但愿能辅佐到各人,感谢各人对本站的支持!

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读