本篇文章為大家展示了怎么在iOS中使用tableView實現下拉圖片放大效果,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
具體內容如下
#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上述內容就是怎么在iOS中使用tableView實現下拉圖片放大效果,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。