iOS 주류 개인 홈페이지는 스크롤을 통해 헤더 이미지를 확대/축소할 수 있습니다.

그 결과 위로 스크롤하면 그림이 더 좁아지고 위로 스크롤하면 그림이 변경되지 않고 그대로 유지됩니다.

맞춤 헤더

#import <UIKit/UIKit.h>

@interface ZoomHeaderView : UIView

- (void)updateHeaderImageViewFrameWithOffsetY:(CGFloat)offsetY;

@end

#import "ZoomHeaderView.h"

@interface ZoomHeaderView ()

@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, assign) CGRect originalHeaderImageViewFrame;

@end

@implementation ZoomHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        headerImageView.clipsToBounds = YES;
        headerImageView.contentMode = UIViewContentModeScaleAspectFill;
        headerImageView.image = [UIImage imageNamed:@"head.png"];
        [self addSubview:headerImageView];
        self.headerImageView = headerImageView;
        self.originalHeaderImageViewFrame = self.bounds;
    }
    return self;
}

//根据offsetY更新布局
- (void)updateHeaderImageViewFrameWithOffsetY:(CGFloat)offsetY
{
    //防止height小于0
    if (self.originalHeaderImageViewFrame.size.height - offsetY < 0) {
        return;
    }
    //如果不使用约束的话,图片的y值要上移offsetY,同时height也要增加offsetY
    CGFloat x = self.originalHeaderImageViewFrame.origin.x;
    CGFloat y = self.originalHeaderImageViewFrame.origin.y + offsetY;
    CGFloat width = self.originalHeaderImageViewFrame.size.width;
    CGFloat height = self.originalHeaderImageViewFrame.size.height - offsetY;
    self.headerImageView.frame = CGRectMake(x, y, width, height);
}

@end

외부 통화

#import "ViewController.h"
#import "ZoomHeaderView.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,strong) ZoomHeaderView *headerView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];

    ZoomHeaderView *headerView = [[ZoomHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
    tableView.tableHeaderView = headerView;
    self.headerView = headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *equipmentCellId = @"equipmentCellId";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:equipmentCellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:equipmentCellId];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];

    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    [self.headerView updateHeaderImageViewFrameWithOffsetY:offsetY];
}

자세한 내용은 아래
에서 확인하세요 . http://www.jianshu.com/p/9c86eb8c1b76

Supongo que te gusta

Origin blog.csdn.net/baidu_33298752/article/details/51782711
Recomendado
Clasificación