iOS下拉圖片放大

來源:互聯網
上載者:User

標籤:

開始簡單的代碼過程

其實思路很簡單 就是 讓tableView位移 一圖片的高度,然後在把圖片添加到tableView中,然後再監聽didScrollView,在裡面改變圖片的frame

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.automaticallyAdjustsScrollViewInsets = NO; _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, navH, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - navH) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; /* 設定tableView的位移量 **/ _tableView.contentInset = UIEdgeInsetsMake(startImageViewH, 0, 0, 0); [self.view addSubview:_tableView]; /* * 注意 imageView 的Y 值是-startImageViewH 其實這個值就是 tableView的內容位移量 * 如果Y 是0的話它還是會跟著tableView位移startImageViewH 值 **/ UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -startImageViewH, CGRectGetWidth(self.view.frame), startImageViewH)]; imageView.image = [UIImage imageNamed:@"1"]; self.imageView = imageView; [self.tableView addSubview:imageView];}- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetY = scrollView.contentOffset.y; NSLog(@"offsetY = %f",offsetY); /* 計算往下拉的時候位移量 **/ if (offsetY < -startImageViewH) { /* 計算差值 **/ CGFloat interval = -CGRectGetHeight(self.imageView.frame) - offsetY; /* imageView 的高度 增加 interval **/ CGFloat imageViewH = CGRectGetHeight(self.imageView.frame) + interval; /* 計算 imageView的 高度增加後的,寬度增加後的值 **/ CGFloat imageViewW = imageViewH*CGRectGetWidth(self.imageView.frame)/CGRectGetHeight(self.imageView.frame); /* 計算 imageView X 的位移量 **/ CGFloat imageViewX =self.imageView.frame.origin.x - (imageViewW -CGRectGetWidth(self.imageView.frame))/2; /* 計算 imageView Y 的位移量 **/ CGFloat imageViewY =self.imageView.frame.origin.y - interval; /* 賦值 **/ self.imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH); }}

demo地址:https://github.com/lichory/pullDownImageView

 

本文借鑒簡書作者李重陽_arc的文章,寫在這裡是為了在自己的部落格裡做一份備份。

iOS下拉圖片放大

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.