通过 Longpress 使用 3D touch peek 和 pop
Using 3D touch peek and pop with Longpress
我正在尝试为所有设备(如 Instagram)模拟 3D 触摸查看和弹出 - 所以在长按手势下显示查看和弹出。这可能吗?我找到了 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) {
...
库的问题是 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
,如果出现任何其他问题,请反馈:)
我正在尝试为所有设备(如 Instagram)模拟 3D 触摸查看和弹出 - 所以在长按手势下显示查看和弹出。这可能吗?我找到了 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) {
...
库的问题是 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
,如果出现任何其他问题,请反馈:)