现在很多的app都有这个功能
先上效果图
#define IMAGEHEIGHT 200.0f
#define MAINSCREENWIDTH [UIScreen mainScreen].bounds.size.width
先添加两个UIImageView
//大图片
_zoomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, IMAGEHEIGHT)];
_zoomImageView.image = [UIImage imageNamed:@"iPhone_personal_head_bg"];
_zoomImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:_zoomImageView];
// 小图片
_littleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(MAINSCREENWIDTH/2 - 50, 50, 100, 100)];
_littleImageView.image = [UIImage imageNamed:@"loadingIcon"];
[_zoomImageView addSubview:_littleImageView];
//添加一下渐变暗视图
_effectView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, IMAGEHEIGHT)];
_effectView.backgroundColor = [UIColor blackColor];
[_zoomImageView addSubview:_effectView];
//图片下是一个表格
//当滚动时动态计算高度
CGFloat changeY = 0.0;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat y = scrollView.contentOffset.y; //如果有导航控制器,这里应该加上导航控制器的高度64
changeY = y + 200;
NSLog(@"%lf",y);
if (y< -IMAGEHEIGHT) {
CGRect frame = _zoomImageView.frame;
frame.size.height = -y;
_zoomImageView.frame = _effectView.frame = frame;
CGPoint center = _littleImageView.center;
NSLog(@"changeY %lf",changeY);
center.y = 100-changeY/2;
_littleImageView.center = center;
}else{
_effectView.alpha = 1 + y/200;
}
changeY -= y;
}
这样就能简单实现上推隐藏 下拉放大 小图片大小不变的效果