iOS页面动态化,怎么样用JSON数据的原生页面摆脱低效的H5页面,来动态更新app页面样式

最近花了几天时间研究了一下怎么用iOS原生页面高效的更新app页面样式,我的思路是使用仿H5样式的JSON数据,利用各种布局(流动,线性等布局)来动态更新页面样式。

页面动态化主要的使用场景是app的电商页面,由于电商的业务需求较大,经常会需要改变各个banner的样式来满足业务的需求,所以需要客户端根据数据实时更新样式,下面说说我对页面动态化的理解跟思路。先看一下最后demo的效果:

2017-04-27 18_34_37.gif

其他app是怎么做的

用Charles抓包,发现京东跟网易考拉都是用type这个字段提前定义好各种banner的样式,通过后台返回的数据,来实现页面动态化更新,这样做有好的方面也有不好的地方:

  • 好的方面:提前定义好banner的样式,客户端只需要定义好各个样式的UI,根据type字段来展现就好
  • 不好的方面:只能根据定义好的样式来进行动态化更新,如果需要加入新的样式,需要提交新包

我的做法

客户端不定义banner样式,而是定义布局样式来实现高度动态更新页面,思路来自于CSS里定义的div+CSS布局,CSS里可以根据display的inline跟block值来进行布局,iOS里,我定义了几种常见的布局:

  • <b>FlowLayout</b>:流式布局,根据display值横向或者纵向布局,根据屏幕宽度跟banner宽度换行
  • <b>LinearLayout</b>:线性布局,纵向布局
  • <b>WaterfallLayout</b>:瀑布流布局
  • <b>OneAndNLayout</b>:左边一个大图,右边N张小图的布局
  • <b>StickyLayout</b>:悬浮布局

整体架构

页面动态化 整体架构如下:


Screenshot 2017-04-27 19.10.17.png

每个UIViewController都是一个页面,页面是由一个个卡片组成的,卡片其实就是代表了一个布局样式,每个卡片是由一个个元素组成的,元素其实指的就是卡片布局样式下的各个banner,对应的JSON数据结构如下:

{
    "cards": [
        {
            "layout": 1,
            "elements": [
                {
                    
                },
                {
                    
                },
                ...
            ]
        },
        {
            "layout": 1,
            "elements": [
                {
                    
                },
                {
                    
                },
                ...
            ]
        },
        ...
    ]
}

具体实现

由于大部分页面内容都需要上下滑动显示更多内容的,所以页面动态化的实现是基于UICollectionView来做的,当然也可以使用UIScrollView来实现(淘宝天猫都是通过一个自定义具有回收复用机制的UIScrollView来展示动态化页面内容的),我的理解是既然UICollectionView可以通过自定义UICollectionViewLayout来展现自定义的cell,为什么不用还需要自己定义一个具有回收复用机制的UIScrollView去展现内容,UICollectionView自身就是带有回收复用机制的。我的具体实现,先看下整体的UML图:

Screenshot 2017-04-27 23.10.15.png

每个KBBaseCard代表了一个布局的卡片,对应UICollectionView里的一个sectionKBBaseCard里elements的每个KBBaseElement代表了一个banner,对应UICollectionView里的一个itemKBBaseElement里的KBElementStyle提供banner需要展现的样式。KBElementStyle里的type字段是用来定义banner的样式,比方说type=1代表这个banner就是一张图。实际展示图如下:

Screenshot 2017-04-27 23.21.04.png

UICollectionViewLayout的自定义

UICollectionView提供的UICollectionViewFlowLayout并不能满足我的对样式展现的需求,所以这里我们必须自己写一套UICollectionViewLayout来提供对KBBaseElementstyle字段里的样式支持,核心方法如下,具体实现在本文中不做过多详解,大家可以自己查询一下相关的知识:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    NSInteger sectionLayout = 1;    // CardFlowLayout is the default layout
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:layoutForSection:)]) {
        sectionLayout = [self.delegate collectionView:self.collectionView layout:self layoutForSection:indexPath.section];
    }
    
    // CardFlowLayout 流式布局
    if (sectionLayout == 1) {
        [self handleLayoutAttributesForFlowLayout:layoutAttributes atIndexPath:indexPath];
    }
    // CardLinearLayout 线性布局
    if (sectionLayout == 2) {
        [self handleLayoutAttributesForLinearLayout:layoutAttributes atIndexPath:indexPath];
    }
    // CardOneAndNLayout 左边一张大图右边N张小图布局
    if (sectionLayout == 3) {
        [self handleLayoutAttributesForOneAndNLayout:layoutAttributes atIndexPath:indexPath];
    }
    // CardWaterfallLayout 瀑布流布局
    if (sectionLayout == 4) {
        [self handleLayoutAttributesForWaterfallLayout:layoutAttributes atIndexPath:indexPath];
    }
    
    return layoutAttributes;
}

JSON数据

