項目實戰 網易彩票2

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/ab20514/article/details/49934281

一 競技場欄目實現

  • 自定義控制器的view
- (void)loadView
{
    UIImageView *bgV = [[UIImageView alloc] initWithFrame:XMGKeyWindow.bounds];

    bgV.image = [UIImage imageNamed:@"NLArenaBackground"];

    // 一定要運行用戶交互
    bgV.userInteractionEnabled = YES;

    self.view = bgV;
}
  • 導航條背景圖片XMGArenaNavController
+ (void)initialize
{
    // 設置導航條背景圖片
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];

    UIImage *image =  [UIImage imageNamed:@"NLArenaNavBar64"];
    image =[image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];

    [bar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
  • 設置XMGArenaViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // 設置導航條的titleView內容
    UISegmentedControl *segContontrol = [[UISegmentedControl alloc] initWithItems:@[@"足球",@"籃球"]];

    // 設置UISegmentedControl選中的圖片
    [segContontrol setBackgroundImage:[UIImage imageNamed:@"CPArenaSegmentSelectedBG"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

    // 正常的圖片
    [segContontrol setBackgroundImage:[UIImage imageNamed:@"CPArenaSegmentBG"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    [segContontrol setTintColor:[UIColor colorWithRed:0 green:142/255.0 blue:143/255.0 alpha:1]];

    // 設置選中的文字顏色
    [segContontrol setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateSelected];
   // 默認選中第一個 
    segContontrol.selectedSegmentIndex = 0;

    self.navigationItem.titleView = segContontrol;    
}

二 我的彩票欄目實現

- (void)viewDidLoad {  // 控制器 view 加載完
    [super viewDidLoad];
    // 獲取按鈕的背景圖片
    UIImage *image =  _loginBtn.currentBackgroundImage;
    image = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
    [_loginBtn setBackgroundImage:image forState:UIControlStateNormal];
}
// 設置導航條的左邊按鈕
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"客服" forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"FBMM_Barbutton"] forState:UIControlStateNormal];

    // 根據按鈕的文字和圖片,自動計算按鈕尺寸,
    [btn sizeToFit];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

    // 設置導航條右邊的按鈕
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginRenderingName:@"Mylottery_config"] style:UIBarButtonItemStyleBordered target:self action:@selector(setting)];

三 發現欄目實現

  • tableviewcell

    這裏寫圖片描述

    • 設置 Style:Grouped
    • 設置 Content:Staic Cells (靜態 cell)
  • cell 動畫設置

// 當一個cell即將顯示的時候就會調用這個方法
// 通常這個方法,用來做cell的動畫
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.transform = CGAffineTransformMakeTranslation(self.view.width, 0);

    [UIView animateWithDuration:0.5 animations:^{
        cell.transform = CGAffineTransformIdentity;
    }];
}
  • 每次進入頁面都能執行動畫
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.tableView reloadData];
}
  • 點擊 cell 跳轉控制器XMGBuyViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *titleBtn = [XMGTitleButton buttonWithType:UIButtonTypeCustom];
    _titleBtn = titleBtn;
    [titleBtn setTitle:@"全部採種" forState:UIControlStateNormal];

    [titleBtn setImage:[UIImage imageNamed:@"YellowDownArrow"] forState:UIControlStateNormal];

    [titleBtn sizeToFit];

    self.navigationItem.titleView = titleBtn;
}
  • 點擊修改文字
    • 重寫 title 方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 [_titleBtn setImage:nil forState:UIControlStateNormal];

//    [_titleBtn setTitle:@"全部彩種全部彩種" forState:UIControlStateNormal];
//    [_titleBtn sizeToFit];
}
  • 修改子控件的位置(文字和圖片換位置)

    • 方法1

      // 設置按鈕UIImageView的位置
      - (CGRect)imageRectForContentRect:(CGRect)contentRect
      
      // 設置按鈕內部UILabel的位置
      - (CGRect)titleRectForContentRect:(CGRect)contentRect
      
    • 方法2

