IOS UIScrollView实现自动轮播图功能
来源:互联网 发布:网络直播内容策划表格 编辑:程序博客网 时间:2024/05/17 00:02
//// LoopScrollImageView.h// test07 实现自动轮播图效果//// Created by mouweng on 17/8/26.// Copyright © 2017年 mouweng. All rights reserved.//#import <UIKit/UIKit.h>@interface LoopScrollImageView : UIView <UIScrollViewDelegate> @property (nonatomic, retain) NSTimer * timer; //设置定时器 @property (nonatomic, retain) UIScrollView * scrollView; @property (nonatomic, retain) UIPageControl * pageControl; @property (nonatomic, retain) UILabel * currentPageLabel; @property (nonatomic, retain) NSArray * dataSource; - (id) initWithFrame:(CGRect)frame images:(NSArray *)images; - (void) startTimer; - (void) stopTimer;@end
//// LoopScrollImageView.m// test07 实现自动轮播图效果//// Created by mouweng on 17/8/26.// Copyright © 2017年 mouweng. All rights reserved.//#import "LoopScrollImageView.h"@implementation LoopScrollImageView//重写初始化方法- (id) initWithFrame:(CGRect)frame images:(NSArray *)images { if (self = [super initWithFrame:frame]) { self.dataSource = images; // 1. 初始化定时器 _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(loopImageTimer:) userInfo:nil repeats:YES]; //[timer setFireDate:[NSDate distantFuture]]; CGFloat viewWidth = CGRectGetWidth(self.frame); CGFloat viewHeigh = CGRectGetHeight(self.frame); NSUInteger pageCount = images.count; // 2. 初始化UIScrollView _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeigh)]; _scrollView.contentSize = CGSizeMake(viewWidth * pageCount, viewHeigh); _scrollView.showsVerticalScrollIndicator = NO; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.pagingEnabled = YES; _scrollView.delegate = self; for (int i = 0; i < images.count; i++) { UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(viewWidth * i, 0, viewWidth, viewHeigh)]; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.image = [UIImage imageNamed:images[i]]; imageView.tag = i; imageView.userInteractionEnabled = YES; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loopImageOnClicked:)]; [imageView addGestureRecognizer:tap]; [_scrollView addSubview:imageView]; } [self addSubview:_scrollView]; if (images.count > 1) { // 3. 初始化UIPageControl int pageControllHeight = 30; CGFloat scrollViewBottom = _scrollView.frame.origin.y + _scrollView.frame.size.height; _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, scrollViewBottom - pageControllHeight, 100, pageControllHeight)]; _pageControl.currentPage = 0;//当前页数所在的page位置 _pageControl.numberOfPages = pageCount; _pageControl.backgroundColor = [UIColor grayColor]; [self addSubview:_pageControl]; // 4. 初始化 当前页/总页数 _currentPageLabel = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth - 50, _pageControl.frame.origin.y, 50, pageControllHeight)]; _currentPageLabel.text = [NSString stringWithFormat:@"%d/%ld", 1, images.count]; [self addSubview:_currentPageLabel]; } } return self;}- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if ([scrollView isMemberOfClass:[UITableView class]]) { }else { int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width; //当前是第几个视图 _pageControl.currentPage = index; _currentPageLabel.text = [NSString stringWithFormat:@"%d/%ld", index + 1, self.dataSource.count]; }}//循环定时器设置#pragma mark - Actions- (void) loopImageTimer:(NSTimer *)timer { NSUInteger totalNum = self.dataSource.count; if (totalNum > 1) { CGPoint newOffset = _scrollView.contentOffset; newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame); if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (totalNum-1))) { newOffset.x = 0 ; } int index = newOffset.x / CGRectGetWidth(_scrollView.frame); //当前是第几个视图 newOffset.x = index * CGRectGetWidth(_scrollView.frame); _pageControl.currentPage = index; _currentPageLabel.text = [NSString stringWithFormat:@"%d / %ld",index+1, totalNum]; [_scrollView setContentOffset:newOffset animated:YES]; }else{ [_timer setFireDate:[NSDate distantFuture]];//关闭定时器 }}//点击事件- (void) loopImageOnClicked:(UITapGestureRecognizer *) tapGestureRecognizer{ NSInteger imageViewTag = tapGestureRecognizer.view.tag; NSLog(@"imageViewTag: ----------------- %ld", imageViewTag); // 0, 1, 2 }//开始和停止#pragma mark - Methods- (void) startTimer { [_timer setFireDate:[NSDate distantPast]];}- (void) stopTimer { [_timer setFireDate:[NSDate distantFuture]];}@end
//// ViewController.m// test07 实现自动轮播图效果//// Created by mouweng on 17/8/26.// Copyright © 2017年 mouweng. All rights reserved.//#import "ViewController.h"#import "LoopScrollImageView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.title = @"UIImage自动轮播"; NSArray * imageDataSource = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", nil]; // 只需初始化就可以实现效果 LoopScrollImageView * loopScrollImageView =[[LoopScrollImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 736) images:imageDataSource]; [self.view addSubview:loopScrollImageView];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
阅读全文
0 0
- IOS UIScrollView实现自动轮播图功能
- IOS 下划线随视图移动+自动轮播图功能
- iOS 利用UISCrollView作广告自动轮播效果
- UIScrollview实现左右循环滑动和自动轮播
- iOS实现图片自动轮播展示
- iOS开发 UI 自动轮播图
- [UIScrollview 卡片式无限自动轮播]
- viewPager 实现banner图 自动轮播
- 自动轮播图简单实现
- View pager实现自动轮播图
- okhttp+banner实现自动轮播图
- 自动轮播图
- UIScrollView实现自动轮播,可用于广告图片轮播
- VIewPager实现自动轮播
- jQuery实现轮播功能,及其在其基础上实现类似放大镜自动轮播功能
- bootstrap自动轮播图
- Banner自动轮播图
- ViewPager自动轮播图
- CodeForces
- HDU 6070 Dirt Ratio(二分+线段树)
- 飞扬23期总结
- [java]冒泡排序的常规、改进以及递归实现
- HDOJ1106 排序(字符串数字提取)
- IOS UIScrollView实现自动轮播图功能
- 浅析Linux命令之grep
- Hadoop 及 Hive 压缩应用
- 动脑学院笔记
- jQuery基础
- 归并排序--JAVA代码实现
- SWOT分析和PEST分析
- 理解c#中的闭包
- 洛谷P1141 01迷宫