iOS Swift&OC 模仿主流App 實現滑動視圖隱藏導覽列

來源:互聯網
上載者:User

標籤:

簡單直接上代碼 -.- 一個GIF圖5M?

@property (nonatomic, strong) UITableView *tableViewScroll;@property (nonatomic, assign) double recordDistance; //記錄滑動的距離@property (nonatomic, strong) UIView *customView;@property (nonatomic, strong) UIButton *btn;

建立所需要的視圖

- (UITableView *)tableViewScroll{    if (_tableViewScroll == nil) {        _tableViewScroll = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight) style:UITableViewStylePlain];        _tableViewScroll.delegate = self;        _tableViewScroll.dataSource = self;        [_tableViewScroll registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];    }    return _tableViewScroll;}
- (UIView *)customView{    if (_customView == nil) {        _customView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, screenWidth, 30)];        _customView.backgroundColor = [UIColor lightGrayColor];    }    return _customView;}
- (UIButton *)btn{    if (_btn == nil) {        _btn = [UIButton buttonWithType:UIButtonTypeCustom];        _btn.frame = CGRectMake(screenWidth - 70, 5, 40, 20);        [_btn setTitle:@"關注" forState:UIControlStateNormal];        _btn.titleLabel.font = [UIFont systemFontOfSize:13];        _btn.backgroundColor = [UIColor greenColor];    }    return _btn;}
關鍵在於scrollView協議方法中的處理
// Objective-C- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    // 記錄滑動距離    self.recordDistance = scrollView.contentOffset.y;    NSLog(@"histroy_y ======== %lf", self.recordDistance);}// Swiftfunc scrollViewDidEndDecelerating(scrollView: UIScrollView) {        self.recordDistance = scrollView.contentOffset.y    }
// Objective-C- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    NSLog(@"y ======= %lf", scrollView.contentOffset.y);    scrollView.contentOffset.y > self.recordDistance ? scrollView.contentOffset.y < self.recordDistance ? [self.navigationController setNavigationBarHidden:NO animated:YES]:[self.navigationController setNavigationBarHidden:YES animated:YES] : [self.navigationController setNavigationBarHidden:NO animated:YES];}// Swiftfunc scrollViewDidScroll(scrollView: UIScrollView) {        scrollView.contentOffset.y > self.recordDistance ? scrollView.contentOffset.y < self.recordDistance ? self.navigationController?.setNavigationBarHidden(false, animated: true):self.navigationController?.setNavigationBarHidden(true, animated: true) : self.navigationController?.setNavigationBarHidden(false, animated: true);    }

省略了tableview相關的代碼
在viewDidload中

    [self.view addSubview:self.tableViewScroll];    [self.navigationController.navigationBar addSubview:self.customView];    [self.customView addSubview:self.btn];    self.title = @"這是一個測試頁";    self.navigationController.navigationBar.barTintColor = [UIColor lightGrayColor];

覺得還可以請點個贊, 我要申請微博加V. 點個贊吧客官 -.- 哈哈哈

iOS Swift&OC 模仿主流App 實現滑動視圖隱藏導覽列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.