然後我們來寫: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; }
@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
-(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];
- (CGRect)searchTypesOriginFrame { return CGRectMake(0.0f, 44.0f- 80.0f, 320.0f, 80.0f); }
- (CGRect)searchTypesShowFrame { return CGRectMake(0.0f, 44.0f, 320.0f, 80.0f); }
- (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]; }
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization // self.view.backgroundColor = [MyViewController RGBColorFromHexString:@"#eeeeee" alpha:1.0f];//特別注意不要加這句,如果加上會使MyViewController去執行viewdidload會導致程序崩潰 } 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]; }