I've tried to drag the circle shape from one place to the other. Which is easy in SwiftUI with the below example.
But the same example, I've tried to use in UIKit using UIHostingController. It is dragging for once, but re-dragging the same again is not working. Seems like it is losing touch area in UIKit. I'm running out of ideas to support UIKit with the same SwiftUI code I've done.
struct MovingCircle: View {
@State private var dragged = CGSize.zero
@State private var accumulated = CGSize.zero
var body: some View {
Circle()
.frame(width: 20, height: 20).border(Color.red)
.offset(x: self.dragged.width, y: self.dragged.height)
.gesture(DragGesture()
.onChanged{ value in
self.dragged = CGSize(width: value.translation.width + self.accumulated.width, height: value.translation.height + self.accumulated.height)
}
.onEnded{ value in
self.dragged = CGSize(width: value.translation.width + self.accumulated.width, height: value.translation.height + self.accumulated.height)
self.accumulated = self.dragged
}
)
}
}
Usage:
let swiftUIView = UIHostingController(rootView: MovingCircle())
swiftUIView.view.translatesAutoresizingMaskIntoConstraints = false
/// Add as a child of the current view controller.
addChild(swiftUIView)
/// Add the SwiftUI view to the view controller view hierarchy.
view.addSubview(swiftUIView.view)
/// Notify the hosting controller that it has been moved to the current view controller.
swiftUIView.didMove(toParent: self)
swiftUIView.view.addLeft(isSuperView: self.view, constant: 100)
swiftUIView.view.addTop(isSuperView: self.view, constant: 100)