Design the tag selector titleswitch

Source: Internet
Author: User

Design the tag selector titleswitch

The effect is as follows:

The source code is as follows:

Titleswitch. h and titleswitch. m

//// Titleswitch. h // titleswitch /// created by youxianming on 14/11/4. // copyright (c) 2014 youxianming. all rights reserved. // # import <uikit/uikit. h> @ protocol titleswitchdelegate <nsobject> @ optional-(void) willselectindex :( nsinteger) index;-(void) didselectindex :( nsinteger) index; @ end @ interface titleswitch: uiview/*** Protocol */@ property (nonatomic, assign) ID <titleswitchdelegate> delegate;/*** as the button title */@ property (nonatomic, strong) nsarray * titles;/*** line width */@ property (nonatomic, assign) cgfloat linewidth;/*** line color */@ property (nonatomic, strong) uicolor * linecolor;/*** title Font */@ property (nonatomic, strong) uifont * titlefont;/*** normal title Color */@ property (nonatomic, strong) uicolor * normaltitlecolor;/*** color of the selected title */@ property (nonatomic, strong) uicolor * selectedtitlecolor; /*** only one button can be used to trigger the animation effect at a time */@ property (nonatomic, assign) bool cantouchonlybuttononetime; /*** highlight effect & Highlight color when the Enable button is clicked */@ property (nonatomic, assign) bool enablebuttontitlehighlighted; @ property (nonatomic, strong) uicolor * highlightedtitlecolor; /*** create the view of titleswitch */-(void) createtitleswitchview; @ end
//// Titleswitch. M // titleswitch /// created by youxianming on 14/11/4. // copyright (c) 2014 youxianming. all rights reserved. // # import "titleswitch. H "typedef Enum: nsuinteger {normal_button = 0x11, line_view = 0x1122,} enum_viewtag; @ implementation titleswitch-(void) createtitleswitchview {// if there is no title, if (_ titles. count = 0) {return;} // obtain the cgfloat framewidth = self. bounds. size. width; Cgfloat frameheight = self. bounds. size. height; // calculate the width & height of the button cgfloat buttonwidth = framewidth/_ titles. count; cgfloat buttonheight = 0; cgfloat defaultlinewidth = 2.f; if (_ linewidth = 0) {buttonheight = frameheight-defaultlinewidth; // The default line occupies one pixel} else {buttonheight = frameheight-_ linewidth;} // Initialize all buttons for (INT I = 0; I <_ titles. count; I ++) {uibutton * button = [[uibutton alloc] ini Twithframe: cgrectmake (buttonwidth * I, 0, buttonwidth, buttonheight)]; button. tag = normal_button + I; [self addsubview: button]; [Button settitle: _ titles [I] forstate: uicontrolstatenormal]; // normal color if (I = 0) {[self selectbuttonstyle: button];} else {[self normalbuttonstyle: button];} // If (_ enablebuttontitlehighlighted = Yes & _ highlightedtitlecolor) {[Button settitlecolor: _ highlighted Titlecolor forstate: uicontrolstatehighlighted];} // Add event [Button addtarget: Self action: @ selector (buttonsevent :) forcontrolevents: uicontroleventtouchupinside]; // set the font if (_ titlefont) {button. titlelabel. font = _ titlefont ;}// initialize the horizontal view uiview * lineview = [[uiview alloc] initwithframe: cgrectmake (0, buttonheight, buttonwidth, (_ linewidth = 0? Defaultlinewidth: _ linewidth)]; lineview. tag = line_view; [self addsubview: lineview]; If (_ linecolor) {lineview. backgroundcolor = _ linecolor;} else {lineview. backgroundcolor = [uicolor redcolor] ;}}/*** button event ** @ Param button: touch the button in the event */-(void) buttonsevent :( uibutton *) button {// obtain lineview uiview * lineview = [self viewwithtag: line_view]; // which button nsinteger whichbutton = button. tag-normal_button; // calculate the width and height of the button cgfloat framewidth = self. bounds. size. width; cgfloat buttonwidth = framewidth/_ titles. count; [[self subviews] enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * Stop) {uibutton * TMP = (uibutton *) OBJ; If ([TMP iskindofclass: [uibutton class]) {If (TMP = button) {[self selectbuttonstyle: TMP];} else {[self normalbuttonstyle: TMP] ;}}]; // perform the animation if (_ cantouchonlybuttononetime = Yes) {self. userinteractionenabled = no;} If (_ delegate & [_ delegate respondstoselector: @ selector (willselectindex :)]) {[_ delegate willselectindex: latest];} [uiview failed: 0.25f animations: ^ {cgrect rect = lineview. frame; rect. origin. X = whichbutton * buttonwidth; lineview. frame = rect;} completion: ^ (bool finished) {If (_ cantouchonlybuttononetime = Yes) {self. userinteractionenabled = yes;} If (_ delegate & [_ delegate respondstoselector: @ selector (didselectindex :)]) {[_ delegate didselectindex: whichbutton] ;}}];} /*** selected button style ** @ Param button */-(void) selectbuttonstyle :( uibutton *) button {If (_ normaltitlecolor) {[Button settitlecolor: _ normaltitlecolor forstate: uicontrolstatenormal];} else {[Button settitlecolor: [uicolor redcolor] forstate: uicontrolstatenormal];} /*** normal button style ** @ Param button */-(void) normalbuttonstyle :( uibutton *) button {If (_ selectedtitlecolor) {[Button settitlecolor: _ selectedtitlecolor forstate: uicontrolstatenormal];} else {[Button settitlecolor: [uicolor colorwithred: 0.369 Green: 0.369 Blue: 0.369 ALPHA: 1] forstate: uicontrolstatenormal];} @ end

Usage:

//// Viewcontroller. M // titleswitch /// created by youxianming on 14/11/4. // copyright (c) 2014 youxianming. all rights reserved. // # import "viewcontroller. H "# import" titleswitch. H "@ interface viewcontroller () <titleswitchdelegate> @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; titleswitch * titleswitch = [[titleswitch alloc] initwithframe: cgrectmake (0,100,320, 40)]; titleswitch. titles = @ [@ "youxianming", @ "nozuonodie", @ "blueshit"]; titleswitch. titlefont = [uifont systemfontofsize: 15.f]; titleswitch. linewidth = 1.f; titleswitch. cantouchonlybuttononetime = yes; titleswitch. delegate = self; [titleswitch createtitleswitchview]; [self. view addsubview: titleswitch];}-(void) willselectindex :( nsinteger) index {nslog (@ "willselectindex % lD", (long) index);}-(void) didselectindex :( nsinteger) index {nslog (@ "didselectindex % lD", (long) index) ;}@ end

Note:

Design the tag selector titleswitch

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.