QQ空间的下拉放大图片 其实原理 很简单 只需要改变下网络或者本地加载图片的frame即可,
首先 创建一个新的类,继承于UIView,同时写几个方法
//初始化图片
- (instancetype)initWithFrame:(CGRect)frame withImages:(NSString *)imageName;
//加载网络图片 图片拉伸
- (instancetype) initWithFrame:(CGRect)frame WithImages:(NSURL *)imageUrl PlaceholderImage:(UIImage *)placeholder;
//图片放的的方法
- (void)imageViewStretchingWithOffSet:(CGFloat)offset;
当然、.m里面实现这三个方法
//初始化图片
- (instancetype)initWithFrame:(CGRect)frame withImages:(NSString *)imageName
{
self = [super initWithFrame:frame];
if (self) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
imageView.image = [UIImage imageNamed:imageName];
self.localImageView = imageView;
self.orginWidth = imageView.frame.size.width;
self.orginHeight = imageView.frame.size.height;
self.isLocal = localImage;
[self addSubview:imageView];
}
return self;
}
//加载网络图片 图片拉伸
- (instancetype) initWithFrame:(CGRect)frame WithImages:(NSURL *)imageUrl PlaceholderImage:(UIImage *)placeholder
{
self = [super initWithFrame:frame];
if (self) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[imageView sd_setImageWithURL:imageUrl placeholderImage:placeholder];
self.netImageView = imageView;
self.orginWidth = imageView.frame.size.width;
self.orginHeight = imageView.frame.size.height;
self.isLocal = netImage;
[self addSubview:imageView];
}
return self;
}
//图片放的的方法
- (void)imageViewStretchingWithOffSet:(CGFloat)offset
{
CGFloat whPercent = self.orginWidth/self.orginHeight;//原始比例
CGFloat height = self.orginHeight - offset;//拉伸后的高度
CGFloat width = self.orginWidth - offset * whPercent;//拉伸后的宽度
if (offset < -1) {
if (self.isLocal == localImage) {
self.localImageView.frame = CGRectMake(offset/3*2, offset, width, height);
}
if (self.isLocal == netImage) {
self.netImageView.frame = CGRectMake(offset/3*2, offset, width, height);
}
}
else {
if (self.isLocal == localImage) {
self.localImageView.frame = CGRectMake(0, 0, self.orginWidth, self.orginHeight);
}
if (self.isLocal == netImage) {
self.netImageView.frame = CGRectMake(0, 0, self.orginWidth, self.orginHeight);
}
}
}
然后, 我们在ViewController里面创建视图视图继承于 创建的类
在ViewController.m里面 调用ScrollView的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY < -1) {
if (offsetY > -100) {
//修改本地
[self.netImageView imageViewStretchingWithOffSet:offsetY];
self.NavbgImg.hidden = YES;
}
}
//导航栏
if (offsetY > -19) {
if (offsetY < 210) {
self.NavbgImg.hidden = NO;
self.NavbgImg.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.8 alpha:offsetY / 210];
} else {
self.NavbgImg.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.8 alpha:1];
self.NavbgImg.hidden = NO;
}
} else {
self.NavbgImg.hidden = YES;
}
}
即可 ,当然此处的导航栏也需要自定义的,
到此为止,下拉放大图片就完成了,如果有什么错误,希望大家多多指教,小弟不胜感激!!!