话不多说,直接上代码
//
// ViewController.m
// huanyingye
//
// Created by 刘祺旭 on 15/8/12.
// Copyright (c) 2015年探花. All rights reserved.
//
#import "ViewController.h"
#define Page_Number (5)
#define Time_Sec (2)
@interface ViewController ()
{
int _page;
}
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) UIScrollView *playScrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
_page =0;
self.playScrollView = [[UIScrollViewalloc] initWithFrame:self.view.frame];
self.playScrollView.backgroundColor = [UIColorclearColor];
self.playScrollView.contentSize =CGSizeMake([UIScreenmainScreen].bounds.origin.x, [UIScreenmainScreen].bounds.size.height*Page_Number);
self.playScrollView.pagingEnabled =YES;
self.playScrollView.delegate =self;
NSArray *color = [NSArrayarrayWithObjects:[UIColorredColor], [UIColororangeColor],[UIColoryellowColor],[UIColorgreenColor],[UIColorblueColor],nil];
for (int i =0; i < Page_Number; i++) {
UIImageView *imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0, [UIScreenmainScreen].bounds.size.height*i, [UIScreenmainScreen].bounds.size.width, [UIScreenmainScreen].bounds.size.height)];
imageView.backgroundColor = color[i];
[self.playScrollViewaddSubview:imageView];
}
[self.viewaddSubview:self.playScrollView];
[selfaddTimer];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
* 开启定时器
*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if ([scrollViewisEqual:self.playScrollView]) {
// 开启定时器
[selfaddTimer];
}
}
/**
* 开启定时器
*/
- (void)addTimer{
self.timer = [NSTimerscheduledTimerWithTimeInterval:Time_Sectarget:selfselector:@selector(nextImage)userInfo:nilrepeats:YES];
[[NSRunLoopcurrentRunLoop] addTimer:self.timerforMode:NSRunLoopCommonModes];
}
/**
* 切换图片
*/
- (void)nextImage
{
if (_page ==Page_Number) {
}else{
_page++;
// 滚动scrollview
CGFloat y =_page * self.playScrollView.frame.size.height;
self.playScrollView.contentOffset =CGPointMake(0, y);
}
}
/**
* 滚动中调用
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint offset = scrollView.contentOffset;
if (offset.y <0 ) {
// 自己做操作
} elseif (offset.y >=_page * [UIScreenmainScreen].bounds.size.height) {
// 自己做操作
} else {
_page = offset.y / [UIScreenmainScreen].bounds.size.height;
// 自己改效果
}
}
/**
* 关闭定时器
*/
- (void)removeTimer
{
[self.timerinvalidate];
}
/**
* 开始拖拽的时候调用
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if ([scrollViewisEqual:self.playScrollView]) {
[selfremoveTimer];
}
}
@end