How to use animation with value in SwiftUI (iOS 16+) | Bootcamp #67
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
📚 动画修改器的更新与使用
Nick在视频中讨论了Swift UI中动画修改器的更新。他提到,尽管新的动画修改器代码与旧版本相似,但许多用户对如何使用它感到困惑。Nick解释说,新修改器实际上给了开发者更多的控制权,可以更精细地控制动画。他通过创建一个ZStack和VStack,以及一个按钮和一个矩形,展示了如何使用新的动画修改器,并解释了为什么Apple会进行API的更改。
🔄 动画效果的自定义与控制
Nick展示了如何使用新的动画修改器来控制视图中的动画效果。他通过创建两个动作(action one 和 action two),并为它们设置不同的动画效果,来演示如何对同一个视图中的不同元素应用不同的动画。他强调了新动画修改器的优势,即能够更精确地控制哪些元素应该动画化,以及动画的具体方式和时间。此外,Nick还提到了使用动画值(animation value)的重要性,并展示了如何通过改变这个值来控制动画的触发。
⚠️ 弃用旧动画修改器与推荐使用新方法
Nick指出,旧的动画修改器已经被弃用,并在iOS 15中标记为不推荐使用。他建议开发者始终使用新的动画修改器,并强调了其在构建复杂的Swift UI应用时的优势。Nick还提到了在旧动画修改器的弃用信息中推荐使用的新动画修改器存在的问题,并重申了他的建议:在所有应用中100%使用带有值的动画修改器。视频以感谢观众的观看和预告下一期视频结束。
Mindmap
Keywords
💡动画修改器
💡API变更
💡弃用
💡Swift UI
💡动画效果
💡值绑定
💡ZStack
💡对齐
💡帧
💡条件语句
💡多重动画
Highlights
视频介绍了如何在Swift UI中使用新的动画修饰符,解释了为什么Apple更改了API,提供了对动画更多的控制。
动画修饰符的更改不仅仅是代码的变更,还增加了开发者对动画的控制能力。
介绍了如何使用新的动画修饰符以及为什么它比旧版本更优秀。
展示了如何在iOS 15中使用新的动画修饰符替代已弃用的版本。
通过一个简单的例子说明了新动画修饰符的使用,如何控制一个矩形在屏幕上的移动。
演示了如何通过动画修饰符实现视图的跳跃和平滑过渡效果。
解释了如何在同一个视图中应用多个动画或效果,并如何控制它们。
展示了如何通过动画修饰符的值来控制特定动画的触发条件。
强调了新动画修饰符允许开发者更精确地指定哪些视图应该被动画化,以及动画的时机和方式。
通过实际代码示例展示了如何叠加动画修饰符来实现复杂的动画效果。
说明了新动画修饰符对于构建复杂Swift UI应用的影响和重要性。
推荐开发者在所有应用中100%使用新的动画修饰符,因为它提供了更好的控制和更少的bug。
指出了原始弃用信息中提到的width动画修饰符存在的问题,并推荐使用带有值的动画修饰符。
视频最后强调了新动画修饰符的简单性和它在实际应用中的重大影响。
Nick提到了很多人在他的先前视频下评论有关动画修饰符弃用的问题,说明了新动画修饰符的必要性。
视频以感谢观众和预告下一视频结束,保持了Nick一贯的风格。
Transcripts
yo what is up everybody uh this is the
video that I did not know that I needed
to make but we're making it uh I've had
a hundred and one people comment on the
animation video that I put in this
playlist that the original animation
modifier is deprecated and people are
very confused how to use the new one uh
the new one is literally the same line
of code with like one extra value in it
but I've had enough people comment
asking how to use it that they thought
it would deserve its own video so that's
we're gonna dive into here uh but I
actually want to show you guys that not
just how to use this new modifier but
why Apple has changed the API because
there's a new modifier it's not just
changing our code it's actually giving
us more control over the animation as
the developer so I assume most of you
already know how to use this but uh it's
definitely tripping some people up so
let's cover it let's get in our toolbox
and let's move on
welcome back everybody my name is Nick
this is swiffel thinking and we're going
to look at animation yet again in this
playlist let's right click the Navigator
create a new file this will be a swift
UI view as always we're going to call
this one animation updated boot camp
and let's click create
and the reason we're calling it updated
is because earlier in this playlist we
already did an animation bootcamp
so if I come back up here to one of the
earliest Maybe video number 20 or
something like that we did an animation
timing and an animation bootcamp here
let's mark this animation bootcamp as
deprecated
now we can see already here that in the
regular animation modifier that we went
over in this last boot camp we get this
warning here from the compiler that it
is deprecated in iOS 15 and to use the
other animation modifier that we're
going to learn right now
if I right click and jump to the
definition of this we can see the at
available iOS and the deprecated message
here and so what we're going to do is
actually copy this deprecated this ad
available and we're going to put it on
here we're doing that just so that if we
were to use the animation bootcamp we
would get another warning so for example
if I put this up here and I call this
we're going to now get a warning that
animation bootcamp was deprecated so
obviously this is not a real app so it
doesn't really matter and we're going to
use a star here so it's for all
platforms
and we're just going to put the message
here and let's use the new animation
updated bootcamp instead
all right so I'm going to jump into the
animation updated bootcamp and let's
first look at why Apple has made this
change and then how we can use the new
modifier
all right so there are a lot of use
cases here I'm just going to go through
a very simple one just to kind of you
know highlight what's going on here
let's create a z stack
and
in here let's add maybe a v-stack let's
do some spacing of about 40 and let's
just put in maybe a button that says
action one and we're gonna put an action
and then we're going to just copy and
paste that and we're going to do an
action 2 as well
below this on the v-stack let's add in a
rectangle real quick I'll give it a
frame of maybe 100 by 100 that looks
pretty good and I'm just gonna wrap this
in some sort of z-stack as well so that
on this Z stack here so I'll put a
background of color.red
and then I'll just set this to be a Max
width to a frame of maybe a Max width
of infinity and a Max height of infinity
again we don't we are just setting up
some like basic view here this is not
really important that we have a UI but
so what we're going to do is basically
animate this rectangle around the screen
real quick and I'm going to start by
doing another frame here we're just
going to say a frame the max width
of infinity
and let's add an alignment
of leading
all right so just so we can see this
Frame versus this Frame this Frame is
obviously black
this Frame after we extended to Infinity
let's add a background of color dot
green
all right so what we're going to do is
basically animate this Square here from
the left side to the right side I'm
going to do that by animating the
alignment to trailing
I've done this in earlier videos this is
not New Logic let's get that working so
up here I'm going to do an at State
private VAR let's say animate one of
type pool equals false and when we click
on action one we're going to call
animate one toggle let's do a very
simple ternary operator here we'll say
animate one if it's true we will go
leading otherwise we will go trailing
all right
and when I click it we can see it jump
awesome now let's get into some
animation so firstly it's just jumping
but maybe we want to actually animate it
from the side from side to side and so
what we can do is apply maybe a DOT
animation and there are two completions
here so obviously it's a deprecated one
that we've previously used and then
there's this new one but let's start
with the deprecated one here and I'm
just going to do maybe a DOT spring for
now
all right so we got this pretty cool
animation obviously it's working but
let's imagine now we want to do another
animation where it maybe goes up and
down so I'm just going to copy and paste
this line here I'm going to do another
one with maybe a Max height
of infinity
and let's just change it so the first
one is green and this no Max height now
is orange
okay
so we're going to do is basically
animate green left and right and we're
going to animate orange top to bottom
and so inside the Orange Let's do
alignment from top to bottom
so when I click animation obviously it's
working but now this is kind of
obviously not a real screen in your app
but there are many times in your app
where you're going to have multiple
animations or effects inside the same
view so for example maybe we wanted
action 2 to move it up and down and then
action one to go left and right so I
could say create a second animate 2 here
and we can toggle that inside action two
and then animate two will go top to
bottom
so now I can do this action here and I
can do this action here obviously it's
working perfectly but what you're
noticing is that the spring animation
here is applied to both of these actions
and so what is the use case if I wanted
action one to be a spring and I wanted
action 2 to maybe be a different
animation timing maybe I wanted no
animation on action 2. now I can't
actually stop it right so if I took off
the animation
I can now do both without animation but
I can't just do one with animation and
one without and that's kind of where
this new animation modifier is our
savior so you guys don't know how many
people have commented on my previous
animation video that it is deprecating
they are people I guess are super
confused on what to do all we need to do
is use the animation value instead and
so what this allows us to do is another
modifier where we had a value
and the only difference is that this
animation is only going to be applied
when this value changes
so previously this animation is applied
to any change on this view right whether
or not it's animate one anime 2 whether
we're moving the rectangle or something
else on the screen anything that changes
on this view is going to get this
animation
and that led to a lot of like bugs and
like weirdness in Swift UI apps because
you would have things that are animating
that you didn't mean to animate and so
the solution here is this new value
modifier where we can specify that I
want to use the spring animation
specifically for animate one so when
animate one changes anything that is
anything that is changing due to animate
1 will get that spring modifier so if I
look at action one here I now get that
spring modifier if I look at action 2
there's no animation
so this is actually a major upgrade for
us as Swift UI developers because we can
now customize and very explicitly tell
this view what do we want to animate how
do we want to animate when do we want to
animate it's not this one animation
modifier that's applied to everything
so for example I could do a second here
with an animate 2 now and I can stack
these animation modifiers so now animate
2 also has spring but I could also maybe
change animate 2 to a linear of maybe
five seconds
so now I got the nice spring going left
and right I have this five seconds going
up and down obviously not incredibly
important the UI in this video but it is
important to understand that the the
value is that with this new modifier we
can have much more control over what is
animating when it is animating which
animation timings are being used
so very simple and subtle change to your
code but it has major impacts when
you're building complex Swift UI apps
all right that's it for this video hope
you guys are now maybe a little less
confused I can't tell you how many
people commented on the previous video
so this is the new way to use animation
I highly highly recommend it the last
thing I will point out is that in the
original deprecated message it does say
use with animation or the new animation
modifier
the width animation basically has the
same problems that this original
animation modifier has so my
recommendation is to use the animation
with the value a hundred percent of the
time it's the only one that I'm using in
my apps all right
and I will just leave this here
so that we know that this one I will
mark this one as maybe deprecate awesome
thank you guys for watching as always
I'm Nick this is swiftful thinking and
I'll see you in the next video
[Music]
浏览更多相关视频
How to use Popover modifier in SwiftUI | Bootcamp #69
User Experience and Animations in SwiftUI app | Todo List #5
How to use Group in SwiftUI | Bootcamp #66
Use any texture and animations for Unity's Line Renderer
Unreal Engine 5 RPG Tutorial Series - #2: Locomotion - Blendspace, Crouching and Procedural Leaning!
Unreal Engine 5 RPG Tutorial Series - #9: Combat
5.0 / 5 (0 votes)