swift和swiftui_如何在SwiftUI中创建和设置动画复选框

本文介绍了如何在 SwiftUI 中创建并为复选框添加动画效果,详细解析了实现这一功能的步骤和技巧。
摘要由CSDN通过智能技术生成

swift和swiftui

RoundedRectangle复选框 (RoundedRectangle CheckBox)

For the first checkbox, start by creating a new struct view and adding a RoundedRectangle to it. Use the stroke function and give it a StrokeStyle with the line width of your choice:

对于第一个复选框,首先创建一个新的结构视图并向其中添加RoundedRectangle 。 使用stroke函数,并为其提供StrokeStyle并带有您选择的线宽:

Image for post

Embed the RoundedRectangle inside a ZStack and add another RoundedRectangle filled with your choice of color. Make sure that the second RoundedRectangle is smaller than the first one:

RoundedRectangle嵌入到ZStack并添加另一个填充了您选择的颜色的RoundedRectangle 。 确保第二个RoundedRectangle小于第一个:

Image for post

Declare a binding boolean variable called checked at the top of your struct view. Add a checkmark image underneath your second RoundedRectangle inside the condition bracket, as below:

在结构视图的顶部声明一个绑定的布尔变量,称为checked 。 在条件括号内的第二个RoundedRectangle下面添加一个选中标记图像,如下所示:

Image for post

Now use the same checked variable to control the rectangles’ colors:

现在使用相同的checked 变量 控制矩形的颜色:

Image for post

Declare another variable to control the trim function value of the first RoundedRectangle. This value will be animated. Have this value inside the animatableData variable:

声明另一个变量以控制第一个RoundedRectangletrim函数值。 该值将被动画化。 在animatableData变量中包含以下值:

Image for post

Change the to: value of the trim function to your new variable, trimVal:

trim函数的to:值更改为新变量trimVal

Image for post

You are done with the CheckBoxView. Inside your ContentView, add your CheckBoxView as a button and pass two state variables (checked and trimVal):

您已经完成CheckBoxView 。 在ContentView内部,将CheckBoxView添加为按钮,并传递两个状态变量( checkedtrimVal ):

Image for post

You need to change checked and trimVal inside your button. Inside withAnimation, assign the trimVal to 1 and use toggle() to change the checked boolean value. You can add an else statement to reverse the animation too:

您需要在按钮内部更改checkedtrimVal 。 内部withAnimation ,分配trimVal为1,并且使用toggle()来改变checked布尔值。 您也可以添加else语句来反转动画:

Image for post
Image for post
Code result: checkbox inside a button
代码结果:按钮内的复选框

You can use an onTapGesture instead of the button. This will eliminate the flashing that you get with a button:

您可以使用onTapGesture代替按钮。 这将消除您通过按钮获得的闪烁:

Image for post
Image for post
Code result: checkbox with a tap gesture
代码结果:带有轻击手势的复选框

圆形复选框 (Circular CheckBox)

You can go back to your CheckBoxView and change the shape to a circle, as below:

您可以返回CheckBoxView并将形状更改为圆形,如下所示:

Image for post
Image for post
Code result: circular checkbox
代码结果:圆形复选框

胶囊复选框 (Capsule CheckBox)

I have two capsules, one overlying the other. Let’s add two variables: width and removeText. The width variable will be used with the capsule’s frames. To make sure the second capsule is smaller than the first one, subtract 10 from the width. Finally, add a text view inside an if statement, as below:

我有两个胶囊,一个在另一个上。 让我们添加两个变量: widthremoveText width变量将与胶囊的框架一起使用。 为确保第二个胶囊小于第一个胶囊,请从宽度减去10。 最后,在if语句内添加一个文本视图,如下所示:

Image for post

Inside your ContentView, add the removeText and width variables and change the onTapGesture code to the code below:

ContentView ,添加removeTextwidth变量,并将onTapGesture代码更改为onTapGesture代码:

Image for post
Image for post
Code result: capsule checkbox
代码结果:胶囊复选框

All done! Thanks for reading!

全做完了! 谢谢阅读!

翻译自: https://medium.com/better-programming/how-to-create-and-animate-checkboxes-in-swiftui-e428fe7cc9c1

swift和swiftui

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值