效果展示
ViewController.m
#import "ViewController.h"
#import "Model.h"
#import "FrameModel.h"
#import "TableViewCell.h"
@property(nonatomic,strong)UIImageView *headImageView;//頭部圖片
@property(nonatomic,strong)UITableView *tableView;//列表
@property (nonatomic, strong) NSArray *InfoArray;//數(shù)組
//屏幕寬抵怎、高 宏定義
#define IPHONE_W ([UIScreen mainScreen].bounds.size.width)
#define IPHONE_H ([UIScreen mainScreen].bounds.size.height)
// 頭視圖高度
static CGFloat kImageOriginHight = 250;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//將視圖添加到界面上
[self.view addSubview:self.tableView];
[self.tableView addSubview:self.headImageView];
//隱藏系統(tǒng)tableViewCell分割線(xiàn)
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//隱藏垂直滾動(dòng)條
self.tableView.showsVerticalScrollIndicator = NO;
[self getInfo];
}
#pragma mark -- 滾動(dòng)視圖的代理方法
- (void)scrollViewDidScroll:(UIScrollView*)scrollView{
/**
* 關(guān)鍵處理:通過(guò)滾動(dòng)視圖獲取到滾動(dòng)偏移量從而去改變圖片的變化
*/
//獲取滾動(dòng)視圖y值的偏移量
CGFloat yOffset = scrollView.contentOffset.y;
NSLog(@"yOffset===%f",yOffset);
CGFloat xOffset = (yOffset +kImageOriginHight)/2;
if(yOffset < -kImageOriginHight) {
CGRect f =self.headImageView.frame;
f.origin.y= yOffset ;
f.size.height= -yOffset;
f.origin.x= xOffset;
//int abs(int i); // 處理int類(lèi)型的取絕對(duì)值
//double fabs(double i); //處理double類(lèi)型的取絕對(duì)值
//float fabsf(float i); //處理float類(lèi)型的取絕對(duì)值
f.size.width=IPHONE_W + fabs(xOffset)*2;
self.headImageView.frame= f;
}
}
#pragma mark -- 表視圖代理
- (void)getInfo
{
//實(shí)際開(kāi)發(fā)數(shù)據(jù)是網(wǎng)絡(luò)獲取到的九巡,這里模擬給出一個(gè)數(shù)據(jù)
NSArray *array = @[
@{@"name" : @"aaa", @"icon" : @"icon", @"text" : @"這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容", @"picture" : @"hero.jpg"},
@{@"name" : @"bbb", @"icon" : @"icon", @"text" : @"這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容這里是內(nèi)容", @"picture" : @"hero.jpg"},
@{@"name" : @"ccccc", @"icon" : @"icon", @"text" : @"這里是內(nèi)容没讲,沒(méi)有配圖"},
@{@"name" : @"ddd", @"icon" : @"icon", @"picture" : @"hero.jpg"}];
//解析數(shù)據(jù)懒豹,轉(zhuǎn)模型保存
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in array) {
Model *model = [Model modelWithDict:dict];
FrameModel *frameModel = [[FrameModel alloc] init];
frameModel.model = model;
[tempArray addObject:frameModel];
}
self.InfoArray = [tempArray copy];
NSLog(@"%ld",_InfoArray.count);
}
#pragma mark - Table view data source
//組數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//組中行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.InfoArray.count;
}
//cell內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [TableViewCell cellWIthTableView:tableView];
cell.frameModel = self.InfoArray[indexPath.row];
return cell;
}
//設(shè)置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
FrameModel *frameModel = self.InfoArray[indexPath.row];
return frameModel.cellHeight;
}
#pragma mark -- get 初始化操作
-(UITableView *)tableView
{
if (_tableView == nil)
{
_tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0,IPHONE_W,IPHONE_H)];
_tableView.delegate=self;
_tableView.dataSource=self;
_tableView.backgroundColor= [UIColor lightGrayColor];
//內(nèi)容由kImageOriginHight 處開(kāi)始顯示这溅。
_tableView.contentInset=UIEdgeInsetsMake(kImageOriginHight,0,0,0);
}
return _tableView;
}
-(UIImageView *)headImageView
{
if (_headImageView == nil)
{
_headImageView= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"111.jpg"]];
_headImageView.frame=CGRectMake(0, -kImageOriginHight,IPHONE_W,kImageOriginHight);
}
return _headImageView;
}
Model.h
#import <Foundation/Foundation.h>
@interface Model : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *picture;
- (id)initWithDict:(NSDictionary *)dict;
+ (id)modelWithDict:(NSDictionary *)dict;
@end
Model.m
#import "Model.h"
@implementation Model
- (id)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (id)modelWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end
FrameModel.h
@class Model;
@interface FrameModel : NSObject
@property (nonatomic, assign) CGRect iconFrame;
@property (nonatomic, assign) CGRect nameFrame;
@property (nonatomic, assign) CGRect textFrame;
@property (nonatomic, assign) CGRect pictureFrame;
@property (nonatomic, assign) CGFloat cellHeight;
@property (nonatomic, strong) Model *model;
@end
FrameModel.m
#import "FrameModel.h"
#import "Model.h"
#define mainW [UIScreen mainScreen].bounds.size.width
#define HWTextFont [UIFont systemFontOfSize:15]
@implementation FrameModel
- (void)setModel:(Model *)model
{
_model = model;
//頭像
CGFloat padding = 10;
CGFloat iconWH = 30;
self.iconFrame = CGRectMake(padding, padding, iconWH, iconWH);
//名字
CGSize nameSize = [self sizeWithText:model.name font:HWTextFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
CGFloat nameW = nameSize.width;
CGFloat nameH = nameSize.height;
CGFloat nameX = CGRectGetMaxX(self.iconFrame) + padding;
CGFloat nameY = CGRectGetMinY(self.iconFrame);
self.nameFrame = CGRectMake(nameX, nameY, nameW, nameH);
//文字內(nèi)容
CGSize textSize = [self sizeWithText:model.text font:HWTextFont maxSize:CGSizeMake(mainW - padding * 2, MAXFLOAT)];
self.textFrame = CGRectMake(padding, iconWH + padding * 2, textSize.width, textSize.height);
//配圖
if (model.picture) {
self.pictureFrame = CGRectMake(padding, CGRectGetMaxY(self.textFrame) + padding, 120, 120);
_cellHeight = CGRectGetMaxY(self.pictureFrame) + padding;
}
else {
_cellHeight = CGRectGetMaxY(self.textFrame) + padding;
}
}
//根據(jù)字體大小、限定長(zhǎng)度動(dòng)態(tài)獲取文字寬高尺寸
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *dict = @{NSFontAttributeName : font};
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
}
TableViewCell.h
#import <UIKit/UIKit.h>
@class FrameModel;
@interface TableViewCell : UITableViewCell
@property (nonatomic, strong) FrameModel *frameModel;
+ (instancetype)cellWIthTableView:(UITableView *)tableView;
@end
TableViewCell.m
#import "TableViewCell.h"
#import "Model.h"
#import "FrameModel.h"
#define HWTextFont [UIFont systemFontOfSize:15]
@interface TableViewCell ()
@property (nonatomic, weak) UIImageView *icon;
@property (nonatomic, weak) UILabel *name;
@property (nonatomic, weak) UILabel *text;
@property (nonatomic, assign) UIImageView *picture;
@end
@implementation TableViewCell
+ (instancetype)cellWIthTableView:(UITableView *)tableView
{
//cell復(fù)用,唯一標(biāo)識(shí)
static NSString *identifier = @"HWCell";
//先在緩存池中取
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//緩存池中沒(méi)有再創(chuàng)建鹦付,并添加標(biāo)識(shí)昆禽,cell移出屏幕時(shí)放入緩存池以復(fù)用
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}
//重寫(xiě)init方法構(gòu)建cell內(nèi)容
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//取消點(diǎn)擊高亮狀態(tài)
self.selectionStyle = UITableViewCellSelectionStyleNone;
//頭像
UIImageView *icon = [[UIImageView alloc] init];
[self.contentView addSubview:icon];
self.icon = icon;
//名字
UILabel *name = [[UILabel alloc] init];
name.font = HWTextFont;
[self.contentView addSubview:name];
self.name = name;
//內(nèi)容
UILabel *text = [[UILabel alloc] init];
text.numberOfLines = 0;
text.font = HWTextFont;
[self.contentView addSubview:text];
self.text = text;
//配圖
UIImageView *picture = [[UIImageView alloc] init];
[self.contentView addSubview:picture];
self.picture = picture;
}
return self;
}
//重寫(xiě)set方法蝗蛙,模型傳遞
- (void)setFrameModel:(FrameModel *)frameModel
{
_frameModel = frameModel;
Model *model = frameModel.model;
self.icon.image = [UIImage imageNamed:model.icon];
self.icon.frame = frameModel.iconFrame;
self.name.text = model.name;
self.name.frame = frameModel.nameFrame;
self.text.text = model.text;
self.text.frame = frameModel.textFrame;
self.picture.image = [UIImage imageNamed:model.picture];
self.picture.frame = frameModel.pictureFrame;
}