- 创建UIScrollView UIPageControl
- 添加定时器
- 手动滑动与自动滑动的互相转换(UIScrollViewDelegate)
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #31595d}p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Heiti SC Light'; color: #78492a}p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Heiti SC Light'; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s6 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s9 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s10 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s11 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s12 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s13 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s14 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}
#import "ViewController.h"
// 宏定义
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
// 数量 隔离状态栏 pageControl宽度
CGFloat ImageCount = 10;
CGFloat scrollY = 20;
CGFloat pageControlWidth = 200;
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) UIScrollView * scrollView;
@property(nonatomic, strong) UIPageControl * pageControl;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 初始化ScrollView PageControl NSTimer
[self initScrollView];
[self initPageControl];
[self addTimer];
}
#pragma mark - 创建UIScrollView
- (void)initScrollView{
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, scrollY, screenWidth, screenHeight - scrollY)];
self.scrollView.delegate = self;
for (int i = 0; i < ImageCount; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(screenWidth * i, scrollY, screenWidth, screenHeight - scrollY)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"huoying%d.jpg",i+1]];
if (i == 7) {
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"huoying8.png"]];
}
[self.scrollView addSubview:imageView];
}
self.scrollView.contentSize = CGSizeMake(screenWidth * ImageCount, screenHeight - 20);
self.scrollView.pagingEnabled = YES;
[self.view addSubview:self.scrollView];
}
#pragma mark - 创建UIPageControl
- (void)initPageControl{
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((screenWidth - pageControlWidth)/2, screenHeight - scrollY, pageControlWidth, scrollY)];
self.pageControl.numberOfPages = ImageCount;
self.pageControl.pageIndicatorTintColor = [UIColor greenColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
#warning 注意 这里 一定要插到最上面的视图上面才能够显示
[self.view insertSubview:self.pageControl aboveSubview:self.scrollView];
}
- (void)addTimer{
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
#warning 加入Runloop 消息机制 只有把NSTimer加入Runloop才能被调用
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
#pragma mark - NNSTimer调用的方法 获取当前页码 +1 如果等于图片总数量 回归0 设置偏移量
- (void)nextPage{
NSInteger page = self.pageControl.currentPage;
page++;
if (page == ImageCount) {
page = 0;
}
NSLog(@"page为%ld",(long)page);
CGPoint point = CGPointMake(screenWidth * page, 0);
[self.scrollView setContentOffset:point animated:YES];
}
#pragma mark - UIScrollView Delegate 结束滚动获取偏移量计算出页码 赋值给PageControl
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSInteger page = scrollView.contentOffset.x/screenWidth;
self.pageControl.currentPage = page;
}
#pragma mark - 视图将要拖动的时候,处理手动拖动与自动滚动的冲突
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
// 关掉定时器
[self removeTimer];
NSLog(@"brginDragging");
}
// 停止拖拽 重新开始定时器
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// 采用GCD的方式 在视图停止拖拽后的2秒之后 重新添加定时器
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self addTimer];
});
NSLog(@"endDragging");
}
#pragma mark - 移除定时器
- (void)removeTimer{
[self.timer invalidate];
self.timer = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end```
###效果
![picture.gif](http:https://img.haomeiwen.com/i189984/83c61df7a717c9f2.gif?imageMogr2/auto-orient/strip)
网友评论