然后我们来写: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
加入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下载