欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)仿電商類APP首頁實例

 更新時間:2016年11月07日 11:17:37   作者:zhifenx  
本篇文章主要介紹了iOS開發(fā)仿電商類APP首頁實例,主要是利用ui布局,具有一定的參考價值,有需要的可以了解一下。

現(xiàn)在淘寶,京東應(yīng)用很廣泛,今天就效仿做一個類似電商APP首頁的實例。

一、Gif效果圖:

二、UI布局:
看下圖的層級關(guān)系,除去最下方的TabBar,首頁其余部分全是用UICollectionView實現(xiàn);其分兩大部分,實現(xiàn)三種功能。上方是父UICollectionView的headerView,在此headerView中添加了兩個UICollectionView,分別實現(xiàn)圖片無限輪播器和一個橫向滑動的功能菜單按鈕。然后下面就是父UICollectionView的cell,上下滑動展示商品內(nèi)容。

三、代碼:

因為這篇文章主要是講如何實現(xiàn)電商類APP首頁布局的,所以如何設(shè)置UICollectionView我就不再贅述,網(wǎng)上有很多這方面的章。

下面是主控制器MYIHomeViewController.m文件中的代碼,初始化父UICollectionView的代碼就在這里面,貼出這部分代碼是有些地方需要特別注意,有需要的可以下載源碼:https://pan.baidu.com/s/1dFsnJpR

#import "MYIHomeViewController.h"

#import "MYIHomeHeaderView.h"
#import "JFConfigFile.h"
#import "MYIHomeLayout.h"
#import "MYIHomeCell.h"

static NSString *ID = @"collectionViewCell";

@interface MYIHomeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation MYIHomeViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  //關(guān)閉自動調(diào)整滾動視圖(不關(guān)閉圖片輪播器會出現(xiàn)問題)
  self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];

  //隱藏navigationBar
  self.navigationController.navigationBar.hidden = YES;
}

- (void)loadView {
  [super loadView];

  //添加collectionView
  [self.view addSubview:self.collectionView];
}

//懶加載collectionView
- (UICollectionView *)collectionView {
  if (!_collectionView) {
    _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:[[MYIHomeLayout alloc] init]];
    _collectionView.backgroundColor = JFRGBColor(238, 238, 238);

    //注冊cell
    [_collectionView registerClass:[MYIHomeCell class] forCellWithReuseIdentifier:ID];

    //注冊UICollectionReusableView即headerView(切記要添加headerView一定要先注冊)
    [_collectionView registerClass:[MYIHomeHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];

    _collectionView.delegate = self;
    _collectionView.dataSource = self;
  }
  return _collectionView;
}

#pragma mark ---UICollectionViewDataSource 數(shù)據(jù)源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  return 80;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  MYIHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
  cell.iconName = @"goodsimagetest";
  cell.describe = @"螞蟻到家螞蟻到家螞蟻到家";
  cell.currentPrice = @"¥100";
  cell.originalPrice = @"¥288";
  return cell;
}

//添加headerView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  MYIHomeHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];;

  //判斷上面注冊的UICollectionReusableView類型
  if (kind == UICollectionElementKindSectionHeader) {
    return headerView;
  }else {
    return nil;
  }
}

#pragma mark ---UICollectionViewDelegate
//設(shè)置headerView的寬高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

  return CGSizeMake(self.view.bounds.size.width, 380);
}

#pragma mark ---UICollectionViewDelegateFlowLayout

//設(shè)置collectionView的cell上、左、下、右的間距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  return UIEdgeInsetsMake(14, 0, 0, 0);
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

@end

注意:

1、在主控制器中最好將automaticallyAdjustsScrollViewInsets屬性設(shè)置為NO這個原因我在上一篇文章一行代碼實現(xiàn)圖片無限輪播器中有說明,如果在有NavigationBar的情況下如果不設(shè)置就會自動調(diào)整滾動視圖的frame,這樣會導(dǎo)致輪播器imageView顯示錯位。

- (void)viewDidLoad {
   [super viewDidLoad]; 

  //關(guān)閉自動調(diào)整滾動視圖(不關(guān)閉圖片輪播器會出現(xiàn)問題)
   self.automaticallyAdjustsScrollViewInsets = NO;
}

2、UICollectionView的UICollectionReusableView就相當(dāng)于UITableView的headerView和footerView,要想給UICollectionView追加headerView就必須先注冊,且追加的類型是UICollectionElementKindSectionHeader相對應(yīng)的UICollectionElementKindSectionFooter就是追加footerView

