0

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) {
            ...
huong
  • 4,534
  • 6
  • 33
  • 54
vanilla_splsh
  • 153
  • 1
  • 12
  • What makes it "impossible"? Did you add it via cocoapods? What is your actual error/problem? – Losiowaty Mar 03 '16 at 10:50
  • @Losiowaty that would probably help...I've edited the file. I added the file manually to try it - I have a bridging header and I've imported it using `#import "Projname-Swift.h"`. – vanilla_splsh Mar 03 '16 at 11:07
  • Well, I tried adding it via pods and to be honest it doesb't look better. Also, posting your code could prove more usefull for others, rather then the PeekView code ;) – Losiowaty Mar 03 '16 at 11:12
  • @Losiowaty hah! Well mine is very simple at the mo but I've added it above. – vanilla_splsh Mar 03 '16 at 12:09

1 Answers1

1

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 :)

huong
  • 4,534
  • 6
  • 33
  • 54