首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用UIPreviewParameters将文本范围指定为UIContextMenuInteraction突出显示的预览,而不隐藏视图的其余部分?

如何使用UIPreviewParameters将文本范围指定为UIContextMenuInteraction突出显示的预览,而不隐藏视图的其余部分?
EN

Stack Overflow用户
提问于 2020-01-26 09:05:53
回答 2查看 1.3K关注 0票数 6

iOS 13.4更新(2020年3月):

UIPointerInteraction悬停在链接上时,也会发生这种情况。

我有一个显示富文本的视图,并在用户长时间按下链接时显示iOS 13上下文菜单。当用户开始长按压时,我希望能够突出显示链接而不是整个视图。

为此,我提供了一个UITargetedPreview对象,该对象包含UIPreviewParameters,并将每一行的CGRects突出显示给视图的UIContextMenuInteractionDelegate。这正确地突出了链接,但是也隐藏了视图的其余部分,这带来了不必要的副作用。

这张图片说明了这个问题:

注意,虽然链接被正确地突出显示,但是视图的其余部分在链接被长期按下后释放时会闪烁。

将此与苹果自己的Notes.app中的行为进行比较:

注意,当链接被长期按下时,视图的其余部分不会消失。这在苹果的其他应用程序(如Safari)中也能像预期的那样工作。

我以以下方式向交互委托提供UITargetedPreview

代码语言:javascript
复制
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, previewForHighlightingMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
    guard let range = configuration.identifier as? NSRange else { return nil }        
    let lineRects: [NSValue] = // calculate appropriate rects for the range of text
    let parameters = UIPreviewParameters(textLineRects: lineRects)
    return UITargetedPreview(view: /* the rich text view */, parameters: parameters)
}

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, previewForDismissingMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
    guard let range = configuration.identifier as? NSRange else { return nil }        
    let lineRects: [NSValue] = // calculate appropriate rects for the range of text
    let parameters = UIPreviewParameters(textLineRects: lineRects)
    return UITargetedPreview(view: /* the rich text view */, parameters: parameters)
}

我在UITargetedPreviewUIPreviewParameters的文档中找不到任何东西,所以有人知道如何做到这一点吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-03 08:36:53

好吧,多亏了一个在WebKit中的实现,我终于找到了该怎么做。我做错的不是提供有针对性的预览UIPreviewTarget

若要仅突出显示视图的一部分,需要:

  1. 提供UIPreviewParameters,说明要在预览中显示的视图部分,并将背景颜色设置为与要预览的视图相同的颜色。
  2. 提供建立以下内容的UIPreviewTarget
代码语言:javascript
复制
- the whole view as the container of the animation
- the centre of the portion being shown as the centre of the animation

  1. 要显示的视图部分的创建快照,并将其指定为UITargetedPreview视图。

在代码中,如下所示:

代码语言:javascript
复制
let view: UIView = // the view with the context menu interaction
let highlightedRect: CGRect = // the rect of the highlighted portion to show

// 1        
let previewParameters = UIPreviewParameters()
previewParameters.visiblePath = // path of highlighted portion of view to show, remember you can use UIPreviewParameters.init(textLineRects:) for text
previewParameters.backgroundColor = view.backgroundColor

// 2: Notice that we're passing the whole view in here
let previewTarget = UIPreviewTarget(container: view, center: CGPoint(x: highlightedRect.midX, y: highlightedRect.midY))

// 3: Notice that we're passing the snapshot view in here - not the whole view
let snapshot = view.resizableSnapshotView(from: highlightedRect, afterScreenUpdates: true, withCapInsets: .zero)!
return UITargetedPreview(view: snapshot, parameters: previewParameters, target: previewTarget)
票数 5
EN

Stack Overflow用户

发布于 2020-11-04 12:57:43

你的解决方案非常有用。我使用hitTest来检测高度视图,我对结果很高兴,所以只想分享。

代码语言:javascript
复制
- (UITargetedPreview *)contextMenuInteraction:(UIContextMenuInteraction *)interaction previewForHighlightingMenuWithConfiguration:(UIContextMenuConfiguration *)configuration {    
    UIView *view = interaction.view;
    CGPoint location = [interaction locationInView:view];
    // Detecting the hittable subview at the given location
    UIView *subView = [view hitTest:location withEvent:nil];
    if (subView) {
        CGRect highlightedRect = subView.frame;
        UIPreviewParameters *previewParameters = [UIPreviewParameters new];
        previewParameters.backgroundColor = subView.backgroundColor;
        previewParameters.visiblePath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, highlightedRect.size.width, highlightedRect.size.height)];

        return [[UITargetedPreview alloc] initWithView:subView parameters:previewParameters];
    }
    return nil;
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59916934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档