The implementation of Image Carousel for IOS 12th lesson

Source: Internet
Author: User
Tags reserved



0: First, or through the pure code to achieve

0: Delete 3 files Viewcontroller.h,viewcontroller.m,main.storyboard

1: Modify click on the blue button on the left, then select general-"Developer info-" main interface, will this main interface clear sky

2: Then create a mainuiviewcontroller, it inherits from Uiviewcontroller

1:appdelegate.m

APPDELEGATE.M//Twelverollingview////Created by Kodulf on 16/10/28. COPYRIGHT©2016 year Kodulf.
All rights reserved.


#import "AppDelegate.h" #import "MainViewController.h" @interface appdelegate () @end @implementation appdelegate -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {//Ove
    Rride 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 was about to move F Rom Active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or the US
    Er quits the application and it begins the transition to the background state. // Use the To pause ongoing tasks, disable timers, and invalidate graphics rendering.
Games should to pause the game. }-(void) Applicationdidenterbackground: (uiapplication *) application {//use ' this ' to release shared resource s, save user data, invalidate timers, and store enough application state information to restore your to its CU
    Rrent state in case it is terminated later. If your application supports background execution, this 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 this were paused (or not ye T started) while the application is inactive. If the application was Previously in the background, optionally refresh the user interface. }-(void) Applicationwillterminate: (uiapplication *) application {//called when the application was about to Termina Te. Save data if appropriate.
Also applicationdidenterbackground:.
 } @end


2: Copy pictures to

Asset.xcassets inside

Here's the picture's download address: then named 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: Update mainuiviewcontroller.m

MAINVIEWCONTROLLER.M//Twelverollingview////Created by Zhang on 16/10/28. COPYRIGHT©2016 year Zhang.
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);//Get screen widths Self.scrollview =[[uiscrollview alloc]initwithframe:
    CGRectMake (0, width, 220)];//The second parameter is set to 20, otherwise it will appear at the top [Self.scrollview Setbackgroundcolor:[uicolor Graycolor]];
    [Self.view Addsubview:_scrollview];
    Set paging effect [Self.scrollview Setpagingenabled:yes];
    
    Hide horizontal scroll bar [Self.scrollview Setshowshorizontalscrollindicator:no]; [Self.scrollview setcontentsize:cgsizemake (width*5, 0)];
    
    Set agent [Self.scrollview setdelegate:self]; for (Nsinteger i = 0;i<5;i++) {//note Uiimageview and UIImage are different Uiimageview *uimage=[[uiimageview alloc] I
        Nitwithframe: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, WI
    DTH, 20)];
    [Self.pagecontrol Settintcolor:[uicolor Whitecolor]];
    [Self.pagecontrol Setnumberofpages:5];
    
    
    [Self.view Addsubview:_pagecontrol];
    
    The following need to set the timer to achieve the effect of the carousel [self inittimer]; Consider the case of multiple scrollview, involving threads,//adding SCROLLVIEW2 when the timer will suddenly and slowly//solution, written in Inittimer inside the uiscrollview *scrollview2 = [U
    Iscrollview 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 no additional setup after loading the view. }-(void) scrollimage{//No trouble, you can eat Pagecontrol directly. Instead of going to nsinteger currentpage = Self.scrollview.contentoffset.x/cgrectgetwidth () nsinteger nextPage = [Self.pageControl cu
    rrentpage]+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 SE
        Lector: @selector (scrollimage) Userinfo:nil Repeats:yes]; Is that we are now due to the introduction of other scrolling view and our timer conflict, get the current Runloop and Android inside the handler a bit similar feeling, first add a timer to the current Runloop, Then set mode to Nsrunloopcommonmodes on it//when we use the Nstimer Scheduledtimerwithtimeinterval method to create the
In fact, the timer will be added to the runloop of the current thread, and the default mode of the system is nsdefaultrunloopmodes.        And if our current thread, the UI main thread, will automatically switch runloop to Nseventtrackingrunloopmode mode because of some UI events, such as the scrollview of this. Then in this process the default mode registration event is not executed, that is, the timer added to the runloop using Scheduledtimerwithtimeinterval at this time will not be executed, the event will not be executed, You will see the effect of the pause, when this conflict is over, our system will again give us the mode back, at this time it is effective, we will see the fast and slow,//plainly, because our other scrolling view of the production, will lead to our Runloop mode change,
        The events that cause us to invoke automatically cannot be executed correctly in the process of runloop loops,//So to solve this problem, we have to manually set up the Runloop, let it take our model, rather than allow it to be influenced by other views to lead to changes in our patterns.
    [[Nsrunloop Currentrunloop]addtimer:self.timer formode:nsrunloopcommonmodes]; }//To resolve manual sliding and timer conflicts before, first of all, in the beginning of the drag and drop, and then add at the end of the drag-(void) scrollviewwillbegindragging: (Uiscrollview *)

scrollview{[Self.timer invalidate];//First let timer fail [self settimer:nil];}


                  
                  
                  
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{[self inittimer];} In this method, set Pagecontrol to show which picture-(void) Scrollviewdidscroll: (Uisc Rollview *) scrollview{[Self.pagecoNtrol setcurrentpage: (Nsinteger) (Scrollview.contentoffset.x/cgrectgetwidth (Scrollview.frame))];
    }-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated. }/* #pragma mark-navigation//In a storyboard-based application, you'll often want to do a little preparation Re Navigation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller US
    ing [Segue Destinationviewcontroller].
Pass the selected object to the new view controller.
 } * * @end




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.