最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中计算当前长按的坐标是属于自定义的UITableViewCell中哪个子视图的

Swift crifan 2349浏览 0评论

折腾:

[已解决]swift中自定义的UITableViewCell中无法获取长按UILongPressGestureRecognizer

现在问题转化为:

一直point坐标,判断属于哪个view内

swift check point within which subview

ios – Check if a subview is in a view using Swift – Stack Overflow

ios – Determining if a point in a view is within a subviews bounds – Stack Overflow

swift check point within frame

xcode – IOS: verify if a point is inside a rect – Stack Overflow

iphone – How to test if a point is in a view – Stack Overflow

ios – How to check if tap happened in a specific place – Stack Overflow

结果是:

    func longPressAction(gestureReconizer: UILongPressGestureRecognizer) {
        print("longPressAction gestureReconizer=\(gestureReconizer)")
       
        print("self.messageTableView.frame=\(self.messageTableView.frame)")
        //(0.0, 0.0, 375.0, 559.0)
       
        if gestureReconizer.state == UIGestureRecognizerState.Ended {
            let curPoint:CGPoint = gestureReconizer.locationInView(self.messageTableView)
            print("curPoint=\(curPoint)") //(306.0, 40.0)
            if let indexPath = self.messageTableView.indexPathForRowAtPoint(curPoint) {
                print("indexPath=\(indexPath)") //<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 – 0}
                if let curCell = self.messageTableView.cellForRowAtIndexPath(indexPath) {
                    print("curCell=\(curCell)") //<JianDao.MessageTableViewCell: 0x7fe30c830800; baseClass = UITableViewCell; frame = (0 0; 375 66.5); autoresize = W; layer = <CALayer: 0x7fe30e14ca80>>
                   
                    let curMessageTVC:MessageTableViewCell = curCell as! MessageTableViewCell
                    print("curMessageTVC.frame=\(curMessageTVC.frame)") //(0.0, 0.0, 375.0, 66.5)
                   
                    print("curMessageTVC.messageBubbleView.frame=\(curMessageTVC.messageBubbleView!.frame)")
                    //(289.0, 34.5, 0.0, 0.0)
                   
                    let convertedPointFromView = curMessageTVC.convertPoint(curPoint, fromView: curMessageTVC)
                    print("convertedPointFromView=\(convertedPointFromView)") //(306.0, 40.0)
                    let convertedPointToView = curMessageTVC.convertPoint(curPoint, toView: curMessageTVC)
                    print("convertedPointToView=\(convertedPointToView)") //(306.0, 40.0)
                   
                    let pointInCurCell:CGPoint = convertedPointToView
                    print("pointInCurCell=\(pointInCurCell)") //(306.0, 40.0)
                    if let isInside = curMessageTVC.messageBubbleView?.pointInside(pointInCurCell, withEvent: nil) {
                        print("long press point=\(pointInCurCell) is within messageBubbleView.frame=\(curMessageTVC.messageBubbleView!.frame)")
                    }
                   
                    if (CGRectContainsPoint((curMessageTVC.messageBubbleView?.frame)!, pointInCurCell)) {
                        print("long press point=\(pointInCurCell) is within messageBubbleView.frame=\(curMessageTVC.messageBubbleView!.frame)")
                    }
                }
               
            }else{
                print("not find cell for current long press point")
            }
        }
    }

pointInside:始终都是true

而CGRectContainsPoint始终都是false-》因为此处的,自己的view:messageBubbleView有点问题:frame的宽和高都是0,所以没发正确判断是否在frame内。。。

-》不过上述代码,如果是point和frame正常的话,应该是可以正常工作的。

-》算了,去想办法解决此处的布局等问题吧:

[记录]尝试使用Cartography去实现自动布局

-》后来,基本解决了自动布局问题后,结果发现,此处的convertpoint是没有效果的

-》所以还是由于自己不会用convertpoint

-》还是需要去搞懂convertpoint的用法:

-》

搜:

swift convertpoint example

ios – Using convertPoint to get the relative position inside a parent UIView – Stack Overflow

