How to use animation with value in SwiftUI (iOS 16+) | Bootcamp #67

Swiftful Thinking
12 Jun 202310:54

Summary

TLDR在这段视频中,Nick 解释了为什么 Apple 在 iOS 15 中对 Swift UI 的动画修饰符进行了更改,并展示了如何使用新的动画修饰符。他首先创建了一个简单的用户界面,然后通过一个按钮来控制一个矩形在屏幕上的左右移动。Nick 指出,旧的动画修饰符已经不推荐使用,并且展示了如何使用新的动画修饰符,它允许开发者更精确地控制动画。他通过添加不同的动画修饰符来演示如何对不同的动作应用不同的动画效果,强调了新修饰符如何帮助开发者避免不必要的动画效果,并提供了更精细的控制。最后,Nick 推荐开发者在所有应用中使用新的动画修饰符,并感谢观众的观看。

Takeaways

  • 📚 视频中提到了动画修改器(animation modifier)在iOS 15中被弃用,并引入了新的动画修改器,这为开发者提供了更多的控制权。
  • 🔍 新的动画修改器需要一个额外的值,但它允许开发者更精确地指定哪些视图应该被动画化。
  • 🛠️ 通过使用新的动画修改器,开发者可以避免之前动画修改器带来的问题,比如不需要动画的视图被错误地动画化。
  • 🎯 新的动画修改器通过绑定一个特定的布尔值(比如`animateOne`),来控制动画的触发,从而提供了更精细的控制。
  • 🌟 视频中展示了如何使用新的动画修改器来创建更复杂的动画效果,如同时进行水平和垂直的动画。
  • 📈 通过将动画绑定到特定的状态变量,开发者可以更灵活地控制动画的开始和结束。
  • 🚀 视频中提到,使用新的动画修改器是一个重大的升级,因为它允许开发者更明确地告诉视图何时进行动画化。
  • 📝 作者推荐在所有应用中100%使用新的动画修改器,因为它避免了旧动画修改器的问题。
  • 📌 视频中强调了在构建复杂的Swift UI应用时,新的动画修改器对于控制动画的细节至关重要。
  • 💡 作者还提到,尽管UI在视频中不是重点,但理解新的动画修改器如何工作对于开发者来说非常重要。
  • 📘 最后,作者提醒观众,尽管弃用消息中提到了`withAnimation`,但最好始终使用带有值的`animation`修改器。

Q & A

  • 为什么原来的动画修饰符被弃用了?

    -原来的动画修饰符被弃用是因为Apple在iOS 15中引入了新的动画修饰符,它不仅改变了代码的使用方式,还提供了开发者对动画更多的控制能力。

  • 新的动画修饰符与旧的有什么区别?

    -新的动画修饰符允许开发者通过一个额外的值来指定动画效果,并且只在该值改变时应用动画,这样开发者可以更精确地控制动画的触发条件和效果。

  • 为什么使用新的动画修饰符可以避免一些bug和问题?

    -使用新的动画修饰符可以避免动画被错误地应用到不应该动画化的UI元素上,因为它允许开发者指定动画应该应用于哪个特定的状态变化。

  • 如何在Swift UI中创建一个简单的左右移动动画?

    -可以通过改变视图的对齐方式(alignment)来创建一个简单的左右移动动画。例如,通过toggle一个状态变量来在leading和trailing之间切换。

  • 如何为不同的动作应用不同的动画效果?

    -通过使用新的动画修饰符,并结合状态变量,可以为不同的动作应用不同的动画效果。例如,可以为一个动作使用spring动画,而为另一个动作使用linear动画或者不应用动画。

  • 在Swift UI中,如何同时运行多个动画?

    -可以通过堆叠多个动画修饰符来同时运行多个动画。每个动画修饰符可以绑定到不同的状态变量上,这样就可以独立控制每个动画的触发和行为。

  • 为什么说新的动画修饰符是Swift UI开发者的一个重大升级?

    -新的动画修饰符提供了更精细的控制,允许开发者明确指定哪些视图应该动画化,动画应该如何进行,以及何时进行,从而在构建复杂的Swift UI应用时提供更大的灵活性。

  • 在Swift UI中,如何标记一个函数或方法为弃用?

    -可以通过在函数或方法的定义上使用@available属性,并指定一个更高版本的iOS来标记它为弃用,并提供一个弃用的消息。

  • 在Swift UI中,动画修饰符的弃用信息在哪里可以找到?

    -可以通过在代码中右键点击动画修饰符,然后选择跳转到定义,查看其可用性和弃用信息。

  • 为什么推荐在所有情况下都使用带有值的动画修饰符?

    -因为带有值的动画修饰符允许更精确的控制,可以避免动画被错误地应用到不应该动画化的UI元素上,从而减少bug和问题的发生。

  • 在Swift UI中,动画修饰符的宽度问题指的是什么?

    -动画修饰符的宽度问题指的是动画修饰符被应用到了所有变化上,而不仅仅是特定的状态变化,这可能导致一些不期望的动画效果。

  • 如何理解Swift UI中的动画时间控制和动画效果?

    -通过使用新的动画修饰符,开发者可以控制动画的时间(如使用linear指定持续时间)和动画效果(如使用spring创建弹性效果),从而实现更丰富的用户界面动态效果。

