旋转编码器旋钮程序

SwiftUI简直就是小菜一碟 (It’s A Piece Of Cake With SwiftUI)

Building something as seemingly simple as a knob that you can rotate isn’t a simple endeavor using UIKit. Thankfully, SwiftUI makes previously complex UI operations a breeze.

使用UIKit来构建看起来像旋钮一样简单的东西并不是一件容易的事。 幸运的是,SwiftUI使以前复杂的UI操作变得轻而易举。

Drawing a circle in Swift using built-in APIs is a fun exercise — but only because I’m a glutton for punishment.

使用内置的API在Swift中画一个圆是一个有趣的练习-但这只是因为我是惩罚的嘴。

Disclaimer: I don’t understand much about Geometry, so it makes it that much more difficult.

免责声明:我对Geometry的了解不多,因此难度更大。

幸运的是,SwiftUI知道什么是“ Circle()” (Thankfully, SwiftUI knows what a “Circle()” is)

Want to make a circle in SwiftUI? Cool, just type Circle() anywhere you can render a view.

想在SwiftUI上绕圈吗? 很酷,只要在可以渲染视图的任何地方键入Circle()即可。

If you do this inside of your view’s body, you’ll get a perfect (but unstyled, full-width) circle when you press resume on your canvas view.

如果在视图的主体内部执行此操作,则在画布视图上按“恢复”时,将得到一个完美的(但未设置样式的全角)圆圈。

That was easy
那很简单
It ain’t pretty, but it’s a circle
不好看,不过是一个圆圈

现在让我们对其进行样式设置 (Now let’s style it)

Again, SwiftUI makes this super simple. Let’s make a 60x60 circle and make it a silver color.

同样,SwiftUI使这个超级简单。 让我们制作一个60x60的圆圈并将其设为银色。

We change the circle’s color using .fill and give it a frame with .frame.

我们使用.fill更改圆的颜色,并使用.fill为其指定.frame

Simply set the desired attributes using dot notation
只需使用点符号设置所需的属性
There’s our knob, but it’s missing an indicator
有我们的旋钮,但缺少指示器

All we need now is an indicator. I’m going to use a circle, but you can experiment with a line or other shapes if you’d like. I’m going to size it proportionally to the knob, and give it an offset proportional to the knob’s width to place it on the outside of the knob.

我们现在需要的只是一个指标。 我将使用圆,但是如果您愿意,您可以尝试使用直线或其他形状。 我将按比例调整它的大小,并给它一个与旋钮的宽度成比例的偏移量,以将其放置在旋钮的外部。

Here’s how we make it:

这是我们的方法:

好的,让我们将这个圆圈放在另一个圆圈的上方。 应该很容易吧? (Ok, let’s put this circle on top of the other circle. Should be easy, right?)

Well, it is. But you have to know how…

好吧,是的。 但是你必须知道如何……

If you tried to render this circle inside of the other by placing it on the line after .frame in the knob’s outer circle, you’re probably seeing an error. I haven’t delved very deeply into SwiftUI, but from what I can gather, the body can only essentially render one view. You can stack views, but attempting to render more than one outside of stack views has never worked for me. Additionally, we haven’t given any positional information, aside from an X offset.

如果您试图通过将其放置在旋钮外圆.frame之后的行上来将此圆渲染为另一个圆,则可能会看到错误。 我还没有对SwiftUI进行过深入的研究,但是据我所知,主体实际上只能渲染一个视图。 您可以堆叠视图,但是尝试在堆叠视图之外渲染多个视图对我来说从来没有用。 此外,除了X偏移量之外,我们没有提供任何位置信息。

堆栈视图,堆栈视图,无处不在的堆栈视图 (Stack Views, Stack Views, Everywhere A Stack View)

With SwiftUI, we get a horizontal stack with HStack, a vertical stack with VStack, and a 3D stack with ZStack. So what we need here in order to render one view on top of the other is a ZStack. If we place these views as-is in a ZStack, it’ll render just fine. But this code is already starting to get mucked up with a bunch of magic numbers, so I’m going to replace those with properties.