最后用如下代码:

    func longPressAction(gestureReconizer: UILongPressGestureRecognizer) {
        print("longPressAction gestureReconizer=\(gestureReconizer)")
       
        print("self.fileTableView.frame=\(self.fileTableView.frame)")
        //(0.0, 0.0, 375.0, 554.0)
       
        if gestureReconizer.state == UIGestureRecognizerState.Ended {
            let curPoint:CGPoint = gestureReconizer.locationInView(self.fileTableView)
            print("curPoint=\(curPoint)") //(69.0, 230.5)
            if let indexPath = self.fileTableView.indexPathForRowAtPoint(curPoint) {
                print("indexPath=\(indexPath)") //<NSIndexPath: 0xc000000000400016> {length = 2, path = 0 – 2}
                if let curCell = self.fileTableView.cellForRowAtIndexPath(indexPath) {
                    print("curCell=\(curCell)") //<JianDao.FileTableViewCell: 0x7faaa1045600; baseClass = UITableViewCell; frame = (0 220; 375 96); text = ‘李茂原型.xlsx’; autoresize = W; layer = <CALayer: 0x7faaa324ec20>>
                   
                    let curTVC:FileTableViewCell = curCell as! FileTableViewCell
                    print("curTVC.frame=\(curTVC.frame)") //(0.0, 220.0, 375.0, 96.0)
                   
                    print("curTVC.senderLabel.frame=\(curTVC.senderLabel.frame)")
                    //(15.0, 0.0, 70.0, 17.0)
                   
//                    let convertedPointFromView = curTVC.convertPoint(curPoint, fromView: curTVC)
//                    print("convertedPointFromView=\(convertedPointFromView)") //(69.0, 230.5)
//                    let convertedPointToView = curTVC.convertPoint(curPoint, toView: curTVC)
//                    print("convertedPointToView=\(convertedPointToView)") //(69.0, 230.5)
//                    let convertedPointFromView = curTVC.convertPoint(curPoint, fromView: curCell)
//                    print("convertedPointFromView=\(convertedPointFromView)") //(73.0, 231.5)
//                    let convertedPointToView = curTVC.convertPoint(curPoint, toView: curCell)
//                    print("convertedPointToView=\(convertedPointToView)") //(73.0, 231.5)
                    let convertedPointFromView = curTVC.convertPoint(curPoint, fromView: self.fileTableView)
                    print("convertedPointFromView=\(convertedPointFromView)") //(73.0, 12.0)
                    let convertedPointToView = self.fileTableView.convertPoint(curPoint, toView: curTVC)
                    print("convertedPointToView=\(convertedPointToView)") //(73.0, 12.0)
                   
                    let pointInCurCell:CGPoint = convertedPointFromView
                    print("pointInCurCell=\(pointInCurCell)") //(73.0, 12.0)
                   
                    if curTVC.senderLabel.pointInside(pointInCurCell, withEvent: nil) {
                        print("pointInside: long press point=\(pointInCurCell) is within frame=\(curTVC.senderLabel.frame)")
                    }
                   
                    if (CGRectContainsPoint(curTVC.senderLabel.frame, pointInCurCell)) {
                        print("CGRectContainsPoint: long press point=\(pointInCurCell) is within frame=\(curTVC.senderLabel.frame)")
                    }
                }
               
            }else{
                print("not find cell for current long press point")
            }
        }
    }

终于可以正常工作了。

其中核心代码是:

                    let convertedPointFromView = curTVC.convertPoint(curPoint, fromView: self.fileTableView)
                    print("convertedPointFromView=\(convertedPointFromView)") //(73.0, 12.0)
                    let convertedPointToView = self.fileTableView.convertPoint(curPoint, toView: curTVC)
                    print("convertedPointToView=\(convertedPointToView)") //(73.0, 12.0)

关于convertPoint的用法,总结一下就是:

convertedPointInSubview = subView.convertPoint(pointInParentView, fromView: parentView)

等价于:

convertedPointInSubview = parentView.convertPoint(pointInParentView, toView: subView)

都是:

把,一个在 父视图 中的某个点的坐标值,转换为 (父视图中的)某个字视图 中的坐标

以便于下来,去判断转换后的点的坐标,是否处于该字视图中的某个区域

因为,上面那部分的代码的含义就是:

获取了当前,长按屏幕的点的坐标curPoint,是属于父视图fileTableView中的

(此处用fileTableView.indexPathForRowAtPoint(curPoint)得知处于哪个UITableViewCell==curTVC中,之后)

再去用:

curTVC.convertPoint(curPoint, fromView: self.fileTableView)

或:

curTVC.convertPoint(curPoint, toView: self.fileTableView)

将父视图中的点的坐标值,转换为当前cell中的坐标值pointInCurCell

然后再去用:

curTVC.senderLabel.pointInside(pointInCurCell, withEvent: nil)

或:

CGRectContainsPoint(curTVC.senderLabel.frame, pointInCurCell)

去判断,当前cell中的这个点,是否是属于 senderLabel这个发送这UILabel中内。

转载请注明:在路上 » [已解决]swift中计算当前长按的坐标是属于自定义的UITableViewCell中哪个子视图的

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.201 seconds, using 22.24MB memory