標籤:
簡單直接上代碼 -.- 一個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 實現滑動視圖隱藏導覽列