随着SwiftUI,我们得到一个水平叠层HStack ,垂直堆栈VStack ,并配有3D堆栈ZStack 。 因此,为了在一个视图之上呈现一个视图,我们需要一个ZStack 。 如果我们将这些视图原样放置在ZStack中,它将很好地呈现。 但是这段代码已经开始被大量的魔术数字所困扰,因此我将用属性替换那些数字。

If you want a label at the bottom of your volume control, now all you have to do is put the ZStack in a VStack and add Text("Volume") after the ZStack’s closing brace. This renders the label underneath the 2 views we just created.

如果你想在你的音量控制底部的标签,现在你需要做的就是把ZStackVStack并添加Text("Volume")ZStack的右括号。 这将在我们刚刚创建的2个视图下绘制标签。

Adding a gesture recognizer to the knob that animates the position of the indicator is all that we need now. This will give the appearance that the entire volume knob is spinning.

现在,我们只需要在旋钮上添加一个手势识别器以动画化指示器的位置即可。 这将使整个音量旋钮旋转。

We can add a gesture recognizer using .gesture and a rotation animation using .rotationEffect. We’ll also need a way of managing the state of one view using another view’s value (rotation). Combining all of this is easy… but takes a little SwiftUI know-how.

我们可以通过添加一个手势识别.gesture并使用旋转动画.rotationEffect 。 我们还需要一种使用另一个视图的值(旋转)来管理一个视图的状态的方法。 将所有这些结合起来很容易……但是需要一些SwiftUI知识。

First, let’s handle managing state…

首先,让我们处理状态管理...

@州 (@State)

@State is a property modifier that allows us to manage the state of our UI when the value changes. Let’s add another property to the top of our struct that we’ll use to track when the view is being rotated.

@State是一个属性修饰符,允许我们在值更改时管理UI的状态。 让我们在结构的顶部添加另一个属性,该属性将用于跟踪视图何时旋转。

@State var rotation: Double = 0

@State var rotation: Double = 0

手势识别器 (Gesture Recognizer)

Now, let’s add a gesture recognizer for a rotation gesture. Place this at the end of your volume knob. This will change our state variable every time the knob is rotated.

现在,让我们为旋转手势添加一个手势识别器。 将其放在音量旋钮的末端。 每次旋转旋钮,这都会更改我们的状态变量。

.gesture(    RotationGesture()        .onChanged { value in            self.rotation = value.degrees        })

Lastly, let’s add our animation to the end of the indicator.

最后,让我们将动画添加到指示器的末尾。

.rotationEffect(    .degrees(self.rotation),    anchor: .center)

Now our Volume Control should be fully designed and functional. If you press play for a live preview, then hold down option and swipe, you should see the knob rotate.

现在,我们的音量控制应该经过充分设计和功能。 如果按播放进行实时预览,然后按住选项并滑动,则应该看到旋钮旋转。

Rotate it in the live preview, in the simulator, or on a device
在实时预览,模拟器或设备上旋转

That said, I don’t like how the body is looking, so I’m going to clean things up by making the knob and grabber (indicator) views properties of the struct. Inside of the body, I’ll place the views, and do the implementation for the recognizer and the animation.

就是说,我不喜欢身体的外观,所以我将通过使旋钮和抓取器(指示器)查看结构的属性来清理事物。 在主体内部,将放置视图,并执行识别器和动画的实现。

Note:I’m not sure why, but when making the views a property, they seem to become static or class members rather than instance members of the class. Thus, the properties involved in creating them need to be static or class members as well.

注意: 我不确定为什么,但是在将视图设为属性时,它们似乎变为静态或类成员,而不是类的实例成员。 因此,创建它们所涉及的属性也必须是静态的或类成员。

That looks much cleaner to me
对我来说看起来更干净

If you want to see how I implemented the rest of the views, combined them, and also implemented a list view containing songs, check out this repository.

如果您想了解我如何实现其余视图,将它们组合在一起并实现包含歌曲的列表视图,请查看此存储库 。

结语 (Wrap-Up)

We implemented a Volume Control knob that responds to a rotation gesture and gives the appearance that we’re rotating the knob by animating the indicator view.

我们实现了“音量控制”旋钮,该旋钮响应旋转手势,并通过为指示器视图设置动画来显示旋转旋钮的外观。

