IOS controls with multiple options for animation (1)
First:
This program has two levels. One is the bar with UITextField on the top, and the other is the view selected from the drop-down list. The selected view has four custom uiviews.
We first define a UIViewController named MyViewController, and then the bar on the top is TopBarView. The view selected from the drop-down list is TypeSelectView. A custom view like UIButton is called TypeView. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + expires/expires + 0 vLOqs/CwK2/expires + PGJyPgo8L3A + CjxwPs/expires + CjxwPjxwcmUgY2xhc3M9 "brush: java; "># 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) {// if the selected item is not selected, We need to cancel the selected status of the selected item in TypeSelectView if (_ typesView. curSelectedView) {_ typesView. curSelectedView. bSelected = NO; [_ typesView. curSelectedView setNeedsDisplay];} _ bSelected = YES; _ typesView. curSelectedView = self; [self setNeedsDisplay];}
Then there is draw:
-(Void) drawRect :( CGRect) rect {CGContextRef context = UIGraphicsGetCurrentContext (); Digest (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) {// display the select background UIImage * bgImage = [UIImage imageNamed: @ "change_icon_touch_bg.png"]; CGRect bgRc = rect; bgRc. origin. x = (bgRc. size. width-bgImage. size. width)/2 + 1.0f; // locate the x coordinate bgRc in the upper left corner of the background image. origin. y = (bgRc. size. height-bgImage. size. height)/2; // locate the y coordinate bgRc in the upper left corner of the background image. size = bgImage. size; // the size of the background image given by the ui is the size of the control [bgImage drawInRect: bgRc]; CGContextSetFillColorWithColor (context, [CCommon RGBColorFromHexString: @ "#58 baff" alpha: 1.0f]. CGColor); // intermediate 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 "];} // name of the src image in the view given by imageName. In either or both 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: CGPoint textPt = CGPointMake (rc. origin. x, rc. origin. y + rc. size. height + 10); [text drawAtPoint: textPt withFont: [UIFont systemFontOfSize: 14.0f];}