// 佈局子控件
- (void)layoutSubviews
{
    [super layoutSubviews];

    if (self.titleLabel.x > self.imageView.x) {
        // 交換兩個子控件的位置
        self.titleLabel.x = self.imageView.x;

        NSLog(@"titleLabel-%f",self.titleLabel.x);

        self.imageView.x = CGRectGetMaxX(self.titleLabel.frame);

        NSLog(@"imageView-%f",self.imageView.x);

    }   
}
  • 重寫方法,添加額外功能
// 思想:如果以後系統自帶的功能不滿足需求,就重寫這個方法,添加額外的功能,一定要記得還原之前的功能
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];  
    [self sizeToFit];
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
    [super setImage:image forState:state];
    [self sizeToFit];
}
  • 隱藏底部條

    這裏寫圖片描述

    • 只能隱藏系統的底部條,不能隱藏自定義條
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated]; 
    // 移除系統tabBar上自帶的按鈕
    for (UIView *childView in self.tabBar.subviews) {

        if (![childView isKindOfClass:[XMGTabBar class]]) {
            [childView removeFromSuperview];
        }     
    }
}

四 設置返回按鈕

  • 統一設置返回 XMGNavigationController
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // viewController:棧頂控制器,導航條的內容是由棧頂控制器

    // 設置導航條左邊按鈕,非根控制器才需要
    if (self.childViewControllers.count != 0) { // 非根控制器       
       viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginRenderingName:@"NavBack"] style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    }

    [super pushViewController:viewController animated:animated];
}
- (void)back
{
    [self popViewControllerAnimated:YES];
}
  • 滑動手勢,有滑動返回功能
    • 清掉代理可以清除滑動返回功能
- (void)viewDidLoad {
    [super viewDidLoad];
    // 前提條件,覆蓋了系統的返回按鈕,就不會有滑動返回功能。 
    _popDelegate = self.interactivePopGestureRecognizer.delegate; // 記錄代理

    self.delegate = self; 
}
#pragma mark - UINavigationControllerDelegate
// 監聽什麼時候回到跟控制器
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == self.childViewControllers[0]) { // 根控制器
        self.interactivePopGestureRecognizer.delegate = _popDelegate;
    }else{
        // 實現導航控制器的滑動返回
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    NSLog(@"%@",viewController);
}
  • 簡單實現滑動返回功能(不常用)
+ (void)initialize
{
     …… ……
 // 統一設置導航條按鈕的顏色
    [bar setTintColor:[UIColor whiteColor]];

    // 獲取UIBarButtonItem
    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];

    // 設置導航條返回按鈕的文字的位置
    [item setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault];    
}

五 滑動返回全屏實現

  • 默認系統滑動功能只能滑動左邊邊緣

  • 獲取成員變量,知道屬性名,怎麼通過運行時機制獲取屬性值?

    • 首先得要獲取屬性的名,通過KVC獲取值
    • 獲取target,需要使用運行時機制,遍歷出類裏面所有屬性名
    • 利用運行時獲取屬性名,只能獲取當前類下的所有屬性名,並不能獲取它的子類或者父類。
    • class_copyIvarList(<__unsafe_unretained Class cls>, <unsigned int *outCount>)
      • copyIvarList:獲取成員屬性列表
      • Class:獲取哪個類下面的屬性
      • outCount:這個類有多少個成員屬性,成員屬性的總數
- (void)viewDidLoad {
    [super viewDidLoad];

    // 系統的滑動手勢觸發的時候,會調用Target的action,去做滑動返回的事情(action), 
    // 獲取系統滑動返回的target和action
    UIScreenEdgePanGestureRecognizer *gesture = self.interactivePopGestureRecognizer;

 NSArray *targets  =  [gesture valueForKeyPath:@"_targets"];

    id gestureRecognizer = targets[0];

    id target = [gestureRecognizer valueForKeyPath:@"_target"];

    self.interactivePopGestureRecognizer.enabled = NO;

    // 借用系統的滑動手勢的功能,當觸發自己的滑動手勢的時候,調用系統的滑動返回功能
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];

    pan.delegate = self;

    [self.view addGestureRecognizer:pan];
}
// 如果返回no,表示不觸發這個手勢
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // 只有非根控制器才能擁有滑動返回功能
    return self.childViewControllers.count != 1;
}

