1. 程式人生 > >swift-tableView滾動,裡面圖片發生偏移動畫

swift-tableView滾動,裡面圖片發生偏移動畫

  1. 在cell的content View上放一個UIView控制元件,設定cell高度與uiview的高度為300
  2. 在uiview上再新增一個UIImageView,設定左右靠邊,高度設定為400
  3. 設定圖片的填充模式為aspectFill【放張大圖】

程式碼設定:

  1. 設定tableviewHeight = 300
  2. 2.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell
(withIdentifier: "reuseIdentifier", for: indexPath) // Configure the cell... // for cell in tableView.visibleCells { let coverView = cell.contentView.viewWithTag(100) let image = coverView?.viewWithTag(200) let rect = coverView?.convert((coverView?.bounds
)!, to: nil) var y = UIScreen.main.bounds.size.height - (rect?.origin.y)! - 600 y *= 0.2 if y > 0 { y = 0 } if y < -100 { y = -100 } image?.frame.origin.y = y // } return cell }
    3.
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        for cell in tableView.visibleCells {
            let coverView = cell.contentView.viewWithTag(100)
            let image = coverView?.viewWithTag(200)

            let rect = coverView?.convert((coverView?.bounds)!, to: nil)

            var y = UIScreen.main.bounds.size.height - (rect?.origin.y)! - 400
            y *= 0.2
            if y > 0 {
                y = 0
            }
            if  y < -100 {
                y = -100
            }
            image?.frame.origin.y = y

        }
    }

動畫效果如下:
動畫效果