iOS tableView實現(xiàn)下拉圖片放大效果
更新時間:2018年05月08日 15:49:03 作者:walkerwqp
這篇文章主要為大家詳細介紹了iOS tableView實現(xiàn)下拉圖片放大效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS實現(xiàn)下拉圖片放大效果展示的具體代碼,供大家參考,具體內(nèi)容如下
#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 = @"向下拉伸放大圖片";
// 下面兩個屬性的設(shè)置是與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"];
/*
重要的屬性設(shè)置
*/
//這個屬性的值決定了 當(dāng)視圖的幾何形狀變化時如何復(fù)用它的內(nèi)容 這里用 UIViewContentModeScaleAspectFill 意思是保持內(nèi)容高寬比 縮放內(nèi)容 超出視圖的部分內(nèi)容會被裁減 填充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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- iOS實現(xiàn)點擊圖片放大和長按保存圖片的示例
- iOS 點擊圖片放大效果的實現(xiàn)
- 解決iOS11圖片下拉放大出現(xiàn)信號欄白條的bug問題
- 利用iOS手勢與scrollView代理實現(xiàn)圖片的放大縮小
- iOS tableView實現(xiàn)頂部圖片拉伸效果
- iOS tableview實現(xiàn)頂部拉伸效果
- iOS TableView頭視圖根據(jù)偏移量下拉縮放效果
- iOS tableView實現(xiàn)頭部拉伸并改變導(dǎo)航條漸變色
- iOS應(yīng)用開發(fā)中UITableView的分割線的一些設(shè)置技巧
- IOS UITableView和UITableViewCell的幾種樣式詳細介紹
相關(guān)文章
iOS測試手機APP的方法匯總:真機運行,打ipa包,testFlighe,蒲公英
這篇文章主要介紹了iOS通常測試手機APP的四種方法:真機運行,打ipa包,(testFlighe)郵件,蒲公英測試。需要的朋友可以參考下2022-12-12
iOS端React Native差異化增量更新的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于iOS端React Native差異化增量更新的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
iOS中使用NSURLConnection處理HTTP同步與異步請求
NSURLConnection的作用現(xiàn)在已經(jīng)基本被NSURLSession所取代,所以我們簡單了解下iOS中使用NSURLConnection處理HTTP同步與異步請求的方法即可:2016-07-07
iOS?UITextView?實現(xiàn)類似微博的話題、提及用戶效果
這篇文章主要介紹了iOS?UITextView?實現(xiàn)類似微博的話題、提及功能,基本思路是使用正則匹配出成對的#,再利用UITextView的富文本實現(xiàn)高亮效果,需要的朋友可以參考下2022-06-06
iOS開發(fā)之UITableView左滑刪除等自定義功能
今天來給大家介紹下iOS開發(fā)中UITableView左滑實現(xiàn)微信中置頂,刪除等功能。對大家開發(fā)iOS具有一定的參考借鑒價值,有需要的朋友們一起來看看吧。2016-09-09

