让背景在文字的后面移动


这种效果应用的场景还是挺多的,而且看起来效果真的还可以,今天跟广大的朋友们分享一下我的做法,本人菜鸟一枚,还望大侠不要嗤之以鼻,都是从懵懂开始的.


#import "ViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic ,weak) UITableView *table;
@property (nonatomic ,weak) UIView *backGroundView;
@end

@implementation ViewController

- (void)viewDidLoad {
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)viewWillAppear:(BOOL)animated{
    
#warning 一定要先添加backGroundView,才能达到在文字后面移动的效果
    
    // 1. 创建一个要移动的背景view 并且把frame设置成跟table的cell一样大
    UIView *backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    
    // 设置背景色 不然移动的时候看不出效果
    backGroundView.backgroundColor = [UIColor redColor];
    
    // 添加到视图上
    [self.view addSubview:backGroundView];
    
   // 保存引用,留到下面做操作
    self.backGroundView = backGroundView;
    
   // 2. 创建一个tableView
    UITableView *table = [[UITableView alloc]initWithFrame:self.view.frame];
    
    // 把table的背景色设置为透明色
    table.backgroundColor = [UIColor clearColor];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    self.table = table;
}

/**
 *  选中某一行所执行的方法
 *
 *  @param tableView tableView
 *  @param indexPath 当前选中的位置
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [UIView animateWithDuration:0.25 animations:^{
        self.backGroundView.frame = CGRectMake(0, self.backGroundView.bounds.size.height * indexPath.row, self.view.bounds.size.width, 44);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Identifier];
    }
    // 这里必须要设置cell的背景颜色为纯洁的
    cell.backgroundColor = [UIColor clearColor];
    // 选中样式要取消掉 必须这样做
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [NSString stringWithFormat:@"test---%ld",indexPath.row];
    
    return cell;
}


我这种办法可能比较屌丝,但是这已经是我想到得最好办法了,希望看到的朋友要是有更好的办法的话能推荐给我,提前谢谢大家啦
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值