【问题标题】:Using 3D touch peek and pop with Longpress通过 Longpress 使用 3D touch peek and pop
【发布时间】:2016-03-03 09:50:28
【问题描述】:

我正在尝试为所有设备(如 Instagram)模拟 3D 触控 peek and pop - 所以在长按手势时显示 peek and pop。这可能吗?我找到了PeekView,但到目前为止,我发现无法将它与我的 Objective C 应用程序(它在 Swift 中)结合起来。

目前我已经复制了 PeekView 项目 - 我有一个包含几个单元格的集合视图,每个单元格中都有一个图像。

 @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];
}

现在我正在尝试实现 handleLongPressGestures 方法,但由于下面显示的错误,我无法使用 PeekView 方法。

编辑:

因此,当尝试使用示例中所示的viewForController 方法时,如果我添加@objc,它会抱怨选项部分。有解决办法吗?

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) {
            ...

【问题讨论】:

  • 是什么让它“不可能”?你是通过 cocoapods 添加的吗?您的实际错误/问题是什么?
  • @Losiowaty 这可能会有所帮助......我已经编辑了文件。我手动添加了文件来尝试一下 - 我有一个桥接头,我已经使用 #import "Projname-Swift.h" 导入了它。
  • 好吧,我尝试通过 pod 添加它,老实说,它看起来并没有更好。此外,发布您的代码可能对其他人更有用,而不是 PeekView 代码;)
  • @Losiowaty 哈哈!嗯,我的在 mo 很简单,但我已经在上面添加了。

标签: objective-c swift 3dtouch


【解决方案1】:

该库的问题是 Swift struct 无法桥接到 Objective-C,因此您无法创建 PeekViewAction 数组。我已经用 Obj-C 支持更新了代码,所以现在您可以使用另一种静态方法来显示 PeekView。用法如下:

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];

确保您下载了最新版本的库。感谢您使用PeekView,如果有任何其他问题出现,请报告:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多