美文网首页UI效果IOSiOS技巧学习_JM
iOS UITableView头视图下拉放大效果及UITable

iOS UITableView头视图下拉放大效果及UITable

作者: 若锦 | 来源:发表于2016-07-22 09:27 被阅读1405次

    很多app的在有关于用户信息的页面中,都会有头部下拉放大的效果,类似QQ空间一样。还有想微信的聊天列表中,左滑会出现删除的按钮。今天呢和大家分享一下如何去实现这些小功能。

    首先,我们先创建一个tableview

    #define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
    #define ScreenWidth [UIScreen mainScreen].bounds.size.width
    #define ScreenHeight [UIScreen mainScreen].bounds.size.height
    @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
    @property(nonatomic, strong)UITableView *tableView;
    @property(nonatomic, strong)NSMutableArray *dataSourceArray;
    @property(nonatomic, strong)UIImageView *headerBackView;
    @property(nonatomic, strong)UIImageView *photoImageView;
    @property(nonatomic, strong)UILabel *userNameLabel;
    @property(nonatomic, strong)UILabel *introduceLabel;
    @property(nonatomic, strong)UIView *tableViewHeaderView;
    @property(nonatomic, assign)NSInteger imageHeight;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        self.navigationController.navigationBar.hidden = YES;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
     
        _dataSourceArray = [NSMutableArray arrayWithObjects:@"谁念西风独自凉,萧萧黄叶闭疏窗,沉思往事立残阳",@"被酒莫惊春睡重,赌书消得泼茶香,当时只道是寻常",@"等闲变却故人心,却道故人心易变",@"谁念西风独自凉,萧萧黄叶闭疏窗,沉思往事立残阳",@"被酒莫惊春睡重,赌书消得泼茶香,当时只道是寻常",@"等闲变却故人心,却道故人心易变",nil];
     
    }
    -(void)loadView{
        [super loadView];
         _imageHeight = 160;//背景图片的高度
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self.view addSubview:_tableView];
        [self createTableViewHeaderView];
        
    }
    

    下拉放大效果

    下拉放大效果,主要是将背景图放在TableView的头视图上,通过下拉的距离从而去改变背景图的frame

    创建头视图

    -(void)createTableViewHeaderView{
        
    
        _tableViewHeaderView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, ScreenWidth, _imageHeight))];
        _headerBackView = [[UIImageView alloc] init];
        
    //    背景图
        _headerBackView.frame = CGRectMake(0, 0, ScreenWidth, _imageHeight);
        _headerBackView.image = [UIImage imageNamed:@"bj1@2x.jpg"];
     
        [_tableViewHeaderView addSubview:_headerBackView];
        _photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 62)/2, 15 , 62 , 62 )];
        [self.tableViewHeaderView addSubview:self.photoImageView];
        _photoImageView.layer.cornerRadius = 31 ;
        _photoImageView.layer.masksToBounds = YES;
        
        _photoImageView.image = [UIImage imageNamed:@"2.jpg"];
        
        _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _photoImageView.frame.origin.y + _photoImageView.frame.size.height + 8 , ScreenWidth, 20 )];
        _userNameLabel.font = [UIFont fontWithName:@"iconfont" size:16 ];
        _userNameLabel.text = @"纳兰性德";
        _userNameLabel.textAlignment = 1;
        _userNameLabel.font = [UIFont systemFontOfSize:16  ];
        _userNameLabel.textColor = [UIColor whiteColor];
        [_tableViewHeaderView addSubview:self.userNameLabel];
       
        _introduceLabel = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth - 229 )/2, _userNameLabel.frame.origin.y + _userNameLabel.frame.size.height + 10 , 229 , 16 )];
        _introduceLabel.alpha = .7;
        _introduceLabel.text = @"人生若只如初见,何事秋风悲画扇";
        _introduceLabel.textAlignment = 1;
        _introduceLabel.font = [UIFont systemFontOfSize:12 ];
        _introduceLabel.textColor = _userNameLabel.textColor;
        [_tableViewHeaderView addSubview:self.introduceLabel];
       
        self.tableView.tableHeaderView = _tableViewHeaderView;
     
    }
    

    接下来,就是实现下拉放大功能了,在- (void)scrollViewDidScroll:(UIScrollView *)scrollView方法里做相应的操作

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
    
        CGFloat width = self.view.frame.size.width; // 图片宽度
        CGFloat yOffset = scrollView.contentOffset.y;  // 偏移的y值
        if (yOffset < 0) {
            CGFloat totalOffset = _imageHeight + ABS(yOffset);
            CGFloat f = totalOffset / _imageHeight;
            self.headerBackView.frame =  CGRectMake(- (width * f - width) / 2, yOffset, width * f, totalOffset); //拉伸后的图片的frame应该是同比例缩放。
        }
        
     
    }
    

    这样就达到了我们想要的下拉放大效果

    滑动UITableViewCell显示多个按钮

    这个主要通过UITableViewRowAction这个类去实现

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath这个方法里面的代码只在iOS8.0以前版本有作用,也可以不实现。

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        editingStyle = UITableViewCellEditingStyleDelete;//此处的EditingStyle可等于任意UITableViewCellEditingStyle,该行代码只在iOS8.0以前版本有作用,也可以不实现。
    }
    

    设置相应的按钮。通过UITableViewRowAction来实现对应的按钮及按钮的响应事件,按钮的排列顺序可以通过更换按钮的响应事件在数组中的位置来调整

    -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
    
    
    {
     
        //设置删除按钮
        
        UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
    //        更新数据
            [self.dataSourceArray removeObjectAtIndex:indexPath.row];
            
    //        更新UI
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            
        }];
        
        //设置收藏按钮
        
        UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
            
            collectRowAction.backgroundColor = [UIColor greenColor];
            
            //实现收藏功能
            NSLog(@"收藏成功");
            
        }];
        
        //设置置顶按钮
        
        UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            
            [self.dataSourceArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
            
            NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
            
            [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
           
            [_tableView reloadData];
            
        }];
        
    // 设置按钮的背景颜色
        topRowAction.backgroundColor = [UIColor blueColor];
     
        collectRowAction.backgroundColor = [UIColor grayColor];
     
        return  @[deleteRowAction,collectRowAction,topRowAction];//可以通过调整数组的顺序而改变按钮的排序
        
     
    }
    
    

    这样滑动显示按钮的效果就实现了。不过UITableViewRowAction这个类只有在iOS8以后才能用

    界面如下:

    界面图

    GIF动图如下:

    下拉放大与滑动显示.gif

    demo地址

    相关文章

      网友评论

      • 75724f2f1287:请问侧滑按钮如何添加图片?
        若锦:@左手键盘右手诗 抱歉,这个我还真没有研究过,估计得自定义才行,tableview自带的应该是没有添加图片这功能。 :smile:
      • 58f0b442f46c:求个demo,谢啦 tomorooo@163.com
        若锦:@TomorJM 嗯,发送过去了,你抽空查收一下 :smile:
      • 旧城丶旧人:给我发一个demo吧,谢谢啦!1832537749@qq.com,再次谢谢无私帮助!
        若锦: @旧城丶旧人 嗯,不客气。发你邮箱了,你查收一下
      • 马铃薯蜀黍:http://www.jianshu.com/p/859e3ef76b05

        这也是一个关于下拉放大的封装,最近看到的,利用的运行时,推荐给你
        若锦:@马铃薯蜀黍 嗯,谢谢你的推荐,写的确实很棒,学习了
      • 马铃薯蜀黍:很厉害!请问现在是工作状态吗?
        若锦:@马铃薯蜀黍 厉害称不上,菜鸟一个而已 :joy:
      • ios_copy:楼主 求个dome。812559044@qq.com 谢谢。
        若锦: @人们都叫我居家好男神 嗯,发送过去了,你抽空查收一下
      • DaZenD:求demo,1161528363@qq.com。谢谢。
        若锦:@bblili 已发送,你抽空查收一下 :smile:
      • 42e1c79aee08:楼主求demo 736182360@qq.com
      • 0x00chen:楼主求一份demo 815807765@qq.com thanks
        若锦:@㴴禕斨 嗯,发过了。你抽空查收一下 :smile:
      • returnzyf:ios 7 按钮只有1个哦
        若锦:@returnzyf 嗯,确实是,这个是iOS8以后才有的,是我考虑不周,没写详细,谢谢提醒哈 :smiley:
      • overla5::smile:下午有时间敲一波
        若锦:@失格人间 嗯,可以有 :smile:
      • 24125cd48243:有 demo 吗?
        若锦:@liusenpo 哈哈,此话有理 :smile:
        若锦:@孙湖滨 恩,有的。如果需要的话可以留个联系方式,可以给你发过去看看 :joy:
        Cocoa_Coder:@孙湖滨自己敲敲代码多好

      本文标题:iOS UITableView头视图下拉放大效果及UITable

      本文链接:https://www.haomeiwen.com/subject/zasbjttx.html