A multi-choice control with animation in IOS (2)
Then let's write: TypeSelectView
This is relatively simple. We only need to position each TypeView and use it as our subview.
@interface TypeSelectView : UIView@property(nonatomic) BOOL bShown;@property(nonatomic, strong) TypeView* curSelectedView;-(id)initWithFrame:(CGRect)frame searchType:(int)type;@end
-(id)initWithFrame:(CGRect)frame searchType:(int)type{ self = [super initWithFrame:frame]; if (self) { _bShown = NO; self.backgroundColor = [UIColor clearColor]; CGRect rcBg = frame; rcBg.origin.y = 0.0f; rcBg.origin.x = 0.0f; UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:rcBg]; bgImageView.image = [UIImage imageNamed:@"change_icon_bg.png"]; [self addSubview:bgImageView]; CGRect typeRc =CGRectMake(4.0f, 0.0f, TypeView_Width, TypeView_Height); for (int i = 0; i < 4; i++) { TypeView *curTypeView = [[TypeView alloc] initWithFrame:typeRc]; curTypeView.typesView = self; curTypeView.typeId = i; if (i == type) { curTypeView.bSelected = YES; self.curSelectedView = curTypeView; } [self addSubview:curTypeView]; if (i == 3) { typeRc.origin.x = 4.0f; typeRc.origin.y = 78.0f; }else { typeRc.origin.x += 314.0f/4; } } } return self;}
Then we write TopBarView
@interface TopBarView : UIView-(void)layoutViews;@property(nonatomic,strong)TopBarControllerViewController* myViewController;@property(nonatomic)UIButton* button;@property(nonatomic)UITextField* textField;@property (nonatomic, strong) TypeSelectView *topTypesView;-(void)onclick;@end
The code for adding TypeSelectView is:
-(Void) layoutViews {self. clipsToBounds = YES; _ topTypesView. bShown = NO; TypeSelectView * tmpType = [[TypeSelectView alloc] initWithFrame: [[HomePageUIManager sharedInstance] searchTypesOriginFrame] searchType: 0]; _ topTypesView. autoresizingMask = bytes | UIViewAutoresizingFlexibleWidth | bytes; _ topTypesView = tmpType; // write _ topTypesView after these views [[MyViewController sharedInstance]. view insertSubview: _ topTypesView belowSubview: self];
Add _ topTypesView to the end of [MyViewController sharedInstance]. view so that the initial position can be covered by TopBarView. the initial position of _ topTypeView is:
- (CGRect)searchTypesOriginFrame{return CGRectMake(0.0f, 44.0f- 80.0f, 320.0f, 80.0f);}
The position shown in the show is:
- (CGRect)searchTypesShowFrame{return CGRectMake(0.0f, 44.0f, 320.0f, 80.0f);}
The Code of show TypeSelectedView is to change the frame of _ topTypesView. hide is to reverse the frame.
- (void)toShowTypesView { _topTypesView.frame = [[HomePageUIManager sharedInstance] searchTypesOriginFrame];[UIView beginAnimations:@"ShowTypes" context:NULL];[UIView setAnimationDuration:0.4f];[UIView setAnimationDelegate:self];[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];[UIView setAnimationBeginsFromCurrentState:YES];[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; _topTypesView.bShown = YES;_topTypesView.frame = [[HomePageUIManager sharedInstance] searchTypesShowFrame]; [UIView commitAnimations];}
MyViewController initialization:
-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) handle {self = [super initWithNibName: nibNameOrNil bundle: callback]; if (self) {// Custom initialization// Self. view. backgroundColor = [MyViewController RGBColorFromHexString: @ "# eeeeee" alpha: 1.0f]; // do not add this sentence. If it is added, MyViewController will execute viewdidload, causing program crash.} Return self;} // statement + (MyViewController *) sharedInstance {static MyViewController * _ sharedInstance = nil; if (_ sharedInstance = nil) _ sharedInstance = [[MyViewController alloc] initWithNibName: nil bundle: nil]; return _ sharedInstance;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. CGRect rcSearchBar = [[HomePageUIManager sharedInstance] searchBarViewFrame]; TopBarView * tmpView = [[TopBarView alloc] initWithFrame: rcSearchBar]; [tmpView layoutViews]; [self. view addSubview: tmpView]; UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 44,320, 2)]; view. backgroundColor = [UIColor grayColor]; [self. view addSubview: view];}
The code can be downloaded at http://download.csdn.net/detail/baidu_nod/7644329