IOS-night Mode ktjnightversion

Source: Internet
Author: User

Ktjnightversion: Quickly deploy night mode. When the use of dknightversion to deploy the night mode to find some problems, because the project is relatively urgent, so according to dknightversion the idea of re-implemented, and strengthen some functions. First completed the test, due to the development and testing of the urgency, so there are some hidden bugs are unavoidable, please correct me. (GitHub, Blog Park) Ktjnightversion Overall Design: 1, mode switching use notification mode for broadcasting, monitoring the broadcast for each viewcontroller, the Viewcontroller respectively in Viewwillappear,     Register for monitoring and delete monitoring in viewdiddisappear. 2, for each view, and its sub-class to provide a common second mode (night) color settings, and through the runtime saved in the object.     At the same time, if the method provided does not meet the requirements, it can be implemented on its own (demo). 3, Viewcontroller through the mode switching monitoring, and then call the corresponding protocol (KTJNIGHTVERSIONCHANGECOLORPROTOCOL) to the color switch settings.     Also in the Viewwillappear: method, the setting is set by default.      4. When the view is set, the mode switch of the display view is done by recursively traversing the ViewController.view.subViews. Design Purpose: 1, in order to reduce the number of listeners in the mode switch, and all need to refresh the UI in the main thread (although the UI can be refreshed in some way not in the main thread), so the refreshed UI through the Viewcontroller division, if the view is not displayed at this time, The listener is removed from the Viewcontroller and the UI is not refreshed.     However, to make the UI appear normal, the UI is set by default in Viewwillappear:. 2, for ease of deployment, easy to set up, so use the runtime mechanism for deployment, you can directly set up, do not need to inherit the operation.        Instructions:      Step 1: To reduce the complexity of iterating through the child views and to control the night mode flexibly, the registration method is provided. Only registered changes will be made when the mode is switched. Registration method:         [Ktjnightversion addclasstoset:cell.textlabel.class];         ps. You can try initialize to register.       Step 2: Set night mode, Normal mode style           cell.textlabel.ktj_ Normaltextcolor = [uicolorgraycolor];          cell.textlabel.ktj_ Nighttextcolor = [Uicolor whitecolor];                or:           cell.textlabel.textcolor = [uicolorgraycolor];           cell.textlabel.ktj_nighttextcolor = [Uicolor whitecolor];     Step 3: Mode switching                 Switch to normal mode           [ ktjnightversionchangetonormal];          &NBsp     Switch to night mode           [ktjnightversion changetonight];      Step 4: Run it, OK?!   Appendix: First, self-implementation Example 1, comply with KTJNIGHTVERSIONCHANGECOLORPROTOCOL protocol, implementation-(BOOL) Ktj_changecolorwithanimation: (bool) animation du Ration: (cgfloat) duration; Method, Method mode:if the parent class does not implement the Protocol, the implementation is:
-(BOOL) Ktj_changecolorwithanimation: (bool) Animation duration: (cgfloat) Duration {if ([ktjnightversion        Shouldchangecolor:self]) {Ktjnightversionstyle style = [Ktjnightversion currentstyle];        Uicolor *backgroundcolor; switch (style) {Case Ktjnightversionstylenormal:backgroundcolor = Self.ktj_normalbackgroudcolo                R                            Break                Case ktjnightversionstylenight:backgroundcolor = Self.ktj_nightbackgroudcolor;                            Break                Default:backgroundcolor = Self.backgroundcolor;        Break        } jgweak (self);        void (^changecolor) (void) = ^ (void) {[weakself ktjhook_setbackgroundcolor:backgroundcolor];        };        if (animation) {[UIView animatewithduration:duration animations:changecolor];        } else {changecolor ();    } return YES; } else {return NO;    }}  

if the parent class has an implementation, use the following:
-(BOOL) Ktj_changecolorwithanimation: (bool) Animation duration: (cgfloat) Duration {if ([Super Respondstoselector: @se Lector (ktj_changecolorwithanimation:duration:)]) {if ([Super Ktj_changecolorwithanimation:animation Duration:durat            Ion]) {Ktjnightversionstyle style = [Ktjnightversion currentstyle];            Uicolor *separatorcolor; switch (style) {Case Ktjnightversionstylenormal:separatorcolor = Self.ktj_normalsepara                    Torcolor;                                    Break                    Case ktjnightversionstylenight:separatorcolor = Self.ktj_nightseparatorcolor;                                    Break                    Default:separatorcolor = Self.separatorcolor;            Break            } jgweak (self);            void (^changecolor) (void) = ^ (void) {[weakself ktjhook_setseparatorcolor:separatorcolor];            }; if (animation) {[UIView animatewithduration:duration animations:changecolor];            } else {changecolor ();        } return YES; }} return NO;

2. Save the properties of the design night mode by yourself. Also refer to:
+ (void) load {static dispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{ktjchangeimp (@selector (setseparatorcolor:), @selector (Ktjhook_setseparatorcolor    :)); });}    -(void) Ktjhook_setseparatorcolor: (Uicolor *) separatorcolor {self.ktj_normalseparatorcolor = SeparatorColor; [Self ktjhook_setseparatorcolor:separatorcolor];} -(void) Setktj_nightseparatorcolor: (Uicolor *) Ktj_nightseparatorcolor {if ([ktjnightversion currentstyle] = = KTJNight    Versionstylenight) {[Self ktjhook_setseparatorcolor:ktj_nightseparatorcolor]; } objc_setassociatedobject (Self, @selector (ktj_nightseparatorcolor), Ktj_nightseparatorcolor, Objc_association_ret AIN);} -(Uicolor *) Ktj_nightseparatorcolor {return objc_getassociatedobject (self, @selector (Ktj_nightseparatorcolor))?: Self.separatorcolor;} -(void) Setktj_normalseparatorcolor: (Uicolor *) Ktj_normalseparatorcolor {if ([ktjnightversion currentstyle] = = KTJNig Htversionstylenormal) {[Self ktjhook_setseparAtorcolor:ktj_normalseparatorcolor]; } objc_getassociatedobject (Self, @selector (Ktj_normalseparatorcolor))?: Self.separatorcolor;} -(void) Ktj_savenormalseparatorcolor: (Uicolor *) Separatorcolor {objc_setassociatedobject (self, @selector (ktj_ Normalseparatorcolor), Separatorcolor, Objc_association_retain);}

Reprint please keep this link: http://www.cnblogs.com/madordie/p/4716396.html

IOS-night Mode ktjnightversion

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.