We did this by making each view, placing them in a stack view, implementing a rotation gesture, adding a state property to track the rotation of the knob, and finally animating the indicator view based on the knob’s rotation value.

为此,我们制作了每个视图,将它们放置在堆栈视图中,实现了旋转手势,添加了state属性以跟踪旋钮的旋转,最后根据旋钮的旋转值对指示器视图进行了动画处理。

The included repository shows you how to create several views in different files so you aren’t cluttering any one file, and storing complex views as properties so your implementation site remains as clean as possible.

包含的存储库向您展示了如何在不同的文件中创建多个视图,以使您不会混乱任何一个文件,并将复杂的视图存储为属性,从而使实现站点保持尽可能的整洁。

See if you can add on to the functionality of this app. It’s got a half-decent UI, but doesn’t really do anything right now. Maybe you can figure out how to play an actual song and link up the duration to the ProgressView?

查看您是否可以添加此应用程序的功能。 它有一个不错的用户界面,但现在并没有真正做任何事情。 也许您可以弄清楚如何播放实际的歌曲并将持续时间链接到ProgressView?

翻译自: https://medium.com/swlh/lets-build-a-retroaudio-knob-with-rotation-gesture-using-swiftui-540f90b587b4

旋转编码器旋钮程序


http://www.taodudu.cc/news/show-2778122.html