六 引導頁設置

  • 新特性業務
    • 先判斷當前有沒有最新版本,有就進入引導頁提示
 // 最新的版本保存到info.plist文件
    NSString *curVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];

    // 獲取上一次保存的最新版本號
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:"version"];

    UIViewController *rootVc;

    if ([curVersion isEqualToString:lastVersion]) { // 相等
        // 沒新版本,進入主框架界面
        // 創建tabBarVc
        rootVc  = [[XMGTabBarController alloc] init];

    }else{ // 表示有最新的版本號,進入新特性界面

        rootVc = [[UIViewController alloc] init];
        rootVc.view.backgroundColor = [UIColor greenColor];

        // 保存當前的最新的版本號
        [[NSUserDefaults standardUserDefaults] setObject:curVersion forKey:"version"];     
    }
  • UICollectionView實現新特性頁面

    • 1.必須要設置佈局參數
    • 2.註冊cell
    • 3.自定義cell
  • 重寫 init 方法

- (instancetype)init
{   
    // 創建一個流水佈局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    // 設置cell的尺寸
      layout.itemSize = [UIScreen mainScreen].bounds.size;

    // 設置滾動的方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // 行間距
    layout.minimumLineSpacing = 0;

    // 設置cell之間的間距
    layout.minimumInteritemSpacing = 0;

   // 組間距
//    layout.sectionInset = UIEdgeInsetsMake(100, 20, 0, 30);

    return [super initWithCollectionViewLayout:layout];
}
static NSString *ID = @"cell";
- (void)viewDidLoad {
    [super viewDidLoad];

    // 在UICollectionViewController中self.view != self.collectionView

    // 註冊cell
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
}
  • Items對應cell
// 返回有多少個cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}
// 返回每一個cell長什麼樣
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{  
    // 從緩存池裏取
    // UICollectionViewCell 沒有UIImageView
    XMGNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];

    NSString *imageName = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item + 1];
    cell.image = [UIImage imageNamed:imageName];

    return cell;
}
  • 初始化
- (void)setUp
{
    self.collectionView.bounces = NO;

    self.collectionView.showsHorizontalScrollIndicator = NO;

    self.collectionView.pagingEnabled = YES;

    self.collectionView.delegate = self;
}
  • 減速完成的時候調用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{   
    // 移動子控件的位置,並且做動畫
    NSLog(@"%f",scrollView.contentOffset.x);

    // 獲取scrollView滾動的X
    CGFloat offsetX = scrollView.contentOffset.x;

    // 計算偏移量的差值
    CGFloat delta = offsetX - _lastOffsetX;

    // 平移子控件
    _guide.x += 2 * delta;

    [UIView animateWithDuration:0.25 animations:^{
        _guide.x -= delta;
    }];
    // 計算頁數
    int page = offsetX / scrollView.width + 1;
    // 切換子控件的顯示
    NSString *imageName = [NSString stringWithFormat:@"guide%d",page];
    _guide.image = [UIImage imageNamed:imageName];

    _lastOffsetX = offsetX;
}
  • 添加子控件
- (void)setUpChildViewLayout
{
    // guide
    UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    _guide = guide;
    guide.x += 50;
    [self.collectionView addSubview:guide];

    // largerText
    UIImageView *largerText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    largerText.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.7);
    [self.collectionView addSubview:largerText];

    // smallText
    UIImageView *smallText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    smallText.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.8);
    [self.collectionView addSubview:smallText];

    // guideLine
    UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    guideLine.x -= 150;
    [self.collectionView addSubview:guideLine];
}
  • 自定義 cell樣式
- (void)setImage:(UIImage *)image
{
    _image = image;

    self.imageView.image = image;
}
- (UIImageView *)imageView
{
    if (_imageView == nil) {
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.bounds];

        [self.contentView addSubview:imageV];

        _imageView = imageV;
    }
    return _imageView;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章