自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用 SwiftUI 實(shí)現(xiàn) 3D Scroll 效果

開(kāi)發(fā) 后端
我們預(yù)覽下今天要實(shí)現(xiàn)的 3D scroll 效果。學(xué)完本教程后,你就可以在你的 App 中把這種 3D 效果加入任何自定義的 SwiftUI 視圖。下面我們來(lái)開(kāi)始本教程的學(xué)習(xí)。

[[424241]]

我們預(yù)覽下今天要實(shí)現(xiàn)的 3D scroll 效果。學(xué)完本教程后,你就可以在你的 App 中把這種 3D 效果加入任何自定義的 SwiftUI 視圖。下面我們來(lái)開(kāi)始本教程的學(xué)習(xí)。

入門(mén)

首先,創(chuàng)建一個(gè)新的 SwiftUI 視圖。為了舉例說(shuō)明,在這個(gè)新視圖中,我會(huì)展示一個(gè)有各種顏色的矩形列表,并把新視圖命名為 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.     } 

顏色數(shù)據(jù)

在視圖的結(jié)構(gòu)體里,添加一個(gè)用于記錄顏色的變量。

  1. var colors: [Colors] 

實(shí)現(xiàn)這個(gè)列表

在 body 變量的內(nèi)部,刪除掉占位 Text。在 ScrollView 嵌套中添加一個(gè) HStack,如下:

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

展示矩形

我們使用 ForEach 在 HStack 內(nèi)部根據(jù) colors 中的數(shù)據(jù)分別創(chuàng)建不同顏色的矩形。此外,我修改了矩形的 frame,讓它看起來(lái)與傳統(tǒng) 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 結(jié)構(gòu)體中傳入如下的顏色參數(shù):

  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 中。這樣的話(huà),當(dāng) Rectangle 在屏幕上移動(dòng)的時(shí)候,我們就可以獲得其 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.     } 

根據(jù) GeometryReader 的用法要求,我們需要修改上面定義的 HStack 的 spacing 屬性。

在 Rectangle 中加入下面這行代碼。

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

當(dāng) Rectangle 在屏幕上移動(dòng)時(shí),這個(gè)方法的 Angle 參數(shù)會(huì)發(fā)生改變。請(qǐng)重點(diǎn)看 .frame(in:) 這個(gè)函數(shù),你可以獲取 Rectangle 的 CGRect 屬性 minX 變量來(lái)計(jì)算角度。

axis 參數(shù)是一個(gè)元組類(lèi)型,它定義了在使用你傳入的角度參數(shù)時(shí),哪一個(gè)坐標(biāo)軸要發(fā)生改變。在本例中,是 Y 軸。

rotation3DEffect() 方法的文檔可以在蘋(píng)果官方網(wǎng)站的 這里 找到。

下一步,把這個(gè)案例跑起來(lái)。當(dāng)矩形在屏幕上移動(dòng)時(shí),你可以看到它們?cè)谛D(zhuǎn)。

我還修改了矩形的 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.     } 

 

責(zé)任編輯:武曉燕 來(lái)源: Swift社區(qū)
相關(guān)推薦

2009-05-13 08:13:37

SUSELinux 10.3Nvidia

2023-05-26 07:08:05

CSS模糊實(shí)現(xiàn)文字

2021-08-30 06:20:39

CSS 技巧3D 效果

2010-06-09 16:21:10

OpenSUSE界面

2012-07-18 20:59:40

jQuery

2011-09-07 10:00:53

Ubuntu3D

2010-01-04 15:17:52

Ubuntu啟動(dòng)

2021-11-08 06:02:17

CSS 技巧代碼重構(gòu)

2009-04-03 08:33:59

Symbian諾基亞Photo Brows

2010-06-09 10:13:40

OpenSUSE 3D

2013-07-23 07:03:51

Android開(kāi)發(fā)學(xué)習(xí)Gallery實(shí)現(xiàn)3DAndroid源碼下載

2013-01-30 16:15:40

adobeHTML5css3

2012-06-16 16:57:52

WebGL

2012-02-27 10:00:50

HTML 5

2011-05-26 10:55:39

2023-09-01 09:30:22

Three.js3D 圖形庫(kù)

2022-09-19 19:16:42

輪播圖has

2011-08-15 14:16:12

2010-01-06 10:16:06

Ubuntu 3D

2011-10-06 13:30:45

宏碁投影儀
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)