实现类似QQ个人主页图片拉动变大

    xiaoxiao2021-03-25  133

    先上图

    可惜啦,图片格式不对,先不传啦,直接上代码

    - (void)viewDidLoad {

        [super viewDidLoad];

    //    实现思路

    //    

    //    实现这一效果我们需要用到ScrollViewContentInset属性,contentInset scrollviewcontentView.frame.originscrollview.frame.origin的关系。把ScrollViewcontentView向下偏移200,留出一段空白的位置,在这段空白的位置上放上一张图片。然后在scrollView滚动的方法中,重现去设置图片的frame,下拉了多少就把image的高度放大多少。

    //    UIEdgeInsetsMake(IMAGE_HEIGHT, 0, 0, 0);1,上下偏移,2.向左右偏移,

    // tabbleViewcontentview 是从划横线的位置开始的

        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,20, self.view.bounds.size.width, self.view.bounds.size.height - 20) style:UITableViewStylePlain];

        _tableView.delegate = self;

        _tableView.dataSource = self;

        _tableView.showsVerticalScrollIndicator = NO;

        [self.view addSubview:_tableView];

        

        //设置偏移量

        _tableView.contentInset = UIEdgeInsetsMake(IMAGE_HEIGHT, 0, 0, 0);

        

        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -IMAGE_HEIGHT, self.view.bounds.size.width, IMAGE_HEIGHT)];

        imageView.image = [UIImage imageNamed:@"fengjing.jpg"];

        

        //可以使得图片在拉伸的过程中向外边延伸

        imageView.contentMode = UIViewContentModeScaleAspectFill;

        imageView.layer.masksToBounds = YES;

        [_tableView addSubview:imageView];

    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        return 10;

    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];

        if (cell == nil) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];

        }

        return cell;

    }

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{

        

        //滚动时候的偏移量,偏移量:向下为负,向右为负

        float y = scrollView.contentOffset.y;

        if (y < -IMAGE_HEIGHT) {

            CGRect frame = imageView.frame;

            frame.origin.y = y;

            frame.size.height = -y;

            imageView.frame = frame;

        }

    }

    转载请注明原文地址: https://ju.6miu.com/read-11075.html

    最新回复(0)