Outlines

00:00

📚 动画修改器的更新与使用

Nick在视频中讨论了Swift UI中动画修改器的更新。他提到,尽管新的动画修改器代码与旧版本相似,但许多用户对如何使用它感到困惑。Nick解释说,新修改器实际上给了开发者更多的控制权,可以更精细地控制动画。他通过创建一个ZStack和VStack,以及一个按钮和一个矩形,展示了如何使用新的动画修改器,并解释了为什么Apple会进行API的更改。

05:01

🔄 动画效果的自定义与控制

Nick展示了如何使用新的动画修改器来控制视图中的动画效果。他通过创建两个动作(action one 和 action two),并为它们设置不同的动画效果,来演示如何对同一个视图中的不同元素应用不同的动画。他强调了新动画修改器的优势,即能够更精确地控制哪些元素应该动画化,以及动画的具体方式和时间。此外,Nick还提到了使用动画值(animation value)的重要性,并展示了如何通过改变这个值来控制动画的触发。

10:03

⚠️ 弃用旧动画修改器与推荐使用新方法

Nick指出,旧的动画修改器已经被弃用,并在iOS 15中标记为不推荐使用。他建议开发者始终使用新的动画修改器,并强调了其在构建复杂的Swift UI应用时的优势。Nick还提到了在旧动画修改器的弃用信息中推荐使用的新动画修改器存在的问题,并重申了他的建议:在所有应用中100%使用带有值的动画修改器。视频以感谢观众的观看和预告下一期视频结束。

Mindmap

Keywords

💡动画修改器

动画修改器是Swift UI中用于创建和控制用户界面动画的工具。在视频中,提到了旧的动画修改器被弃用,并引入了新的动画修改器,这允许开发者更精细地控制动画效果。例如,通过使用新的动画修改器,开发者可以为不同的动作指定不同的动画效果,如弹簧效果或线性效果。

💡API变更

API变更指的是应用程序编程接口(Application Programming Interface)的变化。在视频中,提到了Apple对Swift UI的动画API进行了更改,这是为了提供更多的控制力和更好的用户体验。API变更通常意味着开发者需要更新他们的代码以适应新的API。

💡弃用

弃用是指某个特性或者方法在将来的版本中将不再被支持,但目前仍然可以使用。在视频中,提到了原来的动画修改器被标记为弃用,并推荐使用新的动画修改器。弃用是软件开发中常见的做法,它给开发者提供了一个过渡期来更新他们的应用程序。

💡Swift UI

Swift UI是一个由Apple开发的用于创建用户界面的框架,它允许开发者以声明式的方式构建iOS、macOS、watchOS和tvOS应用程序的界面。视频中主要围绕Swift UI的动画功能进行讲解,展示了如何使用Swift UI创建和管理动画。

💡动画效果

动画效果指的是用户界面元素在屏幕上的视觉运动或变化。在视频中,通过Swift UI的动画修改器,可以实现多种动画效果,如弹簧效果、线性运动等。动画效果可以提升用户界面的交互性和吸引力。

💡值绑定

值绑定是一种技术,允许开发者将Swift UI视图的属性绑定到数据源上。在视频中,通过使用值绑定,可以控制动画的触发条件,使得动画仅在特定数据源变化时发生。这是一种提高代码灵活性和可维护性的方法。

💡ZStack

ZStack是Swift UI中的一个视图,用于在Z轴方向上堆叠多个子视图,从而创建层叠的布局效果。在视频中,ZStack被用来组织和布局按钮和矩形视图,以展示动画效果。

