tableView下拉图片放大
2015-07-03 12:02
302 查看
其实这个效果,本质上就是在你tableView下拉 造成offset时候, 保持你顶部图片的y坐标点还停留在下拉时屏幕的顶点(offset), 而图片的长度变为原始的height长度-(offset ) 就达到了下拉放大的效果。
直接上代码了:
1. 首先创建一个UIView作为headerView
_topView = [[UIView
alloc]initWithFrame:CGRectMake(0,
0, MCAPPWidth,
80)];
_tableView.tableHeaderView =
_topView;
2.其次创建一个UIImageView放那张图片
_topImageView = [[UIImageView
alloc]initWithFrame:CGRectMake(0,
0, MCAPPWidth,
80)];
_topImageView.contentMode =
UIViewContentModeScaleAspectFill;
_topImageView.layer.masksToBounds =
YES;
[_topImageView
sd_setImageWithURL:[NSURL
URLWithString:nil]
placeholderImage:[UIImage
imageNamed:@"mian_bg"]];
[_topView
addSubview:_topImageView];
3.最后引入scrollView的代理方法 监听frame的变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (_tableView == scrollView) {
CGFloat yOffset = _tableView.contentOffset.y;
//下拉图片放大
if (yOffset < 0) {
_topView.frame = CGRectMake(0,
yOffset, MCAPPWidth, 80 * Height - yOffset);
_topImageView.frame = CGRectMake(0,
yOffset, MCAPPWidth, 80 * Height - yOffset);
NSLog(@"%.2f", _topImageView.y);
}
else {
_topView.frame = CGRectMake(0, 0, MCAPPWidth, 80 * Height -
yOffset);
_topImageView.y = yOffset * 0.65;
_topImageView.height = 80 * Height -
yOffset * 0.65;
}
}
}
我这里上拉 图片做的是相对位移 如果只需要下拉变大效果 上拉回原样 需要把else 里的 后两句代码注释掉, 换成_topImageView.y = _topView.y值保持一样即可。 我这里的.y是自己封装的方法直接copy会出错哦。
直接上代码了:
1. 首先创建一个UIView作为headerView
_topView = [[UIView
alloc]initWithFrame:CGRectMake(0,
0, MCAPPWidth,
80)];
_tableView.tableHeaderView =
_topView;
2.其次创建一个UIImageView放那张图片
_topImageView = [[UIImageView
alloc]initWithFrame:CGRectMake(0,
0, MCAPPWidth,
80)];
_topImageView.contentMode =
UIViewContentModeScaleAspectFill;
_topImageView.layer.masksToBounds =
YES;
[_topImageView
sd_setImageWithURL:[NSURL
URLWithString:nil]
placeholderImage:[UIImage
imageNamed:@"mian_bg"]];
[_topView
addSubview:_topImageView];
3.最后引入scrollView的代理方法 监听frame的变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (_tableView == scrollView) {
CGFloat yOffset = _tableView.contentOffset.y;
//下拉图片放大
if (yOffset < 0) {
_topView.frame = CGRectMake(0,
yOffset, MCAPPWidth, 80 * Height - yOffset);
_topImageView.frame = CGRectMake(0,
yOffset, MCAPPWidth, 80 * Height - yOffset);
NSLog(@"%.2f", _topImageView.y);
}
else {
_topView.frame = CGRectMake(0, 0, MCAPPWidth, 80 * Height -
yOffset);
_topImageView.y = yOffset * 0.65;
_topImageView.height = 80 * Height -
yOffset * 0.65;
}
}
}
我这里上拉 图片做的是相对位移 如果只需要下拉变大效果 上拉回原样 需要把else 里的 后两句代码注释掉, 换成_topImageView.y = _topView.y值保持一样即可。 我这里的.y是自己封装的方法直接copy会出错哦。
相关文章推荐
- Android Fragment 真正彻底的解决(下一个)
- 跨域请求数据解决方案整理
- Visual Studio中修改项目的输出目录
- leetcode 23: Merge k Sorted Lists
- 【RAC】 RAC For W2K8R2 安装--卸载(八)
- tomcat8项目引入servlet-api.jar包后报错
- Objective-C 之 NSNotificationCenter vs delegate vs KVO
- informatica 在抽取EXCEL中遇到问题及INFA售后给出的建议
- iOS 笔试题
- 监听网络状态
- NSRange,判断字符串的各种操作~
- HDU 4508 沼泽湿地系列故事——记住减肥I (2013腾讯编程马拉松预赛第一)
- MySQL 求时间差
- leetcode 22: Generate Parentheses
- Android中保存图片的两种方式
- cocos2dx3.6 实现带光标的输入框(二);光标可移动
- java java web及tomcat的使用
- 使用css绘制六边形
- leetcode 21: Merge Two Sorted Lists
- Java时间比较