tableViewCell的动画效果及动态高度计算

动画图git效果不是很好,实际效果比较好些
这里写图片描述

这里写图片描述
这里写图片描述

import UIKit

class MyViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    @IBOutlet weak var myTableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
             // 返回的是相对于屏幕的位置
        let rect = bottomView?.convert(bottomView!.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
        }
        imageView?.frame.origin.y = Y
        return cell 
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 300
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = .zero
        cell.layoutMargins = .zero
    }

    override func viewDidLayoutSubviews() {
        self.myTableView.separatorInset = .zero
        self.myTableView.layoutMargins = .zero
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // 重点:
        for cell in myTableView.visibleCells{
            let bottomView = cell.contentView.viewWithTag(2000)
            let imageView = bottomView!.viewWithTag(2001)

            // 返回的是相对于屏幕的位置
            let rect = bottomView?.convert(bottomView!.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
            }
            imageView?.frame.origin.y = Y

        }
    }

}

第二个动画
第一种情况
这里写图片描述

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    let testValue :NSString = "这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n"
    var dic : Dictionary<String,String> = [:]
    override func viewDidLoad() {
        super.viewDidLoad()
       tableView.tableFooterView = UIView()

        // 系统自己计算cell高度
       tableView.estimatedRowHeight = 60
       tableView.rowHeight = UITableViewAutomaticDimension
       tableView.separatorInset = .zero
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let label = cell.contentView.viewWithTag(1000) as! UILabel
        label.text = testValue as String
//        if dic[String(indexPath.row)] == "0"{
//           label.numberOfLines = 0
//        }else{
//            label.numberOfLines = 1
//        }
        return cell

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        let label = cell?.contentView.viewWithTag(1000) as! UILabel
        // tableView.beginUpdates(),tableView.endUpdates()之间的动画一帧一帧执行.替代tableView.reloadData()
        tableView.beginUpdates()
        if label.numberOfLines == 0{
            label.numberOfLines = 1
            dic[String(indexPath.row)] = "1"
        }else{
            label.numberOfLines = 0
             dic[String(indexPath.row)] = "0"
        }
        tableView.endUpdates()
//        tableView.reloadData()
    }

    override func viewDidLayoutSubviews() {
        // 使分割线顶头处理
        self.tableView.separatorInset = .zero
        self.tableView.layoutMargins = .zero
        print("aaaaaaaaa")
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
         // 使分割线顶头处理
        cell.separatorInset = .zero
        cell.layoutMargins = .zero
    }

//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//      return testValue.boundingRect(with: CGSize(width: 300, height: 0), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: [UIFont .systemFont(ofSize: 17)]], context: nil).size.height + 20
//    }



}

第二种情况
这里写图片描述

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    let testValue :NSString = "这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n"
    var dic : Dictionary<String,String> = [:]
    override func viewDidLoad() {
        super.viewDidLoad()
       tableView.tableFooterView = UIView()

        // 系统自己计算cell高度
       tableView.estimatedRowHeight = 60
       tableView.rowHeight = UITableViewAutomaticDimension
       tableView.separatorInset = .zero
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let label = cell.contentView.viewWithTag(1000) as! UILabel
        label.text = testValue as String
        if dic[String(indexPath.row)] == "0"{
           label.numberOfLines = 0
        }else{
            label.numberOfLines = 1
        }
        return cell

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        let label = cell?.contentView.viewWithTag(1000) as! UILabel
        // tableView.beginUpdates(),tableView.endUpdates()之间的动画一帧一帧执行.替代tableView.reloadData()
        tableView.beginUpdates()
        if label.numberOfLines == 0{
            label.numberOfLines = 1
            dic[String(indexPath.row)] = "1"
        }else{
            label.numberOfLines = 0
             dic[String(indexPath.row)] = "0"
        }
        tableView.endUpdates()
//        tableView.reloadData()
    }

    override func viewDidLayoutSubviews() {
        // 使分割线顶头处理
        self.tableView.separatorInset = .zero
        self.tableView.layoutMargins = .zero
        print("aaaaaaaaa")
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
         // 使分割线顶头处理
        cell.separatorInset = .zero
        cell.layoutMargins = .zero
    }

//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//      return testValue.boundingRect(with: CGSize(width: 300, height: 0), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: [UIFont .systemFont(ofSize: 17)]], context: nil).size.height + 20
//    }
}

猜你喜欢

转载自blog.csdn.net/u012581760/article/details/81102099
今日推荐