💡对齐

对齐在Swift UI中指的是视图元素在父视图中的位置调整。视频里通过改变对齐属性,实现了视图元素在屏幕上的水平和垂直移动,这是创建动画效果的基础之一。

💡

在Swift UI中,帧指的是视图的尺寸和位置。视频里通过设置帧的宽度和高度,定义了矩形视图的大小,并使用帧的变化来创建动画效果。

💡条件语句

条件语句是编程中的一种结构,用于根据不同的条件执行不同的代码块。在视频中,使用三元运算符(一个简化的条件语句)来根据变量`animateOne`的值决定视图的对齐方式。

💡多重动画

多重动画指的是在同一个视图中同时运行多个动画效果。视频展示了如何通过Swift UI的动画系统控制同一个视图中的多个动画,这在构建复杂的用户界面时非常有用。

Highlights

视频介绍了如何在Swift UI中使用新的动画修饰符,解释了为什么Apple更改了API,提供了对动画更多的控制。

动画修饰符的更改不仅仅是代码的变更,还增加了开发者对动画的控制能力。

介绍了如何使用新的动画修饰符以及为什么它比旧版本更优秀。

展示了如何在iOS 15中使用新的动画修饰符替代已弃用的版本。

通过一个简单的例子说明了新动画修饰符的使用,如何控制一个矩形在屏幕上的移动。

演示了如何通过动画修饰符实现视图的跳跃和平滑过渡效果。

解释了如何在同一个视图中应用多个动画或效果,并如何控制它们。

展示了如何通过动画修饰符的值来控制特定动画的触发条件。

强调了新动画修饰符允许开发者更精确地指定哪些视图应该被动画化,以及动画的时机和方式。

通过实际代码示例展示了如何叠加动画修饰符来实现复杂的动画效果。

说明了新动画修饰符对于构建复杂Swift UI应用的影响和重要性。

推荐开发者在所有应用中100%使用新的动画修饰符,因为它提供了更好的控制和更少的bug。

指出了原始弃用信息中提到的width动画修饰符存在的问题,并推荐使用带有值的动画修饰符。

视频最后强调了新动画修饰符的简单性和它在实际应用中的重大影响。

Nick提到了很多人在他的先前视频下评论有关动画修饰符弃用的问题,说明了新动画修饰符的必要性。

视频以感谢观众和预告下一视频结束,保持了Nick一贯的风格。

Transcripts

play00:01

yo what is up everybody uh this is the

play00:04

video that I did not know that I needed

play00:06

to make but we're making it uh I've had

play00:09

a hundred and one people comment on the

play00:12

animation video that I put in this

play00:13

playlist that the original animation

play00:16

modifier is deprecated and people are

play00:19

very confused how to use the new one uh

play00:22

the new one is literally the same line

play00:24

of code with like one extra value in it

play00:27

but I've had enough people comment

play00:29

asking how to use it that they thought

play00:31

it would deserve its own video so that's

play00:33

we're gonna dive into here uh but I

play00:35

actually want to show you guys that not

play00:37

just how to use this new modifier but

play00:39

why Apple has changed the API because

play00:42

there's a new modifier it's not just

play00:43

changing our code it's actually giving

play00:45

us more control over the animation as

play00:48

the developer so I assume most of you

play00:50

already know how to use this but uh it's

play00:53

definitely tripping some people up so

play00:55

let's cover it let's get in our toolbox

play00:57

and let's move on

play01:06

welcome back everybody my name is Nick

play01:09

this is swiffel thinking and we're going

play01:12

to look at animation yet again in this

play01:14

playlist let's right click the Navigator

play01:16

create a new file this will be a swift

play01:19

UI view as always we're going to call

play01:20

this one animation updated boot camp

play01:25

and let's click create

play01:27

and the reason we're calling it updated

play01:30

is because earlier in this playlist we

play01:32

already did an animation bootcamp

play01:34

so if I come back up here to one of the

play01:37

earliest Maybe video number 20 or

play01:39

something like that we did an animation

play01:41

timing and an animation bootcamp here

play01:43

let's mark this animation bootcamp as

play01:47

deprecated

play01:48

now we can see already here that in the

play01:51

regular animation modifier that we went

play01:53

over in this last boot camp we get this

play01:56

warning here from the compiler that it

play01:58

is deprecated in iOS 15 and to use the

play02:02

other animation modifier that we're

play02:03

going to learn right now

play02:04

