IOS的一個帶動畫的多項選擇的控制項(二)

來源:互聯網
上載者:User

標籤: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下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.