相关文章:

  • 如何为您的Android手机创建自定义铃声
  • css语义化命名_为什么我只在生产中使用语义命名
  • 文本编辑快捷键_42种以上几乎可以在任何地方工作的文本编辑键盘快捷键
  • 带你走进T-Pot多蜜罐平台革命:简述、安装、使用、优化、更新
  • hulu dpp_什么是直播电视的Hulu,它可以代替您的有线电视订阅吗?
  • 知乎周源微信_每周源代码7
  • TPOT网络蜜罐安装——保姆级教程(一个人的血泪史)
  • 【HTML】【休闲益智】真相?真香?只有一个!看看谁是大馋虫 or 贪吃鬼(找出真正吃了月饼的人
  • Oauth2 存储 token 到Redis 报错:READONLY You can‘t write against a read only slave
  • gradle project sync failed.please fix your project and try again-Android Studio3.1.2运行出错
  • 调用系统命令,goto again
  • EAI_AGAIN
  • 基于Linux实现的聊天室小程序
  • 关于安装PotPlayer64出现PotPlayer 64 bit need right Try agin的问题
  • git push时缺少Change-Id报错
  • RedisDesktopManager2022(resp-2022.0)安装失败的解决
  • pod构建的ios版本引发 react-native-safe-area-context RN项目的pod-install构建异常
  • Error:A JNI error has occurred, please check your installation and try again的解决方法
  • 使用c语言实现三子棋游戏
  • java小项目---------银行新用户现金业务办理(运用数据库)
  • Hyperledger Fabric 2.1 BYFN 测试用例
  • 华清远见嵌入式培训_第六周回顾与反思
  • RepVGG: Making VGG-style ConvNets Great Again
  • git命令提交后push失败,缺少changeID的解决办法
  • Gradle project sync failed. Please fix your project and try again
  • idea svn update 时不弹出选择分支的对话框,don't show this dialog in the furture解决方案
  • The stash entry is kept in case you need it again.
  • git报 “The stash entry is kept in case you need it again“ 错误解析
  • 记一次拉去代码失败的解决过程Permission denied, please try again. git@code.odrcloud.cn: Permission denied
  • idea 集成Git 遇到的问题 与解决

旋转编码器旋钮程序_让我们使用SwiftUI构建具有旋转手势的复古音频旋钮相关推荐

  1. 旋钮编码器c代码_非常稳定的旋转编码器解码程序(C51源代码)

    在网上下载过很多种编码器解码程序,使用后感觉都不够稳定,特别是旋转速度稍快时,经常会出现错误解码.为此,经过分析编码器输出波形特点,结合其它解码程序的优点,编写如下代码,不用中断,也可以不用定时器.经 ...

  2. 旋钮编码器c代码_求旋转编码器c程序

    编码器涵盖很多种类,不知你具体要求的哪种?我也曾折腾过一段时间EC11型的,给你个从网上找来的,参考吧(在我板上能运行) //通过编码开关(旋转编码器)控制数码管的加减一 #include #defi ...

  3. meteor构建app程序_在Meteor.js中构建Slack克隆:实时数据

    meteor构建app程序 This is the second of a five-part series on building a Slack clone using Meteor. The a ...

  4. meteor构建app程序_在Meteor.js中构建Slack克隆(第5部分):流星部署

    meteor构建app程序 This is the fifth, and last, of a five-part series on building a Slack clone using Met ...

  5. react.js做小程序_如何使用React.js构建现代的聊天应用程序

    react.js做小程序 In this tutorial, I will guide you to build your own group chat application using React ...

  6. 构建meteor应用程序_我如何在一个月内构建一个复杂的文本分析应用程序

    构建meteor应用程序 by Jeffrey Flynt 由Jeffrey Flynt 我如何在一个月内构建一个复杂的文本分析应用程序 (How I built a complex text ana ...

  7. vr设备应用程序_在15分钟内构建一个VR Web应用程序

    vr设备应用程序 在15分钟内,您可以开发一个虚拟现实应用程序,并在Web浏览器,VR头盔或Google Daydream上运行它. 关键是A-Frame ,这是Mozilla VR Team构建的开 ...

  8. 全阶滑模观测器程序_滑模观测器的构建方法与流程

    本发明属于滑模控制技术领域,特别是涉及一种滑模观测器的构建方法. 背景技术: 现有技术中利用线性积分法计算驱动轴扭矩时,会受到车轮转速传感器.电机旋变误差的影响,转速传感器信号噪声.外界干扰等也会由于 ...

  9. 11旋转编码器原理_旋转编码器的原理是什么?增量式编码器和绝对式编码器有什么区别?...

    先给出结论,最重要的区别在于:增量式编码器没有记忆,断电重启必须回到参考零位,才能找到需要的位置,而绝对式编码器,有记忆,断电重启不用回到零位,即可知道目标所在的位置. 接下来细说一下,主要包含如下的 ...

最新文章

  1. 无监督学习:异常检测与剔除(局部异常因子法 SVM异常检测器)
  2. 革命性新特性 | 单一应用跨多Kubernetes集群的部署与管理
  3. 2015-05-31
  4. java中随机数彩票练习_基于javascript实现彩票随机数生成(简单版)
  5. 李航《统计学习方法》-----支持向量机
  6. oracle的数据泵导入,导出以及创建用户及删除当前连接用户
  7. Nginx gzip参数详解及常见问题(已解决)
  8. python常用的库介绍_Python的标准库介绍与常用的第三方库
  9. 收藏 | 90+深度学习开源数据集整理:包括目标检测、工业缺陷、图像分割等多个方向(附下载)...
  10. 聊聊Hadoop DistCp的数据切分处理方式
  11. Android软键盘弹不出的问题
  12. 如何用Java写出热门大鱼吃小鱼游戏
  13. 机智云(esp8266)与74hc595控制16路继电器
  14. Openssh7.4p1更换为8.2
  15. word embedding(详细讲解word embedding)
  16. 安装mysql 配置环境变量
  17. 03-Mono Flux操作
  18. 基于STM32 + 超详细对新手全面解析讲解SPI协议(附源码)
  19. 【机器学习】当贝叶斯、奥卡姆和香农一起来定义机器学习时
  20. 微信Jsapi支付实战踩坑

热门文章

  1. 解决:provider:Named Pipes Provider error:40无法打开SQL Server的连接
  2. linux系统制作qcow2,制作qcow2镜像
  3. 第二次作业,制作调查问卷
  4. 2021数学建模国赛B题复盘详细解析
  5. 【大数据计算】(四) Spark的安装和基础编程
  6. 如何恢复已删除的照片
  7. 大佬们抖音带货流水都过亿 普通人有什么抖音变现的好方式
  8. 【小程序】微信小程序自定义导航栏及其封装
  9. 防火墙、IDS(入侵检测系统)与双机热备
  10. java后台定时弹框提醒活动脖子(myeclipse)