if I right click and jump to the

play02:06

definition of this we can see the at

play02:09

available iOS and the deprecated message

play02:12

here and so what we're going to do is

play02:14

actually copy this deprecated this ad

play02:16

available and we're going to put it on

play02:19

here we're doing that just so that if we

play02:22

were to use the animation bootcamp we

play02:24

would get another warning so for example

play02:27

if I put this up here and I call this

play02:29

we're going to now get a warning that

play02:33

animation bootcamp was deprecated so

play02:35

obviously this is not a real app so it

play02:36

doesn't really matter and we're going to

play02:38

use a star here so it's for all

play02:40

platforms

play02:41

and we're just going to put the message

play02:43

here and let's use the new animation

play02:46

updated bootcamp instead

play02:49

all right so I'm going to jump into the

play02:50

animation updated bootcamp and let's

play02:52

first look at why Apple has made this

play02:54

change and then how we can use the new

play02:56

modifier

play02:58

all right so there are a lot of use

play02:59

cases here I'm just going to go through

play03:01

a very simple one just to kind of you

play03:03

know highlight what's going on here

play03:04

let's create a z stack

play03:06

and

play03:08

in here let's add maybe a v-stack let's

play03:11

do some spacing of about 40 and let's

play03:15

just put in maybe a button that says

play03:18

action one and we're gonna put an action

play03:20

and then we're going to just copy and

play03:23

paste that and we're going to do an

play03:24

action 2 as well

play03:26

below this on the v-stack let's add in a

play03:29

rectangle real quick I'll give it a

play03:31

frame of maybe 100 by 100 that looks

play03:35

pretty good and I'm just gonna wrap this

play03:38

in some sort of z-stack as well so that

play03:41

on this Z stack here so I'll put a

play03:43

background of color.red

play03:45

and then I'll just set this to be a Max

play03:48

width to a frame of maybe a Max width

play03:54

of infinity and a Max height of infinity

play03:58

again we don't we are just setting up

play04:00

some like basic view here this is not

play04:02

really important that we have a UI but

play04:06

so what we're going to do is basically

play04:07

animate this rectangle around the screen

play04:09

real quick and I'm going to start by

play04:11

doing another frame here we're just

play04:13

going to say a frame the max width

play04:17

of infinity

play04:19

and let's add an alignment

play04:22

of leading

play04:24

all right so just so we can see this

play04:26

Frame versus this Frame this Frame is

play04:28

obviously black

play04:30

this Frame after we extended to Infinity

play04:32

let's add a background of color dot

play04:36

green

play04:37

all right so what we're going to do is

play04:38

basically animate this Square here from

play04:41

the left side to the right side I'm

play04:42

going to do that by animating the

play04:43

alignment to trailing

play04:46

I've done this in earlier videos this is

play04:48

not New Logic let's get that working so

play04:50

up here I'm going to do an at State

play04:52

private VAR let's say animate one of

play04:56

type pool equals false and when we click

play04:59

on action one we're going to call

play05:00

animate one toggle let's do a very

play05:03

simple ternary operator here we'll say

play05:05

animate one if it's true we will go

play05:08

leading otherwise we will go trailing

play05:11

all right

play05:13

and when I click it we can see it jump

play05:15

awesome now let's get into some

play05:18

animation so firstly it's just jumping

play05:19

but maybe we want to actually animate it

play05:21

from the side from side to side and so

play05:24

what we can do is apply maybe a DOT

play05:26

animation and there are two completions

play05:29

here so obviously it's a deprecated one

play05:31

that we've previously used and then

play05:33

there's this new one but let's start

play05:34

with the deprecated one here and I'm

play05:36

just going to do maybe a DOT spring for

play05:41

now

play05:41

all right so we got this pretty cool

play05:43

animation obviously it's working but

play05:46

let's imagine now we want to do another

play05:48

animation where it maybe goes up and

play05:51

down so I'm just going to copy and paste

play05:53

this line here I'm going to do another

play05:55

one with maybe a Max height

play05:58

of infinity

play06:00

and let's just change it so the first

play06:01

one is green and this no Max height now

play06:04

is orange

play06:06

okay

play06:07

so we're going to do is basically

play06:08

animate green left and right and we're

play06:10

going to animate orange top to bottom

play06:13

and so inside the Orange Let's do

play06:15

alignment from top to bottom

play06:19

so when I click animation obviously it's

play06:21

working but now this is kind of