这里提供一个我自己创建的JSON mock数据,还有根据这个JSON数据最终展现出来的页面图

  • JSON
{
    "cards": [
        {
            "layout": 1,
            "style": {
                "backgroundColor": "#ffffff"
            },
            "elements": [
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/lqdbpar1xWZw5NuOB9ezf80UDm75Ei%2BZyMo%2BMgvE%2Bdunea_02T1704271119_480_240.jpg",
                    "style": {
                        "display": "inline",
                        "margin": [
                            "0",
                            "0",
                            "0.75",
                            "0.75"
                        ],
                        "width_ratio": 0.5,
                        "width_height_ratio": 2.0
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/i11BQFE2dWpzplHNBV1Tr8Hafa5zTz%2BCoxF%2BwIBS%2BKF2Vx_03T1704261946_480_240.jpg",
                    "style": {
                        "display": "inline",
                        "margin": [
                            "0",
                            "0.75",
                            "0.75",
                            "0"
                        ],
                        "width_ratio": 0.5,
                        "width_height_ratio": 2.0
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/KwaBThC4mguSnQCGBXVtfK4RmTTSeQ%2BZ5Ql%2BTNvQ%2BKNWzP_05T1704251128_480_240.jpg",
                    "style": {
                        "display": "inline",
                        "margin": [
                            "0.75",
                            "0",
                            "0",
                            "0.75"
                        ],
                        "width_ratio": 0.5,
                        "width_height_ratio": 2.0
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/G4LGkdg7si4p5ZzVBZUKmkTq7EeTKt%2BRRaE%2BfQyx%2B6JlfC_06T1704251128_480_240.jpg",
                    "style": {
                        "display": "inline",
                        "margin": [
                            "0.75",
                            "0.75",
                            "0",
                            "0"
                        ],
                        "width_ratio": 0.5,
                        "width_height_ratio": 2.0
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/HNkdnuFcFey5Om5GpNpO-C2LLgTrO3WtFT1704201935_960_210.jpg",
                    "style": {
                        "display": "block",
                        "margin": [
                            "0.75",
                            "0",
                            "0",
                            "0"
                        ],
                        "width_height_ratio": 4.57
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/mrui23TlZJcRAocRWB70T1704262151_600_375.jpg",
                    "style": {
                        "display": "inline",
                        "margin": [
                            "0.75",
                            "0",
                            "0",
                            "0.75"
                        ],
                        "width_ratio": 0.7,
                        "width_height_ratio": 1.6
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/ppiV8ODszIZciIZbmQih-A6HUakFq6g6oLTNhT1704201914_474_555.jpg",
                    "style": {
                        "display": "inline",
                        "margin": [
                            "0.75",
                            "0.75",
                            "0",
                            "0"
                        ],
                        "width_ratio": 0.3,
                        "width_height_ratio": 0.685
                    }
                }
            ]
        },
        {
            "layout": 3,
                "style": {
                "backgroundColor": "#ffffff"
            },
            "elements": [
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/KHASB80nO5JMsX1e2iChshdRT1704262150_480_480.jpg",
                    "style": {
                        "margin": [
                            "0",
                            "0",
                            "0",
                            "0.75"
                        ],
                        "width_ratio": 0.5,
                        "width_height_ratio": 1.0
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/ygMuXb96hrtGtT6Wc9vZ2T17040101959_480_240.jpg",
                    "style": {
                        "margin": [
                            "0",
                            "0.75",
                            "0.75",
                            "0"
                        ],
                        "width_ratio": 0.5,
                        "width_height_ratio": 2.0
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/ADB5IaeoJWP9pIzVmwzkfGKhT1704262151_480_240.jpg",
                    "style": {
                        "margin": [
                            "0.75",
                            "0.75",
                            "0",
                            "0"
                        ],
                        "width_ratio": 0.5,
                        "width_height_ratio": 2.0
                    }
                }
            ]
        },
        {
            "layout": 1,
            "style": {
                "backgroundColor": "#ffffff"
            },
            "elements": [
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/ppiV8ODszIZciIZbmQih-A6HUakFq6g6oLTNhT1704201914_474_555.jpg",
                    "style": {
                        "display": "inline",
                        "margin": [
                            "0",
                            "0",
                            "0",
                            "0.75"
                        ],
                        "width_ratio": 0.5,
                        "width_height_ratio": 0.856
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/mbO7ln3VxpAWxKkw84Ay-ys6BLHmk9LLRvwa4T1704201914_474_555.jpg",
                    "style": {
                        "display": "inline",
                        "margin": [
                            "0",
                            "0.75",
                            "0",
                            "0"
                        ],
                        "width_ratio": 0.5,
                        "width_height_ratio": 0.856
                    }
                }
            ]
        },
        {
            "layout": 2,
            "style": {
                "backgroundColor": "#ffffff"
            },
            "elements": [
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/hwCkTs9AoDThXhv5k38dr2M0T1704242204_960_480.jpg",
                    "style": {
                        "margin": [
                            "0",
                            "0",
                            "10",
                            "0"
                        ],
                        "width_height_ratio": 2.0
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/Tt9TfxyDTSSirP27lXEiIsKPriT1704241519_960_480.jpg",
                    "style": {
                        "margin": [
                            "0",
                            "0",
                            "10",
                            "0"
                        ],
                        "width_height_ratio": 2.0
                    }
                },
                {
                    "type": 1,
                    "image": "http://haitao.nos.netease.com/uey1sJ03lEOglRm626Th6dKrT1704242203_960_480.jpg",
                    "style": {
                        "margin": [
                            "0",
                            "0",
                            "10",
                            "0"
                        ],
                        "width_height_ratio": 2.0
                    }
                }
            ]
        }
    ]
}
  • 页面图
Screenshot 2017-04-27 23.29.39.png
Screenshot 2017-04-27 23.29.59.png

转载请注明出处,原文地址:http://kobedai.me/p9rsts-6s/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容