简体   繁体   English

如何在 swift 中使用鼠标拖动来旋转 NSImage

[英]How can I rotate an NSImage using mouse drag in swift

I have looked all over the web and I haven't been able to find a solution that explains how you can rotate an NSImage using mouse drag on Xcode.我已经查看了 web 的所有内容,但我无法找到一个解决方案来解释如何在 Xcode 上使用鼠标拖动来旋转 NSImage。 Anyone done it before?以前有人做过吗?

You need to tell use what platform you are writing for.您需要告诉使用您正在编写的平台。 The fact that you mention NSImage rather than UIImage suggests you are talking about MacOS.您提到NSImage而不是UIImage的事实表明您在谈论 MacOS。 You should add the appropriate tag to your question.您应该为您的问题添加适当的标签。

I mostly work on iOS these days, so my Mac OS skills are rusty.这些天我主要在 iOS 上工作,所以我的 Mac OS 技能生疏了。

You also need to decide if you're going to use UIKit/AppKit or SwiftUI.您还需要决定是使用 UIKit/AppKit 还是 SwiftUI。 The industry is moving towards SwiftUI, so you might want to use that rather than AppKit (Mac) or UIKit (iOS).该行业正在向 SwiftUI 发展,因此您可能希望使用它而不是 AppKit (Mac) 或 UIKit (iOS)。

You need to break the problem into parts and solve each part.您需要将问题分解为多个部分并解决每个部分。

First, build an interface that turns a drag gesture into an increasing value.首先,构建一个界面,将拖动手势变成一个递增的值。 Do you want the user to drag on your image and have the image rotate as if they are spinning it with the mouse?您是否希望用户在您的图像上拖动并让图像像用鼠标旋转一样旋转? Or do you want to respond to a drag in a straight line like a slider?或者您想响应像 slider 这样的直线拖动?

Assuming you're building a Mac App, look at NSPanGestureRecognizer .假设您正在构建一个 Mac 应用程序,请查看NSPanGestureRecognizer ( UIPanGestureRecognizer is the equivalent on iOS.) UIPanGestureRecognizer在 iOS 上是等效的。)

Once you've got a system for collecting an increasing value from a drag gesture, you will want to turn that into a rotation value and apply that to your image.一旦你有了一个从拖动手势中收集递增值的系统,你就会想把它变成一个旋转值并将它应用到你的图像上。 (actually, to your image view .) You'll want to install your image into an NSImageView, and then set the wantsLayer property on your image view to true . (实际上,到您的图像视图。)您需要将图像安装到 NSImageView 中,然后将图像视图上的wantsLayer属性设置为true (On iOS all views are backed by layers, but you have to ask for a backing layer on Mac OS.) (在 iOS 上,所有视图都由层支持,但您必须在 Mac OS 上要求支持层。)

Then you will apply a rotation to your view's layer's transform.然后,您将对视图图层的变换应用旋转。 That will cause the image to rotate around its anchor.这将导致图像围绕其锚点旋转。

Do not ask me for code.不要问我要代码。 This is not a code writing service, it's a site where people ask for help debugging code that they wrote.这不是一个代码编写服务,它是一个人们请求帮助调试他们编写的代码的站点。

Once you have code written, if you're having problems getting it working, post your code (and a link to a project on Github) and we can help you debug it.编写代码后,如果您在运行时遇到问题,请发布您的代码(以及 Github 上项目的链接),我们可以帮助您调试它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM