標籤:ios 動畫 多項選擇
然後我們來寫:TypeSelectView
這個比較簡單,我們只要只要每個TypeView的位置,然後作為自己的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;}
然後我們寫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
加入TypeSelectView的代碼是:
-(void)layoutViews{ self.clipsToBounds = YES; _topTypesView.bShown = NO; TypeSelectView* tmpType = [[TypeSelectView alloc] initWithFrame:[[HomePageUIManager sharedInstance] searchTypesOriginFrame] searchType:0]; _topTypesView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; _topTypesView = tmpType; //這樣寫_topTypesView在這些view的後面 [[MyViewController sharedInstance].view insertSubview:_topTypesView belowSubview:self];
這個把_topTypesView加到[MyViewController sharedInstance].view 的後面,這樣初始位置才能被TopBarView遮住,_topTypeView的初始位置是:
- (CGRect)searchTypesOriginFrame{return CGRectMake(0.0f, 44.0f- 80.0f, 320.0f, 80.0f);}
show出來的位置是:
- (CGRect)searchTypesShowFrame{return CGRectMake(0.0f, 44.0f, 320.0f, 80.0f);}
show TypeSelectedView的代碼是,就是改變_topTypesView的frame,hide是把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的初始化:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization <strong>// self.view.backgroundColor = [MyViewController RGBColorFromHexString:@"#eeeeee" alpha:1.0f];//特別注意不要加這句,如果加上會使MyViewController去執行viewdidload會導致程式崩潰</strong> } return self;}//單例的寫法+ (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]; }
代碼可以在http://download.csdn.net/detail/baidu_nod/7644329下載