用 SwiftUI 实现 3D Scroll 效果

开发 后端
我们预览下今天要实现的 3D scroll 效果。学完本教程后,你就可以在你的 App 中把这种 3D 效果加入任何自定义的 SwiftUI 视图。下面我们来开始本教程的学习。

[[424241]]

我们预览下今天要实现的 3D scroll 效果。学完本教程后,你就可以在你的 App 中把这种 3D 效果加入任何自定义的 SwiftUI 视图。下面我们来开始本教程的学习。

入门

首先,创建一个新的 SwiftUI 视图。为了举例说明,在这个新视图中,我会展示一个有各种颜色的矩形列表,并把新视图命名为 ColorList。

  1. import SwiftUI 
  2.  
  3. struct ColorList: View { 
  4.     var body: some View { 
  5.         Text("Hello, World!"
  6.     } 
  7.  
  8. struct ColorList_Previews: PreviewProvider { 
  9.     static var previews: some View { 
  10.         ColorList() 
  11.     } 

颜色数据

在视图的结构体里,添加一个用于记录颜色的变量。

  1. var colors: [Colors] 

实现这个列表

在 body 变量的内部,删除掉占位 Text。在 ScrollView 嵌套中添加一个 HStack,如下:

  1. var body: some View { 
  2.     ScrollView(.horizontal, showsIndicators: false) { 
  3.         HStack(alignment: .center, spacing: 50) { 
  4.  
  5.         } 
  6.     } 

展示矩形

我们使用 ForEach 在 HStack 内部根据 colors 中的数据分别创建不同颜色的矩形。此外,我修改了矩形的 frame,让它看起来与传统 UI 布局更像一些。

  1. var body: some View { 
  2.     ScrollView(.horizontal, showsIndicators: false) { 
  3.         HStack(alignment: .center, spacing: 20) { 
  4.             ForEach(colors, id: \.self) { color in 
  5.                 Rectangle() 
  6.                     .foregroundColor(color) 
  7.                     .frame(width: 200, height: 300, alignment: .center) 
  8.             } 
  9.         } 
  10.     } 

在 Preview 结构体中传入如下的颜色参数:

  1. struct ColorList_Previews: PreviewProvider { 
  2.     static var previews: some View { 
  3.         ColorList(colors: [.blue, .green, .orange, .red, .gray, .pink, .yellow]) 
  4.     } 

你可以看到下图中的效果:

增加 3D 效果

首先,把 Rectangle 嵌套在 GeometryReader 中。这样的话,当 Rectangle 在屏幕上移动的时候,我们就可以获得其 frame 的引用。

  1. var body: some View { 
  2.     ScrollView(.horizontal, showsIndicators: false) { 
  3.         HStack(alignment: .center, spacing: 230) { 
  4.             ForEach(colors, id: \.self) { color in 
  5.                 GeometryReader { geometry in 
  6.                     Rectangle() 
  7.                         .foregroundColor(color) 
  8.                         .frame(width: 200, height: 300, alignment: .center) 
  9.                 } 
  10.             } 
  11.         } 
  12.     } 

根据 GeometryReader 的用法要求,我们需要修改上面定义的 HStack 的 spacing 属性。

在 Rectangle 中加入下面这行代码。

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

当 Rectangle 在屏幕上移动时,这个方法的 Angle 参数会发生改变。请重点看 .frame(in:) 这个函数,你可以获取 Rectangle 的 CGRect 属性 minX 变量来计算角度。

axis 参数是一个元组类型,它定义了在使用你传入的角度参数时,哪一个坐标轴要发生改变。在本例中,是 Y 轴。

rotation3DEffect() 方法的文档可以在苹果官方网站的 这里 找到。

下一步,把这个案例跑起来。当矩形在屏幕上移动时,你可以看到它们在旋转。

我还修改了矩形的 cornerRadius 属性,并加上了投影效果,让它更美观。

Pretty cool right!

最终效果

  1. struct ColorList: View { 
  2.      
  3.     var colors:[Color] 
  4.      
  5.     var body: some View { 
  6.         ScrollView(.horizontal, showsIndicators: false) { 
  7.             HStack(alignment: .center, spacing: 230) { 
  8.                 ForEach(colors, id: \.self) { color in 
  9.                     GeometryReader { geometry in 
  10.                         Rectangle() 
  11.                             .foregroundColor(color) 
  12.                             .frame(width: 200, height: 300, alignment: .center) 
  13.                             .cornerRadius(16) 
  14.                             .shadow(color: Color.black.opacity(0.2), radius: 20, x: 0, y: 0) 
  15.                             .rotation3DEffect(Angle(degrees: (Double(geometry.frame(in: .global).minX) - 210) / -20), axis: (x: 0, y: 1.0, z: 0)) 
  16.                     } 
  17.                 } 
  18.             }.padding(.horizontal, 210) 
  19.         } 
  20.     } 

 

责任编辑:武晓燕 来源: Swift社区
相关推荐

2009-05-13 08:13:37

SUSELinux 10.3Nvidia

2023-05-26 07:08:05

CSS模糊实现文字

2021-08-30 06:20:39

CSS 技巧3D 效果

2010-06-09 16:21:10

OpenSUSE界面

2011-09-07 10:00:53

Ubuntu3D

2012-07-18 20:59:40

jQuery

2010-01-04 15:17:52

Ubuntu启动

2009-04-03 08:33:59

Symbian诺基亚Photo Brows

2010-06-09 10:13:40

OpenSUSE 3D

2021-11-08 06:02:17

CSS 技巧代码重构

2013-07-23 07:03:51

Android开发学习Gallery实现3DAndroid源码下载

2013-01-30 16:15:40

adobeHTML5css3

2012-02-27 10:00:50

HTML 5

2012-06-16 16:57:52

WebGL

2011-05-26 10:55:39

2023-09-01 09:30:22

Three.js3D 图形库

2010-01-06 10:16:06

Ubuntu 3D

2011-08-15 14:16:12

2022-09-19 19:16:42

轮播图has

2012-11-26 12:51:44

木材3D打
点赞
收藏

51CTO技术栈公众号