0:首先还是通过纯的代码来实现
0:删除3个文件ViewController.h,ViewController.m,Main.storyboard
1:修改点击左边的蓝色按钮,然后选择general-》developer info-》main interface ,将这个main interface 晴空
2:然后再创建一个MainUIViewController ,它继承自UIViewController
1:Appdelegate.m
//
// AppDelegate.m
// TwelveRollingView
//
// Created by Kodulf on 16/10/28.
// Copyright © 2016年 Kodulf. All rights reserved.
//
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self setWindow:[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]];
[self.window makeKeyAndVisible];
[self.window setRootViewController:[[MainViewController alloc]init]];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
2:拷贝图片到
Asset.xcassets里面
这里的图片的下载地址:然后依次命名为1.jpg,2.jpg......
http://fdfs.xmcdn.com/group16/M08/F1/13/wKgDbFal40bR7Uc6AAH3JpWhLiQ015_android_large.jpg
http://fdfs.xmcdn.com/group10/M07/F0/14/wKgDaVal9ZLTP5q1AAFIJeYaktQ092_android_large.jpg
http://fdfs.xmcdn.com/group12/M07/E8/35/wKgDXFacqEfReClFAAFvbZHe_mU331_android_large.jpg
http://fdfs.xmcdn.com/group9/M05/EE/15/wKgDZlagtF_yH9YXAAEyq6YSxDo657_android_large.jpg
http://fdfs.xmcdn.com/group11/M07/FC/B4/wKgDbValyzzy0fBpAAMdsEAuI-Q295_android_large.jpg
http://fdfs.xmcdn.com/group9/M01/EF/02/wKgDZlaiCqbTzvIzAAH_l7MCT-k503_android_large.jpg
3:更新MainUIViewController.m
//
// MainViewController.m
// TwelveRollingView
//
// Created by 章银珊 on 16/10/28.
// Copyright © 2016年 章银珊. All rights reserved.
//
#import "MainViewController.h"
@interface MainViewController () <UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,strong)NSTimer *timer;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
CGFloat width = CGRectGetWidth(self.view.bounds);//获取屏幕的宽度
self.scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, width, 220)];//第二个参数要设置为20,否则会显示在顶部
[self.scrollView setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:_scrollView];
//设置分页效果
[self.scrollView setPagingEnabled:YES];
//隐藏水平滚动条
[self.scrollView setShowsHorizontalScrollIndicator:NO];
[self.scrollView setContentSize:CGSizeMake(width*5, 0)];
//设置代理
[self.scrollView setDelegate:self];
for (NSInteger i = 0;i<5;i++) {
//注意了UIImageView和UIImage是不同的
UIImageView *uimage=[[UIImageView alloc] initWithFrame:CGRectMake(width*i, 0, width, 220)];
[uimage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]]];
[_scrollView addSubview:uimage];
}
self.pageControl =[[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scrollView.frame)-20, width, 20)];
[self.pageControl setTintColor:[UIColor whiteColor]];
[self.pageControl setNumberOfPages:5];
[self.view addSubview:_pageControl];
//下面就需要设置定时器去实现轮播的效果
[self initTimer];
//考虑有多个scrollview的情况,涉及到线程,
//添加了scrollview2的时候,定时器会忽快忽慢
//解决办法,写在initTimer里面了
UIScrollView *scrollView2 = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.scrollView.frame)+40, width, 200)];
[scrollView2 setBackgroundColor:[UIColor blackColor]];
[scrollView2 setContentSize:CGSizeMake(width*5, 0)];
[scrollView2 setScrollEnabled:YES];
[self.view addSubview:scrollView2];
// Do any additional setup after loading the view.
}
-(void)scrollImage{
//不用麻烦,可以直接食用pagecontrol。而不用去NSInteger currentPage = self.scrollView.contentOffset.x/CGRectGetWidth()
NSInteger nextPage = [self.pageControl currentPage]+1;
if(nextPage%5==0){
nextPage = 0;
}
[self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.scrollView.frame)*nextPage, 0) animated:YES];
}
-(void) initTimer{
if(!self.timer){
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
//就是我们现在由于其他滚动视图的引入的导致和我们的定时器有冲突,获取当前的runloop和android 里面的handler 有点类似的感觉,首先添加timer到当前的runloop,然后设置mode为NSRunLoopCommonModes就可以了
//当我们使用NSTimer的scheduledTimerWithTimeInterval的方法创建的时候,实际上timer就会被加入到当前的线程的runloop当中,并且系统的默认的模式是NSDefaultRunLoopModes。
//而如果我们当前的线程,也就是ui主线程,由于某些的ui事件,比如说像这的scrollview的操作,就会将runloop自动切换为NSEventTrackingRunLoopMode的模式,然后在这个的过程中默认的模式注册的事件是不会被执行的,也就是说,使用此时使用scheduledTimerWithTimeInterval的添加到runloop中的timer 就不会被执行了,事件不会被执行,就会看到停顿的效果,当这种冲突结束的时候,我们的系统又会给我们把mode恢复回去,此时它有生效了,大家就会看到忽快忽慢,
//说白了,就是由于我们其他的滚动视图的产生,会导致我们runloop的模式发生变化,而导致我们自动调用的事件无法在runloop循环的过程中正确的被执行,
//所以要想把这个问题解决好,我们必须手动的把runloop给设定好了,让它去采用我们制定的模式,而不是任由它由其他视图的影响而导致我们的模式的变化。
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}
}
//为了解决手动的滑动和timer之前的冲突,首先是在开始拖拽的时候去掉,然后再在结束拖拽的时候添加
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self.timer invalidate];//首先让timer失效
[self setTimer:nil];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self initTimer];
}
//在这个方法里面去设置pageControl去显示哪一个图片
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self.pageControl setCurrentPage:(NSInteger)(scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame))];
}
- (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