iOS 列表元素曝光

开发需求中, 产品经常希望知道列表中的某个元素是不是曝光了, 但是曝光的规则又会给的很复杂, 比如

  1. 页面出现且数据加载好 需要曝光
  2. 刷新页面 数据没变不曝光
  3. 进入下一个页面, 再回来, 曝光
  4. 滑动列表, 元素不可见, 然后滑回来, 又变的可见了, 需要曝光
  5. 退后台 再回到应用, 需要曝光
  6. …

甚至不同的产品不同的业务这里曝光规则都会不一样, 如果每个业务自己去单独实现, 又会有很多重复代码.

我封装了一个简单曝光的分类
使用方法可以参考: Example/SubItems/ScrollView/WYScrollExposureViewController.m

简单来说

  1. 导入文件, 并 import 头文件
  2. 实现 UIScrollViewDelegate, 滚动的时候调用 simple_markScrollViewDidScroll方法, 每调用一次, 都会检查屏幕上是否存在没有曝光的列表元素, 然后曝光
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    // 看需要可以打开, 或者做下限频, 比如一秒调用一次
    //- (void)scrollViewDidScroll:(UIScrollView *)scrollView
    //{
    // [self stoppedScrollingForExposure];
    //}

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
    [self stoppedScrollingForExposure];
    }

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
    if (!decelerate)
    {
    [self stoppedScrollingForExposure];
    }
    }

    - (void)stoppedScrollingForExposure
    {
    [self.tableView simple_markScrollViewDidScroll];
    }
  3. 在各个业务时机调用 simple_exposureWhenNeed 以及 simple_markInvisibleForExposure
    1. 页面出现时候, 如页面刷新时, 页面出现时, 从二级页返回时, 后台回到前台, 调用 simple_exposureWhenNeed
    2. 页面消失时, 如退出页面, 进到二级页, 退到后台, 页面刷新之前 调用simple_markInvisibleForExposure
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      #pragma mark - Exposure

      - (void)exposureWhenNeed
      {
      [self.tableView simple_exposureWhenNeed];
      }

      - (void)markInvisibleForExposure
      {
      [self.tableView simple_markInvisibleForExposure];
      }
  4. cell 需要实现 WYScrollViewItemSimpleExposureProtocol 委托
    1. 需要曝光则返回一个不重复字符串, 不需要曝光则返回空字符串即可
    2. 收到 simple_exposureStatInfo 回调时, 自行做上报即可
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      #pragma mark - WYScrollViewItemSimpleExposureProtocol
      - (NSString *)simple_exposureIdentifier
      {
      return WY_AVOID_NIL_STRING(self.data);
      }

      - (void)simple_exposureStatInfo:(NSString *)identifier;
      {
      NSLog(@"Exposure %@", identifier);
      }
-------------本文结束感谢您的阅读-------------

欢迎关注我的其它发布渠道