实现京东金融->悬浮框效果

有些app在某些页面中会有悬浮框效果,这里以京东金融为模板

here:

Snip20170721_2.png

这种效果,首先想到跟滑动代理有关系。

  • 首先新建一个tableView

//声明
@property (nonatomic, strong) UITableView *tableView;

//懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 80;
        
    }
    return _tableView;
}

//添加
[self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(@0);
        make.top.equalTo(self.mas_topLayoutGuide);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];

//tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *iden = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.textLabel.text = @"text";
    cell.detailTextLabel.text = @"detail";
    
    return cell;
}
  • 悬浮框
//声明
@property (nonatomic, strong) UIView *displayView;

//懒加载
- (UIView *)displayView
{
    if (!_displayView) {
        _displayView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth - kWidth, kScreenHeight/3 * 2.0, 60, 60)];
        _displayView.backgroundColor = [UIColor brownColor];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = @"致粉丝的一封信";
        label.textColor = [UIColor orangeColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12];
        label.numberOfLines = 0;
        [_displayView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(@3);
            make.trailing.equalTo(@-3);
            make.top.equalTo(@5);
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [_displayView addGestureRecognizer:tap];
    }
    return _displayView;
}

  • 实现悬浮窗效果

    1. 开始拖拽: 隐藏悬浮框
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [UIView animateWithDuration:kAnimationInterval animations:^{
        self.displayView.frame = CGRectMake(kScreenWidth, kScreenHeight/3 * 2.0, 60, 60);
    }];
}

  1. 结束拖拽
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {//当滑动停止后 显示悬浮框
        [UIView animateWithDuration:kAnimationInterval animations:^{
            self.displayView.frame = CGRectMake(kScreenWidth - kWidth, kScreenHeight/3 * 2.0, 60, 60);
        }];
    }
    
    NSLog(@"---");
}

  1. 快速滑动 减速后 响应此代理
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [UIView animateWithDuration:kAnimationInterval animations:^{
        self.displayView.frame = CGRectMake(kScreenWidth - kWidth, kScreenHeight/3 * 2.0, 60, 60);
    }];
    
    NSLog(@"---=====");
}

Gif效果:

演示.gif

代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,726评论 25 709
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,019评论 3 119
  • 过去研究国民财富和原因的学问和现代经济学有何不同?---过去的学问研究的是事与愿符的规律,而现在经济学研究的是事与...
    会成长的鱼阅读 537评论 0 2
  • 情书,情不知所起一往而深。 书写的是我对她的美好感觉。 同桌,同非常过往共行南极。 同的是价值观,书桌是阵地。 你...
    金金视界阅读 296评论 4 3
  • 拆墙 记得小时候家中没院墙,除了三间土坯主草房和一大间土坯灶草房,另外就是一个草泥猪圈,其他都是八面透风,急于赶路...
    朴一阅读 345评论 3 1