Add category to uiscrollview to achieve the carousel Effect of uiscrollview
As we all know, to add attributes to the category, you must use runtime. In this tutorial, you have added several attributes to the uiscrollview, which are also implemented through runtime.
The effect after implementation is as follows:
The source code of uiscrollview category is:
Uiscrollview + Yx. H + uiscrollview + Yx. m
/// Uiscrollview + Yx. h // pageview // copyright (c) 2014 y. x. all rights reserved. // # import <uikit/uikit. h> # import "yxgcd. H "@ interface uiscrollview (Yx) @ property (nonatomic, strong) nsnumber * currentpage; // the current page number @ property (nonatomic, strong) nsnumber * largestpage; // maximum page number @ property (nonatomic, strong) nsnumber * timerinterval; // time interval @ property (nonatomic, strong) gcdtimer * timer; // timer-(void) start; @ end
/// Uiscrollview + Yx. M // pageview // copyright (c) 2014 y. x. all rights reserved. // # import "uiscrollview + Yx. H "# import <objc/runtime. h> # define animation_duration 0.2 @ implementation uiscrollview (Yx) Static char timerflag;-(void) settimer :( gcdtimer *) timer {timer (self, & timerflag, nil, timer ); objc_setassociatedobject (self, & timerflag, timer, objc_assoc Signature);}-(gcdtimer *) timer {return objc_getassociatedobject (self, & timerflag);} static char currentpageflag;-(void) setcurrentpage :( nsnumber *) currentpage {aggregate (self, & currentpageflag, nil, objc_association_retain_nonatomic); objc_setassociatedobject (self, & currentpageflag, currentpage, callback);}-(nsnumber *) currentpage {return objc _ Getassociatedobject (self, & currentpageflag);} static char largestpageflag;-(void) setlargestpage :( nsnumber *) largestpage {vertex (self, & largestpageflag, nil, callback ); objc_setassociatedobject (self, & largestpageflag, largestpage, callback);}-(nsnumber *) largestpage {return objc_getassociatedobject (self, & largestpageflag);} static Char timerintervalflag;-(void) settimerinterval :( nsnumber *) timerinterval {values (self, & timerintervalflag, nil, interval); values (self, & timerintervalflag, timerinterval, interval );} -(nsnumber *) timerinterval {return objc_getassociatedobject (self, & timerintervalflag);}-(void) Start {If (self. currentpage! = Nil & self. largestpage! = Nil & self. timerinterval! = Nil & self. timer! = Nil) {_ weak uiscrollview * weakobj = self; [self. timer event: ^ {If (Ceil (weakobj. contentoffset. x/weakobj. bounds. size. width) = weakobj. contentoffset. x/weakobj. bounds. size. width) {weakobj. currentpage = [nsnumber numberwithint: weakobj. contentoffset. x/320.f]; [uiview animatewithduration: animation_duration animations: ^ {cgpoint point = weakobj. contentoffset; weakobj. currentpage = [nsnumber numberwithint: [weakobj. currentpage intvalue] + 1]; point. X = ([weakobj. currentpage intvalue] % [weakobj. largestpage intvalue]) * weakobj. bounds. size. width; weakobj. contentoffset = point;}] ;}} timeinterval: nsec_per_sec * [self. timerinterval floatvalue]; [[gcdqueue mainqueue] execute: ^ {[weakobj. timer start];} afterdelay: nsec_per_sec * [self. timerinterval floatvalue];} else {nslog (@ "Please configure parameters, :)");} @ end
Source code used in the main function:
Rootviewcontroller. m
//// Rootviewcontroller. M // pageview // copyright (c) 2014 y. x. all rights reserved. // # import "rootviewcontroller. H "# import" yxgcd. H "# import" uiscrollview + Yx. H "@ interface rootviewcontroller () <uiscrollviewdelegate> @ property (nonatomic, strong) gcdtimer * timer; @ property (nonatomic, assign) nsinteger currentpage; @ property (nonatomic, assign) nsinteger largestpage; @ end @ implementation rootviewcontroller-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor blackcolor]; // data source nsarray * array = @ [@ "youxianming", @ "Qiuliang", @ "linken", @ "kelindun"]; // initialize uiscrollview * rootview = [[uiscrollview alloc] initwithframe: cgrectmake (0, 0,320,320)]; rootview. pagingenabled = yes; rootview. contentsize = cgsizemake (320 * array. count, 320); [self. view addsubview: rootview]; // load the control [array enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * Stop) {uilabel * TMP = [[uilabel alloc] initwithframe: cgrectmake (idx x 320, 0,320,320)]; TMP. TEXT = OBJ; TMP. layer. borderwidth = 2.f; TMP. textcolor = [uicolor cyancolor]; TMP. font = [uifont fontwithname: @ "helveticaneue-thin" Size: 30]; TMP. textalignment = nstextalignmentcenter; TMP. backgroundcolor = [uicolor colorwithred: arc4random () % 100/100. f green: arc4random () % 100/100. f Blue: arc4random () % 100/100. f ALPHA: 0.5f]; [rootview addsubview: TMP] ;}]; // sets the parameter value and starts to carousel rootview. timer = [[gcdtimer alloc] initinqueue: [gcdqueue mainqueue]; rootview. currentpage = [nsnumber numberwithint: 0]; rootview. largestpage = [nsnumber numberwithint: array. count]; rootview. timerinterval = [nsnumber numberwithint: 4]; [rootview start];} @ end
The following describes the design ideas in detail:
The timer is used to implement the timer for carousel. This is the minimum condition :)
For how to add the runtime attribute, Baidu brain supplement or find the author's previous tutorial article :)
Note the following details about the start method:
The core of the design is as follows: