Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-UITableViewCell中的UIgestureRecognitor,类似于Snapchat_Ios_Uitableview_Uipangesturerecognizer_Tableviewcell - Fatal编程技术网

iOS-UITableViewCell中的UIgestureRecognitor,类似于Snapchat

iOS-UITableViewCell中的UIgestureRecognitor,类似于Snapchat,ios,uitableview,uipangesturerecognizer,tableviewcell,Ios,Uitableview,Uipangesturerecognizer,Tableviewcell,我正在尝试在我的实践应用程序中实现与Snapchat非常类似的功能,在Snapchat中,您将UITableViewCell拖动到右侧或左侧,拖动时,视图后面的图像会慢慢出现,直到到达某个点,然后,它启动一个segue或PageViewController键入segue到另一个视图控制器,您可以使用它与朋友聊天 起初,我尝试使用屏幕边缘平移手势识别器,但只有当您从屏幕边缘开始滑动时,它才起作用。我需要能够从UITableViewCell中的任何位置刷卡 我所需功能的一个演示是在Snapchat中

我正在尝试在我的实践应用程序中实现与Snapchat非常类似的功能,在Snapchat中,您将
UITableViewCell
拖动到右侧或左侧,拖动时,视图后面的图像会慢慢出现,直到到达某个点,然后,它启动一个segue或
PageViewController
键入segue到另一个视图控制器,您可以使用它与朋友聊天

起初,我尝试使用屏幕边缘平移手势识别器,但只有当您从屏幕边缘开始滑动时,它才起作用。我需要能够从
UITableViewCell
中的任何位置刷卡

我所需功能的一个演示是在Snapchat中,当你慢慢地向右滑动朋友的一个桌面单元格时,你可以更清楚地看到它,它会慢慢地显示消息图标的图像,并最终引导到另一个可以聊天的视图

那么,平移手势识别器就足够了吗?如果是这样,那么最好的方法是什么?我已经看过关于平移手势识别器的教程,但我不知道在滑动一定距离后,它如何最终导致另一个视图控制器。我想我可以将消息图标放在显示的表格单元格内容后面,在向右滑动时可能会出现这些内容,但是我如何实现这种平滑的功能呢

了解这一点将真正提高我在流畅用户体验方面的体验。如有任何建议或方法,将不胜感激,谢谢


编辑:请在目标C中留下答案。我不知道Swift。

您可以为此创建自定义TableViewCell。下面的代码应该满足您的要求。只需将代理添加到您的
ViewController

protocol TableViewCellDelegate {
    func something()
}

class TableViewCell: UITableViewCell {

    var startSegue = false
    var label: UILabel                      // Or UIView, whatever you want to show
    var delegate: TableViewCellDelegate?    // Delegate to your ViewController to perform the segue

    required init(coder aDecoder: NSCoder) {
        fatalError("NSCoding not supported")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        // Utility method for creating the label / view
        func createLabel() -> UILabel {
            let label = UILabel(frame: CGRect.nullRect)

            // Add label customization here

            return label
        }

        // Create "check" & "cross" labels for context cues
        label = createLabel()        // Create the label
        label.text = "\u{2713}"      // Add check symbol as text

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        addSubview(label)

        // Add a pan recognizer
        var recognizer = UIPanGestureRecognizer(target: self, action: "handleSwipe:")
        recognizer.delegate = self
        addGestureRecognizer(recognizer)
    }

    let cueMargin: CGFloat = 10.0, cueWidth: CGFloat = 50.0

    override func layoutSubviews() {

        // The label starts out of view
        label.frame = CGRect(x: bounds.size.width + cueMargin, y: 0, width: cueWidth, height: bounds.size.height)
    }

    func handleSwipe(recognizer: UIPanGestureRecognizer) {
        let translation = recognizer.translationInView(self)

        if recognizer.state == .Changed {
            center = CGPointMake(center.x + translation.x, center.y)
            startSegue = frame.origin.x < -frame.size.width / 2.0                       // If swiped over 50%, return true
            label.textColor = startSegue ? UIColor.redColor() : UIColor.whiteColor()    // If swiped over 50%, become red
            recognizer.setTranslation(CGPointZero, inView: self)
        }
        if recognizer.state == .Ended {
            let originalFrame = CGRect(x: 0, y: frame.origin.y, width: bounds.size.width, height: bounds.size.height)
            if delegate != nil {
                startSegue ? delegate!.something() : UIView.animateWithDuration(0.2) { self.frame = originalFrame }
            }
        }
    }

    // Need this to handle the conflict between vertical swipes of tableviewgesture and pangesture
    override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
            let velocity = panGestureRecognizer.velocityInView(superview!)
            if fabs(velocity.x) >= fabs(velocity.y) { return true }
            return false
        }
        return false
    }
}
您的
cellforrowatinexpath
TableViewDataSource
如下所示:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as TableViewCell

        cell.delegate = self

        // Add stuff for your other labels like:
        // "cell.name = item.name" etc        

       return cell
    }

如果要将数据传回
视图控制器
,只需使用代理即可。

谢谢,但我不知道Swift。有没有办法在Objective-C中实现这一点?如果没有,我将尝试阅读此代码,并通过将其转换为Objective C来进行尝试。我会给您回复。再次感谢。此外,如果您希望它平滑显示,您可以使用UIView(或标签)的alpha值,并根据滑动使其从0变为1。对不起,我对Objective-C了解不多。我所知道的是,它使用括号
[]
作为方法,而不是Swift中的点符号。我让平移手势稍微起作用了……但这还不是一个成功的方法。有没有办法制作一个Swift文件,将此代码放在其中,并使其与我的Objective-C文件的其余部分共存?我将此问题标记为正确,因为它确实给了我想要的东西。但是我不能使它与我的目标C代码兼容。非常感谢您,很抱歉花了这么长时间才将其标记为正确。np,与您的obj-c代码混合有什么问题?你也可以使用自动布局来做这件事,也许那会更容易?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as TableViewCell

        cell.delegate = self

        // Add stuff for your other labels like:
        // "cell.name = item.name" etc        

       return cell
    }