学IOS感觉时间有点紧,都没有什么时间来深入了解一些基本控件,所以现在先封装一个ScrollView控件来给自己和大家用吧(希望大家可以理解,scrollView的本质是改变scrollView的bounds来实现的),希望可以给大家带来一些帮助,本控件实现可自动无限循环播放,使用3个imageView来显示,用户只需在实现一下初始化方法的时候
- (instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)arr timeInterval : (NSTimeInterval)timeInterval;
将多个图片加入该自定义控件中,并设置自动播放的时间间隔,即可实现。
--------------------------------------------------------------------------
头文件liliLoopScrollView.h
#import
@interface liliLoopScrollView : UIScrollView
/** < 遵循协议 */@interface LoopScrollView : UIScrollView@property(nonatomic,strong) NSMutableArray * imageNameArr;/** < 播放的图片的名称数组 */
@property(nonatomic,strong) NSTimer * timer;/** < 计时器,用于实现自动播放 */
@property(nonatomic,assign) NSTimeInterval timerInterval;/** < 用于设置自动播放的时间间隔 */
@property(nonatomic,strong) UIPageControl * pageController;/** < 分页 控件 */
/** < 初始化ScrollView 第一个参数为其位置及大小 第二个参数为要加入的图片的名字数组,第三个参数为自动滚动运行的间隔时间 */
- (instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)arr timeInterval : (NSTimeInterval)timeInterval;
/** < 停止计时器,释放timer计时器 */
-(void)endTiming;
--------------------------------------------------------------------------
实现文件 liliLoopScrollView.m
#import "liliLoopScrollView.h"
@interface liliLoopScrollView ()
@property(nonatomic,strong) NSMutableArray * imageView;/** < 用于存放3个播放的图片视图 */
-(void)config;/** < 配置scrollView的基本设置 */
-(void)addImageViewToScrollView;/** < 将图片视图加入scrollView */
-(void)addImageToImageView;/** < 将图片加入imageView */
@end
@implementation liliLoopScrollView
- (instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)arr timeInterval:(NSTimeInterval)timeInterval
{
if (self = [super initWithFrame:frame]) {
self.timerInterval = timeInterval;
self.imageNameArr = arr;
[self addImageViewToScrollView];
[self addImageToImageView];
[self config];
/** < 启动计时器 */
self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:self.timerInterval];
NSLog(@"self.timerInterval : %lf",self.timerInterval);
}
return self;
}
#pragma marks - config
- (void)config
{
self.delegate = self;/** < 设置代理 */
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.bounces = NO;
self.userInteractionEnabled = YES;
self.contentSize = CGSizeMake(3 * self.bounds.size.width, self.bounds.size.height);
self.pagingEnabled = YES;
self.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);/** < 设置在中间视图 */
[self addSubview:self.pageController];
}
#pragma marks - addImageViewToScrollView
-(void)addImageViewToScrollView
{
for (int i = 0; i < 3; i ++) {
UIImageView * imageForView = [[UIImageView alloc] initWithFrame:CGRectMake(i * CGRectGetWidth(self.bounds), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
[self addSubview:imageForView];/** < 将视图加入scrollView中 */
[self.imageView addObject:imageForView];/** < 将视图加入用于播放的视图数组中 */
}
}
#pragma marks - addImageToimageView
- (void)addImageToImageView
{
int index = 0;
for (UIImageView * view in _imageView) {
view.image = [UIImage imageNamed:_imageNameArr[index]];
index ++;
}
}
#pragma marks - 实现协议方法,当滑动时,判断偏移量
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
/** < 左滑 */
if (self.contentOffset.x >= 2 * CGRectGetWidth(self.bounds)) {
NSString * firstImageName = [self.imageNameArr.firstObject mutableCopy];/** < 复制最后一张图片的名字 */
[self.imageNameArr removeObjectAtIndex:0];
[self.imageNameArr addObject:firstImageName];
/** < 配置分页控件 */
self.pageController.currentPage = self.pageController.currentPage == self.imageNameArr.count - 1 ? 0 : self.pageController.currentPage + 1;
/** < 右滑 */
}else if (self.contentOffset.x <= 0){
NSString * lastImageName = [self.imageNameArr.lastObject mutableCopy];
[self.imageNameArr removeLastObject];
[self.imageNameArr insertObject:lastImageName atIndex:0];
/** < 配置分页控件 */
self.pageController.currentPage = self.pageController.currentPage == 0 ? 7 : self.pageController.currentPage - 1;
}else{
return;
}
[self addImageToImageView];/** < 重新加载图片到图片视图中 */
self.contentOffset = CGPointMake(self.bounds.size.width, 0);/** < 还原偏移量至中间 */
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
/** < 开始拖动时,,暂停计时器 */
self.timer.fireDate = [NSDate distantFuture];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
/** < 停止拖动后,延迟timerInterval的时间开始计时 */
self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:_timerInterval];
}
/** < 停止计时器,释放timer计时器 */
-(void)endTiming
{
[self.timer invalidate];
}
#pragma marks - respondsToTimer
-(void)respondsToTimer
{
/** < 使用动画会造成一定的分页控件显示误差,就是慢一步 */
[UIView animateWithDuration:_timerInterval animations:^{
self.contentOffset = CGPointMake(2 * CGRectGetWidth(self.bounds), 0);
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
} completion:^(BOOL finished) {
}];
}
#pragma marks - getter
- (NSMutableArray *)imageView
{
if (!_imageView) {
_imageView =[NSMutableArray array];
}
return _imageView;
}
- (NSMutableArray *)imageNameArr
{
if (!_imageNameArr) {
_imageNameArr =[NSMutableArray array];
}
return _imageNameArr;
}
- (UIPageControl *)pageController
{
if (!_pageController) {
_pageController = [[UIPageControl alloc] init];
_pageController.bounds = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 30);
_pageController.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMaxY(self.bounds) - CGRectGetMidY(_pageController.bounds));
_pageController.numberOfPages = self.imageNameArr.count;
_pageController.currentPage = 0;
_pageController.currentPageIndicatorTintColor = [UIColor redColor];
_pageController.pageIndicatorTintColor = [UIColor lightGrayColor];
}
return _pageController;
}
- (NSTimer *)timer
{
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:_timerInterval target:self selector:@selector(respondsToTimer) userInfo:nil repeats:YES];
}
return _timer;
}
这是我慢慢码的,希望您尊重我的劳动成果哟,要转载请注明出处哈:
http://www.jianshu.com/writer#/notebooks/1273630/notes/1658950