本文實例為大家分享了iOS實現下拉圖片放大效果展示的具體代碼,供大家參考,具體內容如下
#import "ViewController.h"#define kScreenbounds [UIScreen mainScreen].bounds#define kScreenWidth [UIScreen mainScreen].bounds.size.width#define kScreenHeight [UIScreen mainScreen].bounds.size.height// 宏定義一個高度#define pictureHeight 200@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) UIImageView *pictureImageView;@property (nonatomic, strong) UIView *header;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.navigationItem.title = @"向下拉伸放大圖片"; // 下面兩個屬性的設置是與translucent為NO,坐標變換的效果一樣 self.edgesForExtendedLayout = UIRectEdgeNone; self.automaticallyAdjustsScrollViewInsets = NO; [self createTableView]; }- (void)createTableView{ self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; // 添加頭視圖 在頭視圖上添加ImageView self.header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, pictureHeight)]; _pictureImageView = [[UIImageView alloc] initWithFrame:_header.bounds]; _pictureImageView.image = [UIImage imageNamed:@"picture"]; /* 重要的屬性設置 */ //這個屬性的值決定了 當視圖的幾何形狀變化時如何復用它的內容 這里用 UIViewContentModeScaleAspectFill 意思是保持內容高寬比 縮放內容 超出視圖的部分內容會被裁減 填充UIView _pictureImageView.contentMode = UIViewContentModeScaleAspectFill; // 這個屬性決定了子視圖的顯示范圍 取值為YES時,剪裁超出父視圖范圍的子視圖部分.這里就是裁剪了_pictureImageView超出_header范圍的部分. _pictureImageView.clipsToBounds = YES; [_header addSubview:_pictureImageView]; self.tableView.tableHeaderView = _header; [self.view addSubview:_tableView]; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 20;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; cell.textLabel.text = @"向下拉我"; return cell;}- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ /** * 這里的偏移量是縱向從contentInset算起 則一開始偏移就是0 向下為負 上為正 下拉 */ // 獲取到tableView偏移量 CGFloat Offset_y = scrollView.contentOffset.y; // 下拉 縱向偏移量變小 變成負的 if ( Offset_y < 0) { // 拉伸后圖片的高度 CGFloat totalOffset = pictureHeight - Offset_y; // 圖片放大比例 CGFloat scale = totalOffset / pictureHeight; CGFloat width = kScreenWidth; // 拉伸后圖片位置 _pictureImageView.frame = CGRectMake(-(width * scale - width) / 2, Offset_y, width * scale, totalOffset); }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答