图片拉伸的特效
**好多应用实现了顶部图片拉伸放大的效果,在这里也实现一下简单的拉伸效果,并且更深刻的理解了UIEdgeInsets的参数(top,left,bottom,right);
拉伸效果
关键代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
_tableView = [[UITableViewalloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.contentInset = UIEdgeInsetsMake(kZoomHeight, 0, 0, 0);
_imgViewZoom = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 240)];
self.imgViewZoom.image = [UIImage imageNamed:@"3.jpg"];
[self.tableView addSubview:self.imgViewZoom];
[self.view addSubview:self.tableView];
}
(利用UITableView继承自scrollView的代理方法)
//拉伸的效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat yOffset = scrollView.contentOffset.y;
if (yOffset < -kZoomHeight) {//向外扩展的高度
CGRect f = self.imgViewZoom.frame;
f.origin.y = yOffset;
f.size.height = -yOffset;
self.imgViewZoom.frame = f;
}
}
UIEdgeInsets的参数(top,left,bottom,right)的理解
摘自官网文档的一句话:
The inset or outset margins of the rectangle identified by the Edge property.
You can specify a value for each the edges (top, left, bottom, right). A positive value shrinks (or insets) the corresponding edge, moving it closer to the center of the button. A negative value expands (or outsets) the corresponding edge.
正值是向中心靠拢,向里缩进,负值是向外扩展,(关键代码解释如下:)
if (yOffset < -kZoomHeight) {//向外扩展的高度
CGRect f = self.imgViewZoom.frame;
f.origin.y = yOffset;
f.size.height = -yOffset;
self.imgViewZoom.frame = f;
}
备注:在扩展范围小于kZoomHeight的时候将拉伸的值赋给self.imgViewZoom.frame,否则拉伸到极限