小编wil*_*lly的帖子

为什么手势开始在 SwiftUI 框架外工作?

我有一个 Rec,我可以在手势动作中移动它,但问题是手势开始在其框架外捕获手势,我希望它只是在框架内工作。这是有关问题的代码和gif:

动图:

在此处输入图片说明

代码:

    struct ContentView: View {
    
    @State private var recLocation: CGFloat = CGFloat()
    @GestureState private var recTranslation: CGFloat = CGFloat()
    
    var body: some View {
        
        GeometryReader { geometry in
            
            Rectangle().fill(Color.red).frame(width: 50, height: 50, alignment: .center)
                .position(x: recLocation + recTranslation + 25, y: geometry.size.height/2)
                .gesture( DragGesture()
                            .updating($recTranslation) { value, state, translation in
                                state = value.translation.width
                            }
                            .onEnded { value in
                                recLocation = recLocation + value.translation.width
                            } )
            
            
        }
        
    }
}
Run Code Online (Sandbox Code Playgroud)

swiftui

5
推荐指数
1
解决办法
88
查看次数

为什么我不能在扩展中使用 round?

我试图使这个customRound函数,但 Xcode 抱怨无法将类型 () 的值转换为 Double 我怎么可能解决这个问题?

extension Double {

    func customRound(zeros: Int) -> Double {
      return round(Double(zeros)*self) / Double(zeros)
    }
    
}
Run Code Online (Sandbox Code Playgroud)

更新:

extension Double {

    func customRound(digits: Int) -> Double {
        return Darwin.round(pow(10.0, Double(digits))*self) / pow(10.0, Double(digits))
    }
  
}
Run Code Online (Sandbox Code Playgroud)

function ios swift

3
推荐指数
1
解决办法
87
查看次数

标签 统计

function ×1

ios ×1

swift ×1

swiftui ×1