zoukankan      html  css  js  c++  java
  • AJ学IOS 之tableView的下拉放大图片的方法

    AJ分享,必须精品

    一:效果

    tableview下拉的时候上部分图片放大会
    这里写图片描述

    二:代码

    直接上代码,自己研究吧

    
    #import "NYViewController.h"
    
    //图片的高度
    const CGFloat NYTopViewH = 350;
    
    @interface NYViewController ()
    @property (nonatomic, weak) UIImageView *topView;
    @end
    
    @implementation NYViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // 设置内边距(让cell往下移动一段距离)
        self.tableView.contentInset = UIEdgeInsetsMake(NYTopViewH * 0.5, 0, 0, 0);
    
        UIImageView *topView = [[UIImageView alloc] init];
        topView.image = [UIImage imageNamed:@"biaoqingdi"];
        topView.frame = CGRectMake(0, -NYTopViewH, 320, NYTopViewH);
    
        //设置图片内容模式,让按照原来宽高比缩放
        topView.contentMode = UIViewContentModeScaleAspectFill;
        [self.tableView insertSubview:topView atIndex:0];
        self.topView = topView;
    }
    
    #pragma mark - 数据源方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 20;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
    
        cell.textLabel.text = [NSString stringWithFormat:@"测试数据---%d", indexPath.row];
    
        return cell;
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        // 向下拽了多少距离
        CGFloat down = -(NYTopViewH * 0.5) - scrollView.contentOffset.y;
        if (down < 0) return;
    
        CGRect frame = self.topView.frame;
    
        // 5决定图片变大的速度,值越大,速度越快
        frame.size.height = NYTopViewH + down * 5;
        self.topView.frame = frame;
    }
    
    @end
    
  • 相关阅读:
    Swift 懒加载
    Swift 模型属性
    Swift 循环引用
    Two Sum
    Best Time to Buy and Sell Stock II
    Best Time to Buy and Sell Stock I
    Pascal's Triangle II
    杨辉三角(数组)
    Merge Sorted Array 合并数组
    Plus One
  • 原文地址:https://www.cnblogs.com/luolianxi/p/4990282.html
Copyright © 2011-2022 走看看