//// ViewController.m// svc//// Created by sjf on 16/9/9.// Copyright © 2016年 sjf. All rights reserved.//#import "ViewController.h"#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)@interface ViewController (){
// UIScrollView 以及数据
UIScrollView *_scrollView;
UIPageControl *_pageControll;
NSArray *imgarr;
}
// 定时器
@property (nonatomic, strong) NSTimer *timer;
// 数量
@property (nonatomic,assign) NSUInteger count;
//位移
@property (nonatomic,assign) CGFloat oldContentOffsetX;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 193)];
[self.view addSubview:_scrollView];
_scrollView.backgroundColor =[UIColor whiteColor];
_pageControll =[[UIPageControl alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50,_scrollView.frame.size.height-30, 100, 30)];
[self.view addSubview:_pageControll];
[self afn];
}
#pragma mark 请求UIScrollView的数据图片
-(void)afn{
// 做请求 添加数据
[self addscrollview];
}
#pragma mark 设置UIScrollView滚动条
-(void)addscrollview{
// 图片的数量 可以根据上面的 计算
self.count = 5;
// 设置控制器成为代理
_scrollView.delegate = self;
for (int i = 0; i < self.count+1; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, 193)];
imageView.tag =50+i;
imageView.backgroundColor =[UIColor redColor];
NSString *imageName;
if (i == self.count) {
imageName = [NSString stringWithFormat:@"%d",1];
}else
{
imageName = [NSString stringWithFormat:@"%d",i + 1];
}
imageView.image = [UIImage imageNamed:imageName];
[_scrollView addSubview:imageView];
}
_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * (self.count + 1), 0);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_pageControll.numberOfPages = self.count;
_pageControll.currentPage = 0;
_pageControll.pageIndicatorTintColor = [UIColor orangeColor];
// 设置 当前页码指示器颜色
_pageControll.currentPageIndicatorTintColor = [UIColor redColor];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doDoubleTap:)];//默认点击第一张
//添加点击手势
[_scrollView addGestureRecognizer:doubleTap];
[self startTimer];
}
#pragma mark 手势tap
-(void)doDoubleTap:(UITapGestureRecognizer *)tap{
CATransition *animation = [CATransition animation];
//创建一个转场动画。
animation.duration = 0.8;
//动画时间
animation.subtype = kCATransitionFromTop;
//动画方向
// animation.type = @"pageCurl";
animation.type = @"rippleEffect";
[tap.view.layer addAnimation:animation forKey:@"aaa"];
NSLog(@"%ld",_pageControll.currentPage);
}
#pragma mark 给uiscrollview添加定时器
- (void)startTimer {
_timer = [NSTimer scheduledTimerWithTimeInterval:3
target:self
selector:@selector(changeImage)
userInfo:nil
repeats:YES];
// 调整timer 的优先级
NSRunLoop *mainLoop = [NSRunLoop mainRunLoop];
[mainLoop addTimer:_timer forMode:NSRunLoopCommonModes];
}
#pragma mark 定时器的事件
- (void)changeImage {
[_scrollView setContentOffset:CGPointMake((_pageControll.currentPage+1)*SCREEN_WIDTH, 0) animated:YES];
}
- (void)stopTimer
{
[self.timer invalidate];
self.timer = nil;
}
#pragma mark uiscrollerview 的滚动方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if(scrollView==_scrollView){
CGPoint point = scrollView.contentOffset;
BOOL isRight = self.oldContentOffsetX < point.x;
self.oldContentOffsetX = point.x;
// 开始显示最后一张图片的时候切换到第二个图
if (point.x > SCREEN_WIDTH*(self.count-1)+SCREEN_WIDTH*0.5 && !self.timer) {//从最后一个图片会到第一个图片
_pageControll.currentPage = 0;
}else if (point.x > SCREEN_WIDTH*(self.count-1) && self.timer && isRight){
_pageControll.currentPage = 0;
}else{
_pageControll.currentPage = (point.x + SCREEN_WIDTH*0.5) / SCREEN_WIDTH;
}
// 开始显示第一张图片的时候切换到倒数第二个图
if (point.x >= SCREEN_WIDTH*self.count) {
//kScrollViewWidth+point.x-kScrollViewWidth*(self.count+1)
[_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}else if (point.x < 0) {
[scrollView setContentOffset:CGPointMake(point.x+SCREEN_WIDTH*(self.count), 0) animated:NO];
}
}
}
#pragma mark 计时器停止
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (scrollView==_scrollView) {
[self stopTimer];
}
}
#pragma mark 手指离开屏幕 计时器开始工作
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if(scrollView==_scrollView){
[self startTimer];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end