图片拉伸放大效果

图片拉伸的特效

**好多应用实现了顶部图片拉伸放大的效果,在这里也实现一下简单的拉伸效果,并且更深刻的理解了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,否则拉伸到极限
图片可拉伸,顶部坐标不变

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值