iOS UITableView 頭部圖片下拉放大效果

工程中要實(shí)現(xiàn)UITableView 頭部圖片下拉放大效果,實(shí)現(xiàn)了以后想把過程記錄下來。

基本思路:
我是將頭部上面的圖片視圖放在UITableView 的backgroundView中,當(dāng)視圖拖動(dòng)的時(shí)候改變頭部圖片的fream就可以了。

為了以后直接使用,我就繼承了UITableView,并通過擴(kuò)展屬性的方法來實(shí)現(xiàn)頭部圖片下拉放大的效果,下面來看看具體實(shí)現(xiàn)過程。

新建了YMTableView對象繼承于UITableView,并添加了imgViewheight屬性,還為其添加了三個(gè)實(shí)例化的方法

.h 文件
#import <UIKit/UIKit.h>

@interface YMTableView : UITableView <UIScrollViewDelegate>

/**
 頭部可縮放的圖片
 */
@property (nonatomic,strong) UIImageView * imgView;


/**
 頭部視圖初始高度
 */
@property (nonatomic,assign)CGFloat height;


/**
 通過圖片和高度
 */
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style andImage:(UIImage *)img andHeight:(CGFloat)height;

/**
 通過圖片路徑和高度
 */
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style andImageUrl:(NSString *)imgUrl andHeight:(CGFloat)height;

/**
 通過圖片名稱
 */
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style andImageName:(NSString *)imgName andHeight:(CGFloat)height;

@end


.m文件
#import "YMTableView.h"


@interface YMTableView ()
/**
 頭部可縮放的圖片
 */
@property (nonatomic,strong)UIImage * img;


/*
 圖片的url鏈接
 */
@property (nonatomic,strong)NSString * imgUrl;


/*
 圖片的名稱
 */
@property (nonatomic,strong)NSString * imgName;
@end


@implementation YMTableView
{
    
    CGFloat contentOffSet;
}



-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style andImage:(UIImage *)img andHeight:(CGFloat)height
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.height = height;
        self.img = img;
        
        [self createHeaderView];
    }
    return self;
}

/**
 通過圖片路徑和高度
 */
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style andImageUrl:(NSString *)imgUrl andHeight:(CGFloat)height
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.height = height;
        self.imgUrl = imgUrl;
        
        [self createHeaderView];
    }
    return self;
    
}


/**
 通過圖片名稱
 */
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style andImageName:(NSString *)imgName andHeight:(CGFloat)height
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.height = height;
        self.imgName = imgName;
        
        [self createHeaderView];
    }
    return self;
}


-(void)createHeaderView
{
    self.backgroundView = [[UIView alloc] init];
    if (_img && _height) {
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, _height)];
       //圖片的contentModel 一定要設(shè)置成UIViewContentModeScaleAspectFill
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.image = _img;
        [self.backgroundView addSubview:_imgView];
        
        //給相同高度的tableHeaderView  ,避免不顯示頭部視圖,如果不添加tableHeaderView ,下拉的時(shí)候才能看見頭部的圖片
        UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, _height)];
        headerView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        self.tableHeaderView = headerView;
        
    }
    
}


-(void)setHeight:(CGFloat)height
{
    if (height) {
        _height = height;
    }
}

-(void)setImgUrl:(NSString *)imgUrl
{
    if (imgUrl) {
        
       UIImage *  img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]]];
        _img = img;
        
    }
}


-(void)setImgName:(NSString *)imgName
{
    if (imgName) {
        UIImage *img = [UIImage imageNamed:imgName];
        _img = img;
    }
}

-(void)setImg:(UIImage *)img
{
    if (img) {
        _img = img;
    }
}

@end


這個(gè)我自定義的一個(gè)TableView,下面看看YMTableView的使用方法

#import "ViewController.h"
//導(dǎo)入頭文件
#import "YMTableView.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@end

@implementation ViewController
{
    YMTableView  * mytabView;  
    CGFloat contentOffSet;//記錄視圖的偏移位置
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatTableView];
}

-(void)creatTableView
{

  //通過圖片的鏈接來實(shí)例化對象
    mytabView = [[YMTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain andImageUrl:@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3637379975,3338374522&fm=21&gp=0.jpg" andHeight:200];
    mytabView.delegate = self;
    mytabView.dataSource = self;

   //如果頭部視圖還有其他視圖的話,比如設(shè)置按鈕之類的還可以用下面的方法添加
#if 1
    UIView * headeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,mytabView.frame.size.width, 200)];
    
    headeView.backgroundColor = [UIColor colorWithWhite:.5 alpha:0];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(8, 28, 60, 30);
    [button setTitleColor:[UIColor orangeColor] forState:0];
    [button setTitle:@"設(shè)置" forState:0];
    [headeView addSubview:button];
    
    [mytabView.imgView addSubview:headeView];
    
#endif

    [self.view addSubview:mytabView];
  //具體其它渲染cell的代碼沒有影響,就不寫了
  
    
}

#pragma mark - ScrollView delegate

//*實(shí)現(xiàn)視圖拖動(dòng)時(shí)候的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    contentOffSet = scrollView.contentOffset.y;
    CGRect oldFream = mytabView.imgView.frame;
    if(contentOffSet <= mytabView.height){
        oldFream.size.height = mytabView.height-contentOffSet;
        mytabView.imgView.frame = oldFream;
    }
}


@end


下面看看效果

效果圖.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,057評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,352評論 4 61
  • 拉開窗簾,外面下起雪來了,這是南京今年的第一場雪。雪片兒大小各異,有風(fēng),像趕集似的爭先控后斜著往下墜。一會(huì)兒,風(fēng)小...
    俗然閱讀 526評論 2 4
  • 1234
    一位老師閱讀 140評論 0 0
  • 分身術(shù)的最后一個(gè)大招,關(guān)注自己的信念。信念它處在情緒和語言的背后,只有清楚自己的信念才能控制自己的情緒,最終講出正...
    朱筍筍閱讀 333評論 1 2

友情鏈接更多精彩內(nèi)容