iOS 获取cell及cell上控件在屏幕上的frame

836 阅读1分钟

cell 在tableview的父view的位置

let rect = tableView.convert(tableView.rectForRow(at: indexPath), to: tableView.superview)

cell 中的小控件在屏幕中的位置

class MyTableViewCell: UITableViewCell {
    
    //省略代码
    ......
    
    @objc func imageClick(tap: UITapGestureRecognizer) {
        guard let image = tap.view else { return }
        //let window = UIApplication.shared.windows.filter{ $0.isKeyWindow }.first
        let rect = image.convert(image.convert(image.frame, from: contentView), to: window)
    }
    
    @objc func buttonClick(_ button: UIButton) {
        //let window = UIApplication.shared.windows.filter{ $0.isKeyWindow }.first
        let rect = button.convert(button.convert(button.frame, from: contentView), to: window)
    }
}