tableview向下拖动,顶部的图片变大,当tableview被放回时,图片自动变回原先大小
2014-11-14 02:45
375 查看
经常在以下应用中看到这样的效果:在tableview的上边,有一个背景图片,当向下拖动tableview时,顶部的背景图片自动变大,当tableview被放回的时候,图片自动还原。
[b]一:效果图:[/b]
正常图片:
向下拖动图片:
二:原理如下:
1,假如,控制器就是tableviewcontroller,设置tableview的顶部内边距,将tableview的顶部留出来。
2,在tableview中,放入一个imageview,一定要放入最底部,将imageview设置大一点
3,设置imageview的内容模式为等比例拉伸(这是最重要的)
4,当tableview拖动的时候,计算拖动的距离。如果是向下拖动,将imageview的高度变大,那么,宽度会自动变大,这样,上边的图片看起来就会等比例变大。当tableview向上反弹或者向上拖动的时候,这时候,要注意了,imageview就不在自动变了,维持先前的大小。
三:示意图:
四:事例代码:(为了方便,就直接将控制器整成一个tableviewController)
#import "CZViewController.h"
#import "UIView+Extension.h"
CGFloat
const CZInitH = 320;
CGFloat
const CZInitW = 320;
CGFloat
const CZInsetTop =
150;
@interface
CZViewController ()
@property(nonatomic,strong)
UIImageView *imageView;
@end
@implementation CZViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
//先设置tableview的顶部内边距
self.tableView.contentInset =
UIEdgeInsetsMake(CZInsetTop,
0, 0,
0);
UIImageView *imageView = [[UIImageView
alloc] init];
imageView.image = [UIImage
imageNamed:@"biaoqingdi"];
//imageview的一部分在屏幕外,一部分在屏幕中
imageView.frame =
CGRectMake(0, -CZInitH,
CZInitW, CZInitH);
//设置imageView等比例伸缩
imageView.contentMode =
UIViewContentModeScaleAspectFill;
[self.tableView
insertSubview:imageView
atIndex:0];
self.imageView = imageView;
}
/** tableview已经拖拽 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//计算往下拖拽的距离
CGFloat dragDelta = -CZInsetTop - scrollView.contentOffset.y;
if(dragDelta <
0){
//如果dragDelta<0,说明,tableview向上拖动
dragDelta =
0;
}
//设置imageview的高度
self.imageView.height =
CZInitH + dragDelta;
}
#pragma mark - tableview的数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static NSString *ID =
@"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ID];
if(cell == nil){
cell = [[UITableViewCell
alloc] initWithStyle:UITableViewCe
bd42
llStyleDefault
reuseIdentifier:ID];
}
cell.textLabel.text = [NSString
stringWithFormat:@"测试数据-%d",indexPath.row];
return cell;
}
@end
[b]一:效果图:[/b]
正常图片:
向下拖动图片:
二:原理如下:
1,假如,控制器就是tableviewcontroller,设置tableview的顶部内边距,将tableview的顶部留出来。
2,在tableview中,放入一个imageview,一定要放入最底部,将imageview设置大一点
3,设置imageview的内容模式为等比例拉伸(这是最重要的)
4,当tableview拖动的时候,计算拖动的距离。如果是向下拖动,将imageview的高度变大,那么,宽度会自动变大,这样,上边的图片看起来就会等比例变大。当tableview向上反弹或者向上拖动的时候,这时候,要注意了,imageview就不在自动变了,维持先前的大小。
三:示意图:
四:事例代码:(为了方便,就直接将控制器整成一个tableviewController)
#import "CZViewController.h"
#import "UIView+Extension.h"
CGFloat
const CZInitH = 320;
CGFloat
const CZInitW = 320;
CGFloat
const CZInsetTop =
150;
@interface
CZViewController ()
@property(nonatomic,strong)
UIImageView *imageView;
@end
@implementation CZViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
//先设置tableview的顶部内边距
self.tableView.contentInset =
UIEdgeInsetsMake(CZInsetTop,
0, 0,
0);
UIImageView *imageView = [[UIImageView
alloc] init];
imageView.image = [UIImage
imageNamed:@"biaoqingdi"];
//imageview的一部分在屏幕外,一部分在屏幕中
imageView.frame =
CGRectMake(0, -CZInitH,
CZInitW, CZInitH);
//设置imageView等比例伸缩
imageView.contentMode =
UIViewContentModeScaleAspectFill;
[self.tableView
insertSubview:imageView
atIndex:0];
self.imageView = imageView;
}
/** tableview已经拖拽 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//计算往下拖拽的距离
CGFloat dragDelta = -CZInsetTop - scrollView.contentOffset.y;
if(dragDelta <
0){
//如果dragDelta<0,说明,tableview向上拖动
dragDelta =
0;
}
//设置imageview的高度
self.imageView.height =
CZInitH + dragDelta;
}
#pragma mark - tableview的数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static NSString *ID =
@"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ID];
if(cell == nil){
cell = [[UITableViewCell
alloc] initWithStyle:UITableViewCe
bd42
llStyleDefault
reuseIdentifier:ID];
}
cell.textLabel.text = [NSString
stringWithFormat:@"测试数据-%d",indexPath.row];
return cell;
}
@end
相关文章推荐
- 拖动 tableView 来缩放顶部图片
- WebView加载HTML图片大小自适应与文章自动换行
- iOS开发-tableView顶部图片拉伸
- ios实现tableView顶部弹簧图片效果
- WebView加载HTML图片大小自适应与文章自动换行
- swift 下拉放大tableView顶部图片 或 放大顶部地图
- iOS tableView实现顶部图片拉伸效果
- 自定义View中图片不能根据屏幕大小自动创建
- WebView加载HTML图片大小自适应与文章自动换行
- 【iOS】WebView加载HTML图片大小自适应与文章自动换行
- Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片
- 自定义控件---继承ViewGroup类方式(循序渐进之第2步效果----图片左右拖动+自动回弹效果)
- iOS开发中tableview中cell图片大小自定义方法
- iOS tableView顶部图片下拉缩放效果实现
- iOS tableView下拉顶部视图放大 自动滚动 无限滚动 自动布局 在code4App和github上早就上传了代码,这边记录下
- 自动调整TextView字体大小以适应文字长度&拖动一个角增大textView面积
- IOS 根据屏幕高度设置tableview中图片元素的大小
- iOS开发——WebView加载HTML图片大小自适应与文章自动换行
- ios 小问题,滑动tableview,导航出去,再回来会保持原样,但我想自动保持顶部
- iOS之设置tableView背景图片/让UITableView自动滑动(定位)到某一行cell