play06:23

obviously not a real screen in your app

play06:25

but there are many times in your app

play06:27

where you're going to have multiple

play06:28

animations or effects inside the same

play06:31

view so for example maybe we wanted

play06:33

action 2 to move it up and down and then

play06:36

action one to go left and right so I

play06:38

could say create a second animate 2 here

play06:41

and we can toggle that inside action two

play06:44

and then animate two will go top to

play06:47

bottom

play06:48

so now I can do this action here and I

play06:50

can do this action here obviously it's

play06:52

working perfectly but what you're

play06:54

noticing is that the spring animation

play06:57

here is applied to both of these actions

play07:00

and so what is the use case if I wanted

play07:02

action one to be a spring and I wanted

play07:05

action 2 to maybe be a different

play07:07

animation timing maybe I wanted no

play07:11

animation on action 2. now I can't

play07:13

actually stop it right so if I took off

play07:16

the animation

play07:18

I can now do both without animation but

play07:21

I can't just do one with animation and

play07:23

one without and that's kind of where

play07:25

this new animation modifier is our

play07:27

savior so you guys don't know how many

play07:30

people have commented on my previous

play07:32

animation video that it is deprecating

play07:34

they are people I guess are super

play07:36

confused on what to do all we need to do

play07:40

is use the animation value instead and

play07:42

so what this allows us to do is another

play07:44

modifier where we had a value

play07:47

and the only difference is that this

play07:49

animation is only going to be applied

play07:52

when this value changes

play07:56

so previously this animation is applied

play07:58

to any change on this view right whether

play08:03

or not it's animate one anime 2 whether

play08:05

we're moving the rectangle or something

play08:07

else on the screen anything that changes

play08:09

on this view is going to get this

play08:11

animation

play08:13

and that led to a lot of like bugs and

play08:16

like weirdness in Swift UI apps because

play08:18

you would have things that are animating

play08:20

that you didn't mean to animate and so

play08:23

the solution here is this new value

play08:25

modifier where we can specify that I

play08:27

want to use the spring animation

play08:29

specifically for animate one so when

play08:32

animate one changes anything that is

play08:35

anything that is changing due to animate

play08:37

1 will get that spring modifier so if I

play08:40

look at action one here I now get that

play08:42

spring modifier if I look at action 2

play08:44

there's no animation

play08:46

so this is actually a major upgrade for

play08:50

us as Swift UI developers because we can

play08:52

now customize and very explicitly tell

play08:55

this view what do we want to animate how

play08:58

do we want to animate when do we want to

play08:59

animate it's not this one animation

play09:01

modifier that's applied to everything

play09:04

so for example I could do a second here

play09:06

with an animate 2 now and I can stack

play09:08

these animation modifiers so now animate

play09:11

2 also has spring but I could also maybe

play09:14

change animate 2 to a linear of maybe

play09:17

five seconds

play09:19

so now I got the nice spring going left

play09:21

and right I have this five seconds going

play09:23

up and down obviously not incredibly

play09:25

important the UI in this video but it is

play09:28

important to understand that the the

play09:30

value is that with this new modifier we

play09:34

can have much more control over what is

play09:37

animating when it is animating which

play09:39

animation timings are being used

play09:42

so very simple and subtle change to your

play09:44

code but it has major impacts when

play09:47

you're building complex Swift UI apps

play09:50

all right that's it for this video hope

play09:52

you guys are now maybe a little less

play09:54

confused I can't tell you how many

play09:56

people commented on the previous video

play09:57

so this is the new way to use animation

play09:59

I highly highly recommend it the last

play10:02

thing I will point out is that in the

play10:04

original deprecated message it does say

play10:06

use with animation or the new animation

play10:09

modifier

play10:10

the width animation basically has the

play10:13

same problems that this original

play10:15

animation modifier has so my

play10:18

recommendation is to use the animation

play10:20

with the value a hundred percent of the

play10:22

time it's the only one that I'm using in

play10:24

my apps all right

play10:28

and I will just leave this here

play10:30

so that we know that this one I will

play10:33

mark this one as maybe deprecate awesome

play10:37

thank you guys for watching as always

play10:39

I'm Nick this is swiftful thinking and

play10:42

I'll see you in the next video

play10:46

[Music]

Rate This

5.0 / 5 (0 votes)

Related Tags
SwiftUI动画API教程Apple控制修饰符编程视频技术更新
Do you need a summary in English?