iOS:下拉时图片拉伸效果

下拉时图片拉伸效果经常会遇到,这次项目中用到,思考了下写了两种思路的实现:

  1. 利用[tableview insertSubview:imageView atIndex:0],将UIImageView加到tableview上;但是需要考虑到滚动时,UIImageView也会进行滚动,此时在滚动时需要改变UIImageView的origin.y。贴一下主要代码如下:
- (void)dropDownImage {
//0.56是图片的比例
    tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    tableview.dataSource = self;
    tableview.delegate = self;
    tableview.contentInset = UIEdgeInsetsMake(0.56*CGRectGetWidth(self.view.frame), 0, 0, 0 );
    //适配ios11的安全区域偏移量,用UIScrollView的contentInsetAdjustmentBehavior属性或者用UIViewController的additionalSafeAreaInsets属性来解决
    tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    //    self.additionalSafeAreaInsets = UIEdgeInsetsMake(-44, 0, 0, 0);
    tableview.backgroundColor = [UIColor clearColor];
    UIImage *image = [UIImage imageNamed:@"1212"];
    imageViewFrame = CGRectMake(0, -0.56*CGRectGetWidth(self.view.frame), CGRectGetWidth(self.view.frame), 0.56*CGRectGetWidth(self.view.frame));
    imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
    imageView.image = image;
    [tableview insertSubview:imageView atIndex:0];
    [self.view addSubview:tableview];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    float y = scrollView.contentOffset.y;
    float height = imageViewFrame.size.height;
    if (y < -height) {
        CGFloat Ky = -scrollView.contentOffset.y-height;
        //除以2是x轴在拉伸的过程也需要位伸,需要居中拉伸,故/2
        CGFloat Kx = (Ky/height)*CGRectGetWidth(imageViewFrame)/2;
        imageView.frame = CGRectMake(0-Kx, 0-Ky-height,
                                      CGRectGetWidth(imageViewFrame)+2*Kx, height+Ky);
    }
}
  1. 利用[self.view addSubview:imageView],将UIImageView加到viewController上,再加tableview。那么UIImageView将在tableview的下层。但是这里tableview会挡住下层的UIImageView,考虑用tableHeaderView预留出跟UIImageView同样高度的视图出来,并将背景色置为透明,这样将会显示下层的UIImageView。
UIImage *image = [UIImage imageNamed:@"1212"];
    imageViewFrame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 0.56*CGRectGetWidth(self.view.frame));
    imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
    imageView.image = image;
    [self.view addSubview:imageView];
    tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    tableview.dataSource = self;
    tableview.delegate = self;
    //适配ios11的安全区域偏移量,用uiscrollview的contentInsetAdjustmentBehavior属性或者用uiviewcontroller的additionalSafeAreaInsets属性来解决
    tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//    self.additionalSafeAreaInsets = UIEdgeInsetsMake(-44, 0, 0, 0);
    tableview.backgroundColor = [UIColor clearColor];
    headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), imageViewFrame.size.height)];
    headerView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    tableview.tableHeaderView = headerView;
    [self.view addSubview:tableview];

我们debug一下view Hierarchy,如下:


image.png

后面代码就是往下滚动时,UIImageView的拉伸效果了;往上滚动时做了个透明度变化的效果。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    float y = scrollView.contentOffset.y;
    float height = imageViewFrame.size.height;
    if (y < 0) {
        float nx = ((-y + height) / 0.56 - imageViewFrame.size.width)/ 2;
        float nh = -y + height;
        NSLog(@"aa:%.f",nx);
        imageView.frame = CGRectMake(-nx, 0, CGRectGetWidth(imageViewFrame)+nx*2, nh);
    }else {
        float al = y / height;
        headerView.backgroundColor = [UIColor colorWithWhite:1 alpha:al];
        imageView.frame = CGRectMake(0, -y, CGRectGetWidth(self.view.frame), 0.56*CGRectGetWidth(self.view.frame));
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,226评论 3 119
  • 1.柚子加科技提供的全部任务信息仅依照该信息提供时的现状提供并仅供用户参考,柚子加科技不对前述信息的准确性、完整性...
    柚子加科技阅读 579评论 0 0
  • 三观很正,擅长思考和反省自己,有自己的看法和观点但也乐意接纳别人不同的意见。哈哈哈!
    莲喵喵阅读 222评论 0 0
  • 开发之前 大约从去年开始吧, 也可能是前年 Html5好像火得不得了, 不得了...总能从网上听说到 XXX混合开...
    changer0阅读 13,287评论 7 40

友情链接更多精彩内容