最新消息:服务器现已从linode换到fzhost.net,且主题已从retina 0.2换为大前端D8

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

Swift crifan 34浏览 0评论

折腾:

[已解决]swift支持长按

中,加了长按的代码,但是此处没有执行对应的longPressAction

因为:

此处,属于一个UITableViewCell,结果执行到了:

didSelectRowAtIndexPath

所以要去:

此处的目的是:

尽量使得代码中,可以检测到,是针对哪个自定义的UITableViewCell中的具体哪个字视图:

自定义的UITableViewCell中有好几个子的view

此处比较关心的是:

一个bubbleView:是个UIView,显示的是聊天的text或者发送的file的视图

用户的头像button:显示用户头像

-》且希望:用户头像的单击,是可以支持的,

-》同时支持,长按用户头像和bubbleView都可以检测到

-》以便执行对应的不同的动作。

[折腾过程]

1.

搜:

swift didSelectRowAtIndexPath long press conflict

ios – iphone – didSelectRowAtIndexPath: only being called after long press on custom cell – Stack Overflow

swift didSelectRowAtIndexPath UILongPressGestureRecognizer conflict

ios – didSelectRowAtIndexPath method conflict with Load More function in Parse using Swift 2 – Stack Overflow

添加了:

    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?{
        return nil
    }

后,虽然的确可以使得,不调用didSelectRowAtIndexPath了,但是还是无法执行到:

longpress

搜:

swift uitableviewcell long press

uitableview – Swift – Adding long press gesture recognizer in custom cell – Stack Overflow

swift – Animate Longpress on UITableViewCell with background Color – Stack Overflow

看到UITableViewCell支持gesture,所以去试试

uitableview cannot select

uitableview not selectable

iphone – UITableViewCell not always selectable? – Stack Overflow

对于cell设置:

self.userInteractionEnabled = false

则是不行的

-》会导致UILongPressGestureRecognizer都无法工作了。。。

objective c – Make UITableViewCell not selectable, but make the textfield on it selectable – Stack Overflow

uitableviewcell long press gesture

iphone – Long press on UITableView – Stack Overflow

  uitableviewcell press conflict select

ios – UITableView need long press to select a row – Stack Overflow

swift long press point

Detecting Long-Press Gestures in iOS 7 – iOScreator

swift UILongPressGestureRecognizer get position

ios – Save touch point location coordinates – Stack Overflow

ios – How do I use UILongPressGestureRecognizer with a UICollectionViewCell in Swift? – Stack Overflow

对应代码为:

    var messageTableView:UITableView = UITableView()
    var lpgr:UILongPressGestureRecognizer? = nil
        //add long press support
        lpgr = UILongPressGestureRecognizer(target: self, action: "longPressAction:")
        self.messageTableView.addGestureRecognizer(lpgr!)
    func longPressAction(gestureReconizer: UILongPressGestureRecognizer) {
        print("longPressAction gestureReconizer=\(gestureReconizer)")
       
        if gestureReconizer.state == UIGestureRecognizerState.Ended {
            let curPoint:CGPoint = gestureReconizer.locationInView(self.messageTableView)
            print("curPoint=\(curPoint)") //(145.5, 54.5)
            if let indexPath = self.messageTableView.indexPathForRowAtPoint(curPoint) {
                print("indexPath=\(indexPath)") //<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 – 0}
                let curCell = self.messageTableView.cellForRowAtIndexPath(indexPath)
                print("curCell=\(curCell)")
            }else{
                print("not find cell for current long press point")
            }
        }
    }

输出:

longPressAction gestureReconizer=<UILongPressGestureRecognizer: 0x7ff2cd1ab370; state = Ended; view = <UITableView 0x7ff2cc15d400>; target= <(action=longPressAction:, target=<JianDao.MessageTableViewController 0x7ff2cacf8b80>)>>
curPoint=(295.5, 115.0)
indexPath=<NSIndexPath: 0xc000000000200016> {length = 2, path = 0 – 1}
curCell=Optional(<JianDao.MessageTableViewCell: 0x7ff2cb142000; baseClass = UITableViewCell; frame = (0 66.5; 375 66.5); autoresize = W; layer = <CALayer: 0x7ff2cd1b9d30>>)

证明是可以检测到,长按这个事件的了。

但是很明显,还需要去:

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

转载请注明:在路上 » [已解决]swift中自定义的UITableViewCell中无法获取长按UILongPressGestureRecognizer

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
53 queries in 0.546 seconds, using 12.97MB memory