iOS 下拉tableView實現圖片的放大效果

screenshot.png
#import "ViewController.h"
#import "TableHeaderPic.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)TableHeaderPic *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 創建一個繼承 TableHeaderPic的子類視圖
    self.tableView = [[TableHeaderPic alloc] initWithFrame:self.view.bounds];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    // 創建一張圖片,作為tableView底層視圖,并進行跟隨放大
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 200)];
    imageView.image = [UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"pic" ofType:@"jpg"]];
    _tableView.bottomView = imageView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:identifier];
    }
    cell.textLabel.text = @"123";
    return cell;
}
// 實現此效果,必須實現下面的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.tableView startDetection:scrollView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

#import <UIKit/UIKit.h>

@interface TableHeaderPic : UITableView
@property (nonatomic,strong)UIView *bottomView;
- (void)startDetection:(UIScrollView *)scrollView;
@end

#import "TableHeaderPic.h"
#define KSetFrameY(A,B) CGRectMake(A.frame.origin.x,B,A.frame.size.width,A.frame.size.height)
@interface TableHeaderPic ()<UIScrollViewDelegate>
@property (nonatomic,assign)CGRect tempFrame;
@property (nonatomic,assign)CGPoint tempContentOffset;
@property (nonatomic,strong)UIView *headerView;
@end
@implementation TableHeaderPic
// 重寫bottom設置方法
- (void)setBottomView:(UIView *)bottomView{
    _bottomView = bottomView;
    [self addSubview:_bottomView];
    self.tempFrame = bottomView.bounds;
    self.tableHeaderView = self.headerView;
}
// 初始化headerView
- (UIView *)headerView{
    if (!_headerView) {
        self.headerView = [[UIView alloc] initWithFrame:_tempFrame];
        _headerView.backgroundColor = [UIColor clearColor];
    }
    return _headerView;
}
// 當視圖滾動是改變底層視圖的frame
- (void)startDetection:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y > 0) {return;}
    CGFloat value = ABS(scrollView.contentOffset.y);
    CGFloat scale = (self.tempFrame.size.height + value) / self.tempFrame.size.height;
    self.bottomView.transform = CGAffineTransformScale(CGAffineTransformIdentity, scale, scale);
    self.bottomView.frame = KSetFrameY(self.bottomView, scrollView.contentOffset.y);
}
@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容