SwiftUI:使用具有可变模糊效果的RotationGesture

问题描述 投票:0回答:1

我正在尝试将RotationGesture与可变的模糊效果结合在一起。因此,如果您将目镜向右转,则可以像双筒望远镜那样使图像更清晰。向左转,它将再次模糊。下面的代码基本上是这样做的,但不是很顺利。您有任何改善效果的想法吗?

struct ContentView: View {
@State var blurring: CGFloat = 20
@State var sharping: CGFloat = 0
@State private var rotateState: Double = 0

var body: some View {

    ZStack {

        Image("cat")
            .resizable()
            .frame(width: 200, height: 200)
            .blur(radius: blurring)
            .clipShape(Circle())

        Image("ocular_rk2")
            .resizable()
            .frame(width: 350, height: 350)
            .rotationEffect(Angle(degrees: self.rotateState))
            .gesture(RotationGesture()
                .onChanged { value in
                    self.rotateState = value.degrees
                    self.changeBlur()
                }
                .onEnded { value in
                    self.rotateState = value.degrees
                    print(self.rotateState)
                }
            )

    }

}


func changeBlur() {
    switch rotateState {
    case -10000...10000: sharping = 0.1

    default:
        print("# Mistake in Switch")
    }

    if rotateState < 0 {
        blurring += sharping
    } else {
        blurring -= sharping
    }
 }

}

enter image description hereenter image description here

rotation swiftui gesture blur uiblureffect
1个回答
0
投票

检查一下(我测试了,可以正常工作)

也许您应该考虑更改某些值,因为您必须旋转一段时间直到看到文本。

顺便说一句:发布可编译的可复制代码(我们没有您的图片)总是一种好方法...

struct ContentView: View {
    @State var blurring: CGFloat = 20
    @State var sharping: CGFloat = 0
    @State private var rotateState: Double = 0

    var body: some View {

        ZStack {

            Text("cat")
           //     .resizable()
                .frame(width: 200, height: 200)
                .clipShape(Circle())
                .background(Color.yellow)

            Text("ocular_rk2")
               // .resizable()
                .blur(radius: blurring)

                .frame(width: 350, height: 350)
                .background(Color.red)
                .rotationEffect(Angle(degrees: self.rotateState))
                .gesture(RotationGesture()
                    .onChanged { value in
                        self.rotateState = value.degrees
                        self.changeBlur()
                }
                .onEnded { value in
                    self.rotateState = value.degrees
                    print(self.rotateState)
                    }
            )

        }

    }


    func changeBlur() {
        switch rotateState {
        case -10000...10000: sharping = 0.1

        default:
            print("# Mistake in Switch")
        }

        if rotateState < 0 {
            blurring = blurring + sharping
        } else {
            blurring = blurring - sharping
            print(blurring)
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.