Search code examples
objective-cswift3dtouch

Using 3D touch peek and pop with Longpress


I'm trying to emulate the 3D touch peek and pop for all devices (like Instagram) - so show the peek and pop at a longpress gesture. Is this possible? I found PeekView but so far I'm finding it impossible to incorporate this with my Objective C app (it's in Swift).

Currently I have replicated the PeekView Project - I have a collection view with a few cells and each cell has an image in it.

 @property (nonatomic, strong) UILongPressGestureRecognizer *longPress;


- (PhotoCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPhotoCell forIndexPath:indexPath];
cell.image.image = [UIImage imageNamed:@"image1"];

return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(nonnull UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath {

self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
self.longPress.minimumPressDuration = 1.0f;
self.longPress.allowableMovement = 100.0f;

[cell addGestureRecognizer:self.longPress];
}

Now I'm trying to implement the handleLongPressGestures method but I can't use the PeekView methods due to the error shown below.

edit:

So when trying to use the viewForController method as shown in the example, if I add @objc, it complains about the Options part. Is there a workaround?

public enum PeekViewActionStyle : Int {
    case Default
    case Selected
    case Destructive
}

public struct PeekViewAction {
    var title: String
    var style: PeekViewActionStyle
}

public class PeekView: UIView {

    var shouldToggleHidingStatusBar = false
    var contentView: UIView?
    var buttonHolderView: UIView?
    var completionHandler: (Int -> Void)?

    var arrowImageView: UIImageView!

    public class func viewForController(
        parentViewController parentController: UIViewController,
        contentViewController contentController: UIViewController,
        expectedContentViewFrame frame: CGRect,
        fromGesture gesture: UILongPressGestureRecognizer,
        shouldHideStatusBar flag: Bool,
        withOptions menuOptions: [PeekViewAction]?=nil,
        completionHandler handler: (Int -> Void)?=nil) {
            ...

Solution

  • The problem with the library is that Swift struct cannot be bridged to Objective-C, so you cannot create the PeekViewAction array. I have updated the code with Obj-C support so now you can use another static method to show the PeekView. The usage is as below:

    NSArray *options = @[@{@"Option 1": @(PeekViewActionStyleDefault)},
                         @{@"Option 2": @(PeekViewActionStyleDestructive)}];
    
    UIViewController *contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"previewVC"];
    
    [PeekView viewForControllerWithParentViewController:self
                                  contentViewController:contentViewController
                               expectedContentViewFrame:CGRectMake(0, 0, 280, 400)
                                            fromGesture:gesture
                                    shouldHideStatusBar:YES
                                            withOptions:options
                                      completionHandler:nil];
    

    Make sure you download the latest version of the library. Thanks for using PeekView and please report back if there's any other issue coming up :)