Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用 SwiftUI 实现 3D Scroll 效果 #6921

Merged
merged 52 commits into from
Apr 22, 2020
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
afb007d
更新测试
chaingangway Mar 30, 2020
a261e82
Revert "更新测试"
chaingangway Mar 30, 2020
d09f476
Merge remote-tracking branch 'upstream/master'
chaingangway Apr 2, 2020
c01a3df
Merge remote-tracking branch 'upstream/master'
chaingangway Apr 11, 2020
7a64d92
Merge remote-tracking branch 'upstream/master'
chaingangway Apr 13, 2020
b087dee
初译完成
chaingangway Apr 15, 2020
862cc57
8 个值得了解的树形数据结构 (#6804)
Amberlin1970 Mar 27, 2020
0632846
眼动跟踪和移动世界的最佳用户体验实践 (#6806)
Charlo-O Mar 27, 2020
a66e069
我并不讨厌箭头函数(#6610) (#6659)
TiaossuP Mar 27, 2020
28c79b4
掌握 JavaScript 面试:什么是纯函数? (#6828)
nia3y Mar 28, 2020
57bd9bd
fix:文章翻译问题 (#6833)
fireairforce Mar 28, 2020
95eec4a
用依赖注入来解耦你的代码 (#6823)
JalanJiang Mar 29, 2020
e4c0cac
Create generator-functions-in-javascript.md (#6841)
lsvih Mar 31, 2020
0465602
Create how-to-keep-your-dependencies-secure-and-up-to-date.md (#6843)
lsvih Mar 31, 2020
bc64328
Create deep-dive-into-react-fiber-internals.md (#6845)
lsvih Mar 31, 2020
e4759ff
Update deep-dive-into-react-fiber-internals.md
lsvih Mar 31, 2020
4e78a12
Create how-can-cloud-services-help-improve-your-businessess-efficienc…
lsvih Mar 31, 2020
ae7da28
Create should-you-learn-vim-as-a-developer-in-2020.md (#6849)
lsvih Mar 31, 2020
6738a6e
Go 发布新版 Protobuf API (#6827)
stuchilde Mar 31, 2020
d884798
JSON.stringify() 的 5 个秘密特性 (#6793)
fireairforce Mar 31, 2020
d04aff3
Create high-speed-inserts-with-mysql.md (#6853)
lsvih Apr 1, 2020
85cc9e1
组合软件:书 (#6832)
fireairforce Apr 2, 2020
d8fc4d3
Create 6-best-javascript-frameworks-in-2020.md (#6861)
lsvih Apr 3, 2020
a8c74da
2020 年用各大前端框架构建的 RealWorld 应用对比 (#6851)
snowyYU Apr 4, 2020
77f1fb3
Create the-importance-of-why-docs.md (#6868)
lsvih Apr 6, 2020
8260ddc
不变性之道 (#6857)
nia3y Apr 6, 2020
548dfe6
Create polymorphic-react-components.md (#6870)
lsvih Apr 6, 2020
b8ba5b2
Create active-learning-in-machine-learning.md (#6872)
lsvih Apr 6, 2020
509ad30
Create kafka-vs-rabbitmq-why-use-kafka.md (#6874)
lsvih Apr 6, 2020
bc01bdc
Create i-built-an-app-that-uses-all-7-new-features-in-javascript-es20…
lsvih Apr 6, 2020
d278484
云服务如何帮助你提高业务效率? (#6859)
QinRoc Apr 7, 2020
d250ddb
NestJS 实现基本用户认证和会话 (#6731)
eizyc Apr 7, 2020
b1a3030
作为 2020 年的开发者,你应该学习 VIM 吗? (#6856)
chaingangway Apr 7, 2020
da77d74
MySQL 最佳实践—— 高效插入数据 (#6863)
stuchilde Apr 8, 2020
5531303
Create combine-getting-started.md (#6883)
lsvih Apr 9, 2020
69ae6a6
Create how-to-be-a-good-remote-developer.md (#6885)
lsvih Apr 9, 2020
6c210ce
Create why-is-object-immutability-important.md (#6887)
lsvih Apr 9, 2020
e0b042e
Create what-on-earth-is-the-shadow-dom-and-why-it-matters.md
lsvih Apr 9, 2020
cc61cef
2020 年排名前 6 位的 JavaScript 框架 (#6867)
QinRoc Apr 9, 2020
53c6ba1
JavaScript 风格元素 (#6878)
febrainqu Apr 9, 2020
533bef9
怎样让依赖库保持安全和最新 (#6864)
chaingangway Apr 9, 2020
ff11c6e
Create 5-best-practices-to-prevent-git-leaks.md (#6894)
lsvih Apr 10, 2020
e3a066a
Create swiftui-3d-scroll-effect.md (#6896)
lsvih Apr 10, 2020
7153da6
Web 应用程序中的数据和 UI 分离 (#6794)
fireairforce Apr 10, 2020
e113faf
fix:typo (#6903)
fireairforce Apr 12, 2020
ce21ade
Merge remote-tracking branch 'upstream/master'
chaingangway Apr 15, 2020
a6d253b
Merge branch 'master' into translation/swiftui3d.md
chaingangway Apr 15, 2020
dd87291
Update TODO1/swiftui-3d-scroll-effect.md
chaingangway Apr 22, 2020
ff8c582
Update TODO1/swiftui-3d-scroll-effect.md
chaingangway Apr 22, 2020
e869717
Update TODO1/swiftui-3d-scroll-effect.md
chaingangway Apr 22, 2020
4893734
Update TODO1/swiftui-3d-scroll-effect.md
chaingangway Apr 22, 2020
9f788e5
更新校对者
chaingangway Apr 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions TODO1/swiftui-3d-scroll-effect.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
> * 原文作者:[Jean-Marc Boullianne](https://medium.com/@jboullianne)
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO1/swiftui-3d-scroll-effect.md](https://github.com/xitu/gold-miner/blob/master/TODO1/swiftui-3d-scroll-effect.md)
> * 译者:
> * 校对者:
> * 译者:[chaingangway](https://github.com/chaingangway)
> * 校对者:[lsvih](https://github.com/lsvih)

# SwiftUI 3D Scroll Effect
# SwiftUI 实现 3D Scroll 效果

![Finished 3D Scroll Effect](https://cdn-images-1.medium.com/max/2000/0*pYnR4ym84WIZk3tf.gif)

Here’s a look at the kind of 3D scroll effect we’ll be making today. At the end of this tutorial, you’ll be able to add this 3D effect to any custom SwiftUI view in your app. Let’s get started!
我们预览下今天要实现的 3D scroll 效果。学完本教程后,你就可以在你的 App 中把这种 3D 效果加入任何自定义的 SwiftUI 视图。下面我们来开始本教程的学习。

## Getting Started
## 入门

Start by creating a new SwiftUI View. For example purposes, I’ll be showing a list of rectangles in different colors, so I named my view `ColorList`.
首先,创建一个新的 SwiftUI 视图。为了举例说明,在这个新视图中,我会展示一个有各种颜色的矩形列表,并把新视图命名为 `ColorList`

```swift
import SwiftUI
Expand All @@ -31,17 +31,17 @@ struct ColorList_Previews: PreviewProvider {
}
```

## Color Data
## 颜色数据

At the top of your view struct, add a variable for keeping track of colors.
在视图的结构体里,添加一个用于记录颜色的变量。

```swift
var colors: [Colors]
```

## Making the List
## 实现这个列表

Inside your `body` variable, get rid of the placeholder `Text`. Add in a `HStack` wrapping in a `ScrollView` like this.
`body` 变量的内部,删除掉占位 `Text`。在 `ScrollView` 嵌套中添加一个 `HStack`,如下:

```swift
var body: some View {
Expand All @@ -53,9 +53,9 @@ var body: some View {
}
```

## Show the Rectangles
## 展示矩形

Inside your `HStack` we need to show a `Rectangle` for each color stored in `colors`. For this we'll use a `ForEach`. I've gone ahead and modified the frame for the rectangle to something more relatable to a traditional UI Card.
我们使用 `ForEach` 在 `HStack` 内部根据 `colors` 中的数据分别创建不同颜色的矩形。此外,我修改了矩形的 frame,让它看起来与传统 UI 布局更像一些。

```swift
var body: some View {
Expand All @@ -71,7 +71,7 @@ var body: some View {
}
```

And if you go ahead and provide the preview struct with a list of colors like this:
在 Preview 结构体中传入如下的颜色参数:

```swift
struct ColorList_Previews: PreviewProvider {
Expand All @@ -81,13 +81,13 @@ struct ColorList_Previews: PreviewProvider {
}
```

You should see this!
你可以看到下图中的效果:

![](https://cdn-images-1.medium.com/max/2000/0*NfpStvbJHfMO2Tqq.png)

## Adding the 3D Effect
## 增加 3D 效果

Start by wrapping your `Rectangle` in a `GeometryReader`. This will allow us to grab a reference to the frame of the `Rectangle` as it moves across the screen.
首先,把 `Rectangle` 嵌套在 `GeometryReader` 中。这样的话,当 `Rectangle` 在屏幕上移动的时候,我们就可以获得其 frame 的引用。

```swift
var body: some View {
Expand All @@ -105,27 +105,27 @@ var body: some View {
}
```

You will need to change the `HStack` spacing you defined above, due to the way `GeometryReader` works.
根据 `GeometryReader` 的用法要求,我们需要修改上面定义的 `HStack` spacing 属性。

Then add this line to your `Rectangle`
`Rectangle` 中加入下面这行代码。

```swift
.rotation3DEffect(Angle(degrees: (Double(geometry.frame(in: .global).minX) - 210) / -20), axis: (x: 0, y: 1.0, z: 0))
```

The `Angle` you're passing into the function is changing as the `Rectangle` moves across the screen. Take a particular look at the `.frame(in:)` function. It allows you to grab the `CGRect` of the `Rectangle` and uses its `minX` coordinate for angle calculations.
当 `Rectangle` 在屏幕上移动时,这个方法的 `Angle` 参数会发生改变。请重点看 `.frame(in:)` 这个函数,你可以获取 `Rectangle` 的 `CGRect` 属性 `minX` 变量来计算角度。

The `axis` parameter is a Tuple that details which axis to modify using the angle you just passed in. In this case it's the Y-axis.
`axis` 参数是一个元组类型,它定义了在使用你传入的角度参数时,哪一个坐标轴要发生改变。在本例中,是 Y 轴。

> The documentation for the rotation3DEffect() can be found [here](https://developer.apple.com/documentation/swiftui/scrollview/3287538-rotation3deffect) on Apple’s Official Website.
> rotation3DEffect() 方法的文档可以在苹果官方网站的 [这里](https://developer.apple.com/documentation/swiftui/scrollview/3287538-rotation3deffect) 找到。

If you go ahead and run the example you should see your `Rectangles` rotating as they move across the screen!
下一步,把这个案例跑起来。当矩形在屏幕上移动时,你可以看到它们在旋转。

> I’ve also modified the corner radius of the rectangle as well as added a drop shadow to make it look a little better.
> 我还修改了矩形的 cornerRadius 属性,并加上了投影效果,让它更美观。

![Pretty cool right!?](https://cdn-images-1.medium.com/max/2000/0*IidRWGBSe936-9Ls.gif)

## Final Product
## 最终效果

```swift
struct ColorList: View {
Expand All @@ -151,9 +151,9 @@ struct ColorList: View {
}
```

## That’s all Folks!
## 结束

If you enjoyed this post, please consider subscribing to my website using this [link](https://trailingclosure.com/signup/), and if you aren’t reading this on [TrailingClosure.com](https://trailingclosure.com/), please come check us out sometime!
如果您喜欢这篇文章,可以用这个 [链接](https://trailingclosure.com/signup/) 订阅我们的网站。如果您不是在 [TrailingClosure.com](https://trailingclosure.com/) 阅读本文,以后也可以来这个网站看看。

> 如果发现译文存在错误或其他需要改进的地方,欢迎到 [掘金翻译计划](https://github.com/xitu/gold-miner) 对译文进行修改并 PR,也可获得相应奖励积分。文章开头的 **本文永久链接** 即为本文在 GitHub 上的 MarkDown 链接。

Expand Down