#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewVC = [[ViewController alloc]init];
self.window.rootViewController =viewVC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
@end
#import "ViewController.h"
#import "CellView.h"
#import "UIscrollView.h"
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)NSTimer *Timer; //让视图自动播放
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
//滚动范围
scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*3,CGRectGetHeight(self.view.frame));
//隐藏滚动条
scrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:scrollView];
//创建三个页面加三张图
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"One.png",@"Two.png",@"Three.png", nil];
for (int i =0 ; i<3; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
[imageView setImage:[UIImage imageNamed:array[i]]];
[scrollView addSubview:imageView];
}
//设置tag值
scrollView.tag = 1000;
//设置滚动视图代理
scrollView.delegate = self;
//启动定时器
self.Timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
}
-(void)timeAction{
//得到滚动视图
UIScrollView *scrollView =(UIScrollView *)[self.view viewWithTag:1000];
//通过偏移量来切换滚动视图子页面
float offset = scrollView.contentOffset.x;
//每次切换一个屏幕界面
offset +=CGRectGetWidth(self.view.frame);
//判断是否是最后一个子视图
if (offset > 2*CGRectGetWidth(self.view.frame)) {
offset = 0;
}
//得到最终的偏移量
CGPoint resultPoint = CGPointMake(offset, 0);
//切换视图的时候带动画效果
//从最后一张回到第一张的时候,不带动画效果
if (offset ==0) {
[scrollView setContentOffset:resultPoint animated:NO];
}else{
[scrollView setContentOffset:resultPoint animated:YES];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end