首页 > 解决方案 > SwiftUI 中的 UIInterpolatingMotionEffect

问题描述

我想在 SwiftUI 中使用 UIInterpolatingMotionEffect。我正在考虑使用协调器作为在 UIKit 和 SwiftUI 之间架起桥梁的一种方式,但没有成功。我想根据 iPhone 的倾斜度来改变圆角矩形的阴影量。任何想法来实现这一点。也许有可能为此获得底层的 UIVie。

import SwiftUI

extension Color {
    static let offWhite = Color(red: 225 / 255, green: 225 / 255, blue: 235 / 255)
}

struct ContentView: View {
    @State var x_tilt = 10.0
    @State var y_tilt = 10.0


    var body: some View {

        ZStack {
            Color.offWhite
            VStack {
                RoundedRectangle(cornerRadius: 25)
                .fill(Color.offWhite)
                .frame(width: 300, height: 300)
                .shadow(color: Color.black.opacity(0.2),
                        radius: 10,
                        x: CGFloat(x_tilt),
                        y: CGFloat(y_tilt))
                .shadow(color: Color.white.opacity(0.7),
                        radius: 10,
                        x: CGFloat(5 + (-0.5 * x_tilt)),
                        y: CGFloat(5 + (-0.5 * y_tilt))
                 )
                 Slider(value: $x_tilt, in: -10...30, step: 1)
                 Text("X: \(x_tilt)")
                 Slider(value: $y_tilt, in: -10...30, step: 1)
                 Text("Y: \(-0.5 * y_tilt)")
            }

         }
         .edgesIgnoringSafeArea(.all)
     }
 }



 struct ContentView_Previews: PreviewProvider {
     static var previews: some View {
         ContentView()
    }
 }

我想使用运动传感器(倾斜)来动态更改x_tilty_tilt值。我现在用两个滑块模拟这个。

标签: swiftui

解决方案


推荐阅读