First on:
This program is divided into 2 levels, one is the top of the bar with Uitextfield, one is the drop-down selected view, the drop-down selected view with 4 of their own definition of UIView
We first define a uiviewcontroller called Myviewcontroller, then the top bar called Topbarview, pull down the selected view called Typeselectview. Like UIButton's own definition of view is called Typeview
There are two states of typeview, assuming that the item that the finger touches is the selected state. So Typeselectview should have a property that indicates which view is currently selected, and Typeview has a property called whether or not it is selected.
The drop-down box has two states, the drop-down and the display. So Typeselectedview has a property that says what kind of state he's in today.
First to write Typeview:
#define Typeview_width 76#define typeview_height 76@class typeselectview; @interface Typeview:uiview@property ( Nonatomic, assign) int typeId; @property (nonatomic, assign) BOOL bselected; @property (nonatomic,strong) Typeselectview * Typesview; @end
Touch event:
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{ if (!_bselected) {//Assuming that the touch is not selected. Then we are going to cancel the selection of the currently selected item in Typeselectview if (_typesview.curselectedview) { _ typesView.curSelectedView.bSelected = NO; [_typesview.curselectedview Setneedsdisplay]; } _bselected = YES; _typesview.curselectedview = self; [Self setneedsdisplay];} }
Then draw:
-(void) DrawRect: (cgrect) rect{cgcontextref context = Uigraphicsgetcurrentcontext (); Cgcontextsetfillcolorwithcolor (context, [Ccommon rgbcolorfromhexstring:@ "#c5c4cc" alpha:1.0f]. Cgcolor); Cgfontref Contextfont = Cgfontcreatewithfontname ((cfstringref) [Uifont Systemfontofsize:14].fontname]; Cgcontextsetfont (context, contextfont); Cfrelease (Contextfont); Cgcontextsetfontsize (context, 14.0); if (_bselected) {//Displays the background of the select uiimage* bgimage = [UIImage imagenamed:@ "Change_icon_touch_bg.png"]; CGRect bgRc = rect; bgrc.origin.x = (bgrc.size.width-bgimage.size.width)/2+1.0f; Find the x-coordinate of the upper-left corner of the image of the background image bgrc.origin.y = (bgrc.size.height-bgimage.size.height)/2; <span style= "font-family:arial, Helvetica, Sans-serif;" >//the y-coordinate of the upper-left corner of the image to find the background image </span> bgrc.size = bgimage.size; The size of the background graph given by the UI as the size of the control [Bgimage DRAWINRECT:BGRC]; Cgcontextsetfillcolorwithcolor (context, [Ccommon rgbcolorfromhexstring:@ "#58baFF "alpha:1.0f". Cgcolor); Middle Fill Color}//draw image nsstring* imageName = [NSString string]; nsstring* Text = [NSString string]; ImageName = @ "Mbwww.png"; if (_typeid = = 0) {text = @ "web"; }else if (_typeid = = 1) {text = @ "Weibo"; }else if (_typeid = = 2) {text = @ "Sina"; }else if (_typeid = = 3) {text = @ "Sohu"; } if (_bselected) {imageName = [imageName stringbyreplacingoccurrencesofstring:@ ". png" withstring:@ "_touch.png" ]; }//imagename gives the name of the SRC image inside the view, with the selected and unchecked two states uiimage* typeimage = [UIImage imagenamed:imagename]; CGRect rc = rect; rc.origin.x = (rc.size.width-typeimage.size.width)/2; RC.ORIGIN.Y = 10; Rc.size = typeimage.size; [Typeimage DRAWINRECT:RC]; Draw text in that text under image cgpoint textpt = Cgpointmake (rc.origin.x, rc.origin.y+rc.size.height+10); [Text drawatpoint:textpt Withfont:[uifont systemfontofsize:14.0f];}
The code can be downloaded in http://download.csdn.net/detail/baidu_nod/7644329
A multi-choice control of the iOS drive-drawing (i)