實例化UICollectionView時的注冊UICollectionReusableView代碼

//注冊UICollectionReusableView即headerView(切記要添加headerView一定要先注冊)
    [_collectionView registerClass:[MYIHomeHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];

3、實現(xiàn)UICollectionView的數(shù)據(jù)源代理方法- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;

//添加headerView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  MYIHomeHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];

  //判斷上面注冊的UICollectionReusableView類型
  if (kind == UICollectionElementKindSectionHeader) {
    return headerView;
  }else {
    return nil;
  }
}

下面是項目文件夾說明:

下載完成解壓后請打開這個文件運行項目:

四、總結(jié):

1、實現(xiàn)這種首頁布局其實還可以用UITableView,原理差不多就看功能需求,這里就簡單的實現(xiàn)了此類電商APP的首頁,像淘寶和京東那樣更復(fù)雜的UI布局,有興趣的同學(xué)可以研究一下,希望這篇文章可以給你帶來收獲和啟發(fā),歡迎評論交流。

最后:源碼下載

相關(guān)文章

  • IOS微信搖一搖聲音無法播放的解決辦法

    IOS微信搖一搖聲音無法播放的解決辦法

    這篇文章主要為大家詳細介紹了IOS微信搖一搖聲音無法播放的解決辦法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • iOS中實現(xiàn)圖片自適應(yīng)拉伸效果的方法

    iOS中實現(xiàn)圖片自適應(yīng)拉伸效果的方法

    圖片拉伸在移動開發(fā)中特別常見,比如常用的即時通訊應(yīng)用中的聊天氣泡就需要根據(jù)文字長度對背景圖片進行拉伸自適應(yīng)。下面這篇文章主要給大家介紹了iOS中實現(xiàn)圖片自適應(yīng)拉伸效果的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • 詳解iOS應(yīng)用程序的啟動過程

    詳解iOS應(yīng)用程序的啟動過程

    這篇文章主要介紹了iOS應(yīng)用程序的啟動過程,講述了從其執(zhí)行main函數(shù)開始到展示UIWindow的流程中的一些關(guān)鍵點,需要的朋友可以參考下
    2016-03-03
  • 淺析iOS多視圖滑動點擊切換的集成

    淺析iOS多視圖滑動點擊切換的集成

    本文將大家常常會用到的多視圖滑動點擊切換視圖進行封裝,這樣在大家使用的時候就很方便了,有需要的可以參考學(xué)習(xí),下面一起來看看吧。
    2016-08-08
  • ios中Deep Linking實例分析用法

    ios中Deep Linking實例分析用法

    本篇文章給大家分享了在IOS中Deep Linking的用法以及代碼實例,有興趣的朋友跟著學(xué)習(xí)下吧。
    2018-01-01
  • IOS獲取系統(tǒng)相冊中照片的示例代碼

    IOS獲取系統(tǒng)相冊中照片的示例代碼

    在大家的日常開發(fā)中,經(jīng)常會遇到有的app需要從系統(tǒng)相冊中獲取圖片,如設(shè)置用戶頭像等,下面這篇文章給大家分享這個功能的實現(xiàn),有需要的可以參考借鑒。
    2016-09-09
  • iOS中長按調(diào)出菜單組件UIMenuController的使用實例

    iOS中長按調(diào)出菜單組件UIMenuController的使用實例

    UIMenuController即是用來制作我們平時對文本長按屏幕后顯示出的復(fù)制粘貼等選項菜單,下面就來詳細整理一下iOS中長按調(diào)出菜單組件UIMenuController的使用實例:
    2016-06-06
  • iOS中大尺寸圖片的旋轉(zhuǎn)與縮放實例詳解

    iOS中大尺寸圖片的旋轉(zhuǎn)與縮放實例詳解

    圖片縮小旋轉(zhuǎn)是我們在開發(fā)中經(jīng)常會遇到的一個功能,下面這篇文章主要給大家介紹了關(guān)于iOS中大尺寸圖片的旋轉(zhuǎn)與縮放的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧
    2018-09-09
  • IOS代碼筆記之下拉菜單效果

    IOS代碼筆記之下拉菜單效果

    這篇文章主要為大家詳細介紹了IOS實現(xiàn)下拉菜單效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • IOS10.11 無法訪問http的問題解決辦法

    IOS10.11 無法訪問http的問題解決辦法

    這篇文章主要介紹了IOS10.11 無法訪問http的問題解決辦法的相關(guān)資料,需要的朋友可以參考下
    2016-12-12

最新評論