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.jpghttp://fdfs.xmcdn.com/group10/M07/F0/14/wKgDaVal9ZLTP5q1AAFIJeYaktQ092_android_large.jpghttp://fdfs.xmcdn.com/group12/M07/E8/35/wKgDXFacqEfReClFAAFvbZHe_mU331_android_large.jpghttp://fdfs.xmcdn.com/group9/M05/EE/15/wKgDZlagtF_yH9YXAAEyq6YSxDo657_android_large.jpghttp://fdfs.xmcdn.com/group11/M07/FC/B4/wKgDbValyzzy0fBpAAMdsEAuI-Q295_android_large.jpghttp://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