Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D Touch and UITableViewCell

I added some test 3D Touch functionality to UITableViewController on iPhone 6s. In general it works fine, but I was able to observe some 2 problems, and I am not sure what is happening and how to fix that in a normal way.

1) My cell is selectable, so user can press on it or use "3D Touch". The problem is that when I use "Pop" and "Peek" for UITableViewCell it becomes selected and I can not deselect in a normal way using setSelected: or setHighlighted: methods. I tried to deselect it in different places even in previewingContext:commitViewController in presentViewController completion. They just do nothing and cells still stay in selected state. I retrieved selected cell by calling reviewingContext.sourceView and other temp code which gave me selected cell, but these methods didn't work.

  • So the question is there a normal way to deselect (or better no to select) the cell when user applied "Pop" press on it?

2) I also noticed that sometimes when I cancel "Pop" gesture and bring cell to initial state (when previewingContext:viewControllerForLocation method wasn't even called) my UI just hangs and doesn't respond to touches at all. I need to kill to make it work. It seems very strange, I checked this Tutorial. It works great, without mentioned issues, but they register delegate not on the cell, but on UITableView so "Pop" gesture highlight the whole tableView, but not the cell.

  • Any thoughts and what can be wrong here?

Here is how I implemented 3D touch in my test UITableViewController which conforms to UIViewControllerPreviewingDelegate

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

    // Some cell configuration

    if #available(iOS 9.0, *) {
        if traitCollection.forceTouchCapability == .Available {
            self.registerForPreviewingWithDelegate(self, sourceView: cell)
        }
    } else { // Fallback on earlier versions}

    return cell;
}


// MARK: - UIViewControllerPreviewingDelegate

    func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?
    {
        // Just Test View Controller
        return UIViewController(nibName: nil, bundle: nil)
    }

    func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController)
    {
        self.presentViewController(viewControllerToCommit, animated: true, completion: nil)
    }

Thanks in advance!

like image 593
B.S. Avatar asked Dec 08 '15 18:12

B.S.


2 Answers

  • Easy way to add 3D Touch in tableview cell. without one line of code

storyboard image

  • More details : https://github.com/yudiz-solutions/YM3DTouch
like image 184
Yogesh Makwana Avatar answered Nov 06 '22 07:11

Yogesh Makwana


I noticed same issue. I think the issue is caused by duplicated registration. I added the flag then the issue is fixed.

Please try this(I'm using Objective-C, so please rewrite in swift).

Implement category that

@interface UITableViewCell (FourceTouchRegistered)

@property (nonatomic, assign) BOOL fourceTouchRegistered;

@end

and

#import "UITableViewCell+FourceTouchRegistered.h"
#import <objc/runtime.h>

@implementation UITableViewCell (FourceTouchRegistered)

- (void)setFourceTouchRegistered:(BOOL)fourceTouchRegistered
{
    objc_setAssociatedObject(self, @"fourceTouchRegistered", @(fourceTouchRegistered), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)fourceTouchRegistered
{
    return [objc_getAssociatedObject(self, @"fourceTouchRegistered") boolValue];
}

@end

then

    if(cell.fourceTouchRegistered == NO) {
        cell.fourceTouchRegistered = YES;
        if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
            [self registerForPreviewingWithDelegate:self sourceView:cell];
        }
    }
like image 1
Akira Matsuda Avatar answered Nov 06 '22 07:11

Akira Matsuda