您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

動手做一個帶動畫的多選下拉控件(二):TypeSelectView、TopBarView 與展開動畫

編輯: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) MyViewController *myViewController;
@property (nonatomic) UIButton *button;
@property (nonatomic) UITextField *textField;
@property (nonatomic, strong) TypeSelectView *topTypesView;
- (void)onclick;
@end

把下拉面板藏到 bar 後面

這裡有個關鍵技巧:用 insertSubview:belowSubview: 把面板插到 TopBarView 的「後面」,這樣面板收合時就能被 bar 完全遮住,不需要額外做隱藏處理。

-(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;

    [[MyViewController sharedInstance].view insertSubview:_topTypesView belowSubview:self];
}

收合位置與展開位置只差一個 bar 的高度:

// 收合:往上收進 bar 底下
- (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);
}

展開 / 收合動畫

做法非常直觀——改變 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];
}

主控制器的初始化

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // 特別注意:不要設定 self.view.backgroundColor,
        // 這會提早觸發 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];

    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];
}

完整專案可於 CSDN 下載頁面取得。這裡最值得記住的兩個觀念是:用「插到後面」代替「隱藏」,以及所有位置資訊集中由 HomePageUIManager 統一提供,讓版面調整不必改動視圖邏輯。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved