iOS开发仿电商类APP首页实例

 更新时间:2016年11月07日 11:17:37   作者:zhifenx  
本篇文章主要介绍了iOS开发仿电商类APP首页实例,主要是利用ui布局,具有一定的参考价值,有需要的可以了解一下。

现在淘宝,京东应用很广泛,今天就效仿做一个类似电商APP首页的实例。

一、Gif效果图:

二、UI布局:
看下图的层级关系,除去最下方的TabBar,首页其余部分全是用UICollectionView实现;其分两大部分,实现三种功能。上方是父UICollectionView的headerView,在此headerView中添加了两个UICollectionView,分别实现图片无限轮播器和一个横向滑动的功能菜单按钮。然后下面就是父UICollectionView的cell,上下滑动展示商品内容。

三、代码:

因为这篇文章主要是讲如何实现电商类APP首页布局的,所以如何设置UICollectionView我就不再赘述,网上有很多这方面的章。

下面是主控制器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];

  //关闭自动调整滚动视图(不关闭图片轮播器会出现问题)
  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 数据源方法
- (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
//设置headerView的宽高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

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

#pragma mark ---UICollectionViewDelegateFlowLayout

//设置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属性设置为NO这个原因我在上一篇文章一行代码实现图片无限轮播器中有说明,如果在有NavigationBar的情况下如果不设置就会自动调整滚动视图的frame,这样会导致轮播器imageView显示错位。

- (void)viewDidLoad {
   [super viewDidLoad]; 

  //关闭自动调整滚动视图(不关闭图片轮播器会出现问题)
   self.automaticallyAdjustsScrollViewInsets = NO;
}

2、UICollectionView的UICollectionReusableView就相当于UITableView的headerView和footerView,要想给UICollectionView追加headerView就必须先注册,且追加的类型是UICollectionElementKindSectionHeader相对应的UICollectionElementKindSectionFooter就是追加footerView

实例化UICollectionView时的注册UICollectionReusableView代码

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

3、实现UICollectionView的数据源代理方法- (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;
  }
}

下面是项目文件夹说明:

下载完成解压后请打开这个文件运行项目:

四、总结:

1、实现这种首页布局其实还可以用UITableView,原理差不多就看功能需求,这里就简单的实现了此类电商APP的首页,像淘宝和京东那样更复杂的UI布局,有兴趣的同学可以研究一下,希望这篇文章可以给你带来收获和启发,欢迎评论交流。

最后:源码下载

相关文章

  • IOS微信摇一摇声音无法播放的解决办法

    IOS微信摇一摇声音无法播放的解决办法

    这篇文章主要为大家详细介绍了IOS微信摇一摇声音无法播放的解决办法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • iOS中实现图片自适应拉伸效果的方法

    iOS中实现图片自适应拉伸效果的方法

    图片拉伸在移动开发中特别常见,比如常用的即时通讯应用中的聊天气泡就需要根据文字长度对背景图片进行拉伸自适应。下面这篇文章主要给大家介绍了iOS中实现图片自适应拉伸效果的方法,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-03-03
  • 详解iOS应用程序的启动过程

    详解iOS应用程序的启动过程

    这篇文章主要介绍了iOS应用程序的启动过程,讲述了从其执行main函数开始到展示UIWindow的流程中的一些关键点,需要的朋友可以参考下
    2016-03-03
  • 浅析iOS多视图滑动点击切换的集成

    浅析iOS多视图滑动点击切换的集成

    本文将大家常常会用到的多视图滑动点击切换视图进行封装,这样在大家使用的时候就很方便了,有需要的可以参考学习,下面一起来看看吧。
    2016-08-08
  • ios中Deep Linking实例分析用法

    ios中Deep Linking实例分析用法

    本篇文章给大家分享了在IOS中Deep Linking的用法以及代码实例,有兴趣的朋友跟着学习下吧。
    2018-01-01
  • IOS获取系统相册中照片的示例代码

    IOS获取系统相册中照片的示例代码

    在大家的日常开发中,经常会遇到有的app需要从系统相册中获取图片,如设置用户头像等,下面这篇文章给大家分享这个功能的实现,有需要的可以参考借鉴。
    2016-09-09
  • iOS中长按调出菜单组件UIMenuController的使用实例

    iOS中长按调出菜单组件UIMenuController的使用实例

    UIMenuController即是用来制作我们平时对文本长按屏幕后显示出的复制粘贴等选项菜单,下面就来详细整理一下iOS中长按调出菜单组件UIMenuController的使用实例:
    2016-06-06
  • iOS中大尺寸图片的旋转与缩放实例详解

    iOS中大尺寸图片的旋转与缩放实例详解

    图片缩小旋转是我们在开发中经常会遇到的一个功能,下面这篇文章主要给大家介绍了关于iOS中大尺寸图片的旋转与缩放的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧
    2018-09-09
  • IOS代码笔记之下拉菜单效果

    IOS代码笔记之下拉菜单效果

    这篇文章主要为大家详细介绍了IOS实现下拉菜单效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • IOS10.11 无法访问http的问题解决办法

    IOS10.11 无法访问http的问题解决办法

    这篇文章主要介绍了IOS10.11 无法访问http的问题解决办法的相关资料,需要的朋友可以参考下
    2016-12-12

最新评论