全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

IOS开发之UIScrollView实现图片轮播器的无限滚动

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, 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, imageY, imageW,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实现图片的轮播的功能,如有疑问大家可以留言,也可以到本站社区留言讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


# UIScrollView实现图片轮播器的无限滚动  # IOS  # 轮播的实现  # 一行iOS代码实现图片无限轮播器  # IOS图片无限轮播器的实现原理  # iOS实现无限循环图片轮播器的封装  # IOS使用UICollectionView实现无限轮播效果  # iOS开发中使用UIScrollView实现图片轮播和点击加载  # IOS实现图片轮播无限循环效果  # iOS实现带有缩放效果的自动轮播图  # iOS实现图片轮播效果  # iOS实现无限循环轮播图效果  # iOS实现图片轮播器  # 第一张  # 设置为  # 就会  # 是在  # 如有  # 希望能  # 这么大  # 分页  # 只需要  # 可以实现  # 两张  # 谢谢大家  # 到第  # 所写  # 切换到  # 中时  # 量为  # 不包含  # 组中  # 是为了 


相关文章: 定制建站如何定义?其核心优势是什么?  Thinkphp 中 distinct 的用法解析  TestNG的testng.xml配置文件怎么写  娃派WAP自助建站:免费模板+移动优化,快速打造专业网站  网站制作培训多少钱一个月,网站优化seo培训课程有哪些?  高防服务器租用如何选择配置与防御等级?  活动邀请函制作网站有哪些,活动邀请函文案?  建站之星如何配置系统实现高效建站?  如何规划企业建站流程的关键步骤?  如何快速搭建FTP站点实现文件共享?  如何自定义建站之星模板颜色并下载新样式?  建站之星代理如何优化在线客服效率?  香港服务器网站卡顿?如何解决网络延迟与负载问题?  专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?  制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  建站之星后台密码遗忘?如何快速找回?  详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)  如何快速查询域名建站关键信息?  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  创业网站制作流程,创业网站可靠吗?  个人网站制作流程图片大全,个人网站如何注销?  油猴 教程,油猴搜脚本为什么会网页无法显示?  如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南  已有域名能否直接搭建网站?  c# Task.ConfigureAwait(true) 在什么场景下是必须的  建站之星图片链接生成指南:自助建站与智能设计教程  如何在IIS7中新建站点?详细步骤解析  建站之星代理费用多少?最新价格详情介绍  如何访问已购建站主机并解决登录问题?  非常酷的网站设计制作软件,酷培ai教育官方网站?  如何在企业微信快速生成手机电脑官网?  浅谈Javascript中的Label语句  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南  如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?  C++如何编写函数模板?(泛型编程入门)  宝塔新建站点为何无法访问?如何排查?  如何在云指建站中生成FTP站点?  如何制作一个表白网站视频,关于勇敢表白的小标题?  香港服务器租用费用高吗?如何避免常见误区?  建站之星展会模板:智能建站与自助搭建高效解决方案  代刷网站制作软件,别人代刷火车票靠谱吗?  如何用wdcp快速搭建高效网站?  如何在腾讯云服务器快速搭建个人网站?  百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?  高性能网站服务器配置指南:安全稳定与高效建站核心方案  洛阳网站制作公司有哪些,洛阳的招聘网站都有哪些?  上海网站制作网页,上海本地的生活网站有哪些?最好包括生活的各个方面的?  如何确保FTP站点访问权限与数据传输安全?  如何实现建站之星域名转发设置? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。