AJ學IOS 之tableView的下拉放大圖片的方法

AJ分享,必須精品markdown

 

一:效果

tableview下拉的時候上部分圖片放大會
這裏寫圖片描述測試

二:代碼

直接上代碼,本身研究吧ui

#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
相關文章
相關標籤/搜索