ios - 如何在按住表格 View 单元格时逐渐增加它的大小

标签 ios uitableview uigesturerecognizer swift4

我想做什么(逐字): 在指定的时间内保持一个 tableview 单元格。一旦达到该时间段,单元格高度就会逐渐增加。当我松开手指时,单元格高度停止增长。

我有什么: 我有几个 tableViewCells。使用以下命令在单元格上按下指定的时间后:

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("longPress:"))
longPressRecognizer.minimumPressDuration = 1.0s
longPressRecognizer.delegate = self
self.view.addGestureRecognizer(longPressRecognizer)

我想增加单元格高度。但是我不能在不知道触摸位于哪一行的情况下这样做,所以我到了这里:

func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
    if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
        let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {
            //let cell = tableView.cellForRow(at: indexPath)
        }
    }
}

我考虑过控制 rowHeight,但那只是在 tableView 函数中,所以我不知道如何调用它。

我不确定如何进行。而且我不寻找任何与 .beginUpdates 和 .endUpdates 有关的东西,因为我希望细胞生长是渐进的,最好是动画的。

任何帮助将不胜感激,因为我已经为这个特定问题寻找答案很长时间了。

包含 rowHeight 声明的代码:

     override func viewDidLoad() {
            super.viewDidLoad()

    //        let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("longPress:"))
    //        longPressRecognizer.minimumPressDuration = 1.0 // 1 second press
    //        longPressRecognizer.delegate = self
    //        self.view.addGestureRecognizer(longPressRecognizer)

            tableView.delegate = self
            tableView.dataSource = self
            tableView.allowsSelection = false

            self.tableView.reorder.delegate = self

            view.backgroundColor = UIColor(red:0.64, green:0.93, blue:0.78, alpha:1.0)
            tableView.backgroundColor = UIColor.clear

            tableView.rowHeight = 84
            tableView.rowHeight = UITableViewAutomaticDimension


            // Do any additional setup after loading the view.
        }

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

最佳答案

Declare property for height of the cell and return this property in the tableviews's delegate method heightForRowAtIndexPath method

var heightForCell = 100

func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
    let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
    if let indexPath = tableView.indexPathForRow(at: touchPoint) {

          self. heightForCell = // height you want to set 
          // now reload cell again
    }
} 
}

关于ios - 如何在按住表格 View 单元格时逐渐增加它的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48784134/

相关文章:

ios - UIGestureRecognizer.State 是 'possible' 而不是 'recognized' 每隔一次点击 MKMapView

ios - 当 LeftMenuView 的宽度完全显示时,如何禁用 ECSlidingViewController pangesture?

objective-c - 如何追踪内存峰值? (这是带有 p 的峰,而不是 l。)

ios - UITableView 不加载数据

Swift:在 JSON 数据的 TableView 返回中同步数据的呈现

ios - 动态可扩展表格 View

ios - 同时独立的手势

ios - 快速将图像上传到 asp.net web 服务

ios - iOS:如何从Dropbox同步上的mp3文件中获取ID3标签

iOS:(Swift)如何显示距当前位置的距离并在注释副标题上存在注释