首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI自定义鼠标指针在iPadOS上

SwiftUI自定义鼠标指针在iPadOS上
EN

Stack Overflow用户
提问于 2021-07-25 17:58:35
回答 1查看 446关注 0票数 1

如何使用SwiftUI + iPadOS实现自定义鼠标指针--视图?

苹果在这段代码中有一个如何做到这一点的例子,但它适用于UIKit:交互作用

代码语言:javascript
复制
class QuiltView: UIView, UIPointerInteractionDelegate {


...
    


func sharedInit() {
    ...

    // Add a pointer interaction for the "editor" area Quilt view.
    self.addInteraction(UIPointerInteraction(delegate: self))
}

// Supply custom regions that correspond to grid lines when "useStraightLineStitch" is enabled.
func pointerInteraction(_ interaction: UIPointerInteraction,
                        regionFor request: UIPointerRegionRequest,
                        defaultRegion: UIPointerRegion) -> UIPointerRegion? {
    if useStraightLineStitch {
        let regionNumber = floor(request.location.y / gridHeight)
        return UIPointerRegion(rect: CGRect(x: 0, y: regionNumber * gridHeight, width: self.bounds.size.width, height: gridHeight))
    } else {
        return defaultRegion
    }
}

// Supply pointer style with custom crosshair shape.
func pointerInteraction(_ interaction: UIPointerInteraction, styleFor region: UIPointerRegion) -> UIPointerStyle? {
    let crosshair = UIPointerShape.path(crosshairBezierPath())

    if useStraightLineStitch {
        return UIPointerStyle(shape: crosshair, constrainedAxes: [.vertical])
    } else {
        return UIPointerStyle(shape: crosshair)
    }
}

func crosshairBezierPath() -> UIBezierPath {
    let crosshairPath = UIBezierPath()
    crosshairPath.move(to: CGPoint(x: -1.5, y: 1.5))
    crosshairPath.addLine(to: CGPoint(x: -1.5, y: 10))
    crosshairPath.addArc(withCenter: CGPoint(x: 0, y: 10),
                         radius: 1.5,
                         startAngle: CGFloat.pi,
                         endAngle: 0,
                         clockwise: false)
    crosshairPath.addLine(to: CGPoint(x: 1.5, y: 1.5))
    crosshairPath.addLine(to: CGPoint(x: 10, y: 1.5))
    crosshairPath.addArc(withCenter: CGPoint(x: 10, y: 0),
                         radius: 1.5,
                         startAngle: CGFloat.pi / 2.0,
                         endAngle: CGFloat.pi * 1.5,
                         clockwise: false)
    crosshairPath.addLine(to: CGPoint(x: 1.5, y: -1.5))
    crosshairPath.addLine(to: CGPoint(x: 1.5, y: -10))
    crosshairPath.addArc(withCenter: CGPoint(x: 0, y: -10),
                         radius: 1.5,
                         startAngle: 0,
                         endAngle: CGFloat.pi,
                         clockwise: false)
    crosshairPath.addLine(to: CGPoint(x: -1.5, y: -1.5))
    crosshairPath.addLine(to: CGPoint(x: -10, y: -1.5))
    crosshairPath.addArc(withCenter: CGPoint(x: -10, y: 0),
                         radius: 1.5,
                         startAngle: CGFloat.pi * 1.5,
                         endAngle: CGFloat.pi / 2.0,
                         clockwise: false)
    crosshairPath.close()
    return crosshairPath
}


...

}

我想要的是实现同样的目标,但使用SwiftUI吗?提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-30 18:10:57

如果你想用苹果目前的API来做这件事,我不相信你能做到。希望在将来的发行版中会出现这样的特性。

但是,您仍然可以通过进行一些内省来启用此特性。

首先,一个警告:虽然普通的UIKit元素在iOS上返回SwiftUI视图,但这并不一定在所有情况下都是正确的。因此,这一解决方案可能会在未来某一时刻失效。

现在,寻找一种可能的方法。

添加一个内省包到您的应用程序。在过去,我使用了SwiftUI-回顾 (向我的视图添加一个UIDropInteraction )。然后将Introspect模块导入源文件。

大多数内省包(如这个包)都包括View扩展,这些扩展允许您检查底层的UIKit组件。取决于您的视图是什么,您将使用特定的修饰符。

例如,如果希望获取特定视图的基础视图控制器,可以使用.introspectViewController()修饰符。您将为它提供一个将被赋予视图控制器的闭包。您可以将交互添加到视图控制器的视图中。(如果有滚动视图,请使用introspectScrollView修饰符等)

示例,假设一个名为PointerDelegate的类符合UIPointerInteractionDelegate

代码语言:javascript
复制
import SwiftUI
import Introspect

struct YourView: View {

  var body: some View {
    // Your view hierarchy
    // For instance
    List { ... }
      .introspectViewController { viewController in 
        viewController.view.addInteraction(UIPointerInteraction(delegate: PointerDelegate()))
      }
  }
}

这是个小问题,正如早些时候警告的那样,未来可能会崩溃。然而,这是一种从您的UIKit应用程序中获得只在SwiftUI中启用的某些特性的访问方式。

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

https://stackoverflow.com/questions/68521245

复制
相关文章

相似问题

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