我正在使用tap事件是非常时间敏感的,所以我很好奇是否有可能在用户简单触摸时激活UITapGestureRecognizer,而不是要求它们也能触摸起来?
我遇到了一个问题,即确定如何检测UIView被触及和UIView被点击.当它被触及时,我希望UIView改变它的背景颜色.当它被触摸时,我希望UIView执行某些任务.我想知道我是如何解决这个问题的.
-(void)viewDidLoad
{
UITapGestureRecognizer *dismissGestureRecognition = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDismissDoubleTap:)];
dismissGestureRecognition.numberOfTapsRequired = 1;
[sectionDismissDoubleView addGestureRecognizer:dismissGestureRecognition];
UITapGestureRecognizer *dismissGestureDownRecognition = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissGestureDownRecognition:)];
dismissGestureRecognition.numberOfTouchesRequired = 1;
[sectionDismissDoubleView addGestureRecognizer:dismissGestureDownRecognition];
}
- (void)handleDismissDoubleTap:(UIGestureRecognizer*)tap {
SettingsDismissDoubleViewController *settingsDouble = [[SettingsDismissDoubleViewController alloc] initWithNibName:@"SettingsDismissDoubleViewController" bundle:nil];
[self.navigationController pushViewController:settingsDouble animated:YES];
}
- (void)dismissGestureDownRecognition:(UIGestureRecognizer*)tap {
NSLog(@"Down");
}
Run Code Online (Sandbox Code Playgroud) 我在 SwiftUI 中有一个按钮,我希望能够对“点击按钮”(正常点击/点击)和“长按”执行不同的操作。
这在 SwiftUI 中可能吗?
这是我现在拥有的按钮的简单代码(仅处理“正常”点击/触摸案例)。
Button(action: {self.BLEinfo.startScan() }) {
Text("Scan")
} .disabled(self.BLEinfo.isScanning)
Run Code Online (Sandbox Code Playgroud)
我已经尝试添加一个“长按手势”,但它仍然只“执行”“正常/短”点击。这是我试过的代码:
Button(action: {self.BLEinfo.startScan() }) {
Text("Scan")
.fontWeight(.regular)
.font(.body)
.gesture(
LongPressGesture(minimumDuration: 2)
.onEnded { _ in
print("Pressed!")
}
)
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
杰拉德
我正在尝试实现一个自定义上下文菜单,该菜单将在用户触摸的位置长按后出现。我一直无法找到一种方法来捕获 onLongPressGesture 的触地事件的 XY 位置。
这就是我开始的地方
struct ExampleView: View {
@State var showCustomContextMenu = false
@State var longPressLocation = CGPoint.zero
var body: some View {
Rectangle()
.foregroundColor(Color.green)
.frame(width: 100.0, height: 100.0)
.onLongPressGesture {
print("OnLongPressGesture")
self.showCustomContextMenu = true
}
.overlay(
Rectangle()
.foregroundColor(Color.red)
.frame(width: 50.0, height: 50.0)
.position(longPressLocation) // <----- this is what I need to capture.
.opacity( (showCustomContextMenu) ? 1 : 0 )
)
}
}
Run Code Online (Sandbox Code Playgroud)
看了这个问题(以及答案中链接的其他问题)后,我尝试了以下操作。
如何检测没有移动或持续时间的 SwiftUI touchDown 事件?
struct ExampleView: View {
@State var showCustomContextMenu = false
@State …Run Code Online (Sandbox Code Playgroud) (对于SwiftUI,不是普通的UIKit)非常简单的示例代码,例如,在灰色背景上显示红色框:
struct ContentView : View {
@State var points:[CGPoint] = [CGPoint(x:0,y:0), CGPoint(x:50,y:50)]
var body: some View {
return ZStack {
Color.gray
.tapAction {
// TODO: add an entry to self.points of the location of the tap
}
ForEach(self.points.identified(by: \.debugDescription)) {
point in
Color.red
.frame(width:50, height:50, alignment: .center)
.offset(CGSize(width: point.x, height: point.y))
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我假设不是需要tapAction,而是需要TapGesture或其他东西?但即使在那儿,我也看不到任何方法来获取有关水龙头位置的信息。我将如何处理?
我正在尝试使用可以点击和拖动的元素来实现 ScrollView。它应该按以下方式工作:
下面的代码涵盖了所有这些要求(抽头指示器除外)。但是,我不确定它为什么起作用,具体来说,为什么我需要使用 .highPriorityGesture 并且例如不能对 Tap Gesture 和 DragGesture 进行排序.sequenced(before: ...)(这会阻止滚动)。
另外,我希望在触地事件时收到通知(不是触地,参见 2.)。我尝试使用 LongPressGesture() 而不是 TapGesture(),但这也会阻止 ScrollView 滚动,之后甚至不会触发 DragGesture。
有人知道这是如何实现的吗?或者这就是 SwiftUI 的极限?如果是这样,是否有可能移植 UIKit 的东西来实现这一点(我也已经尝试过,但没有成功,ScrollView 的内容也应该是动态的,因此移植整个 ScrollView 可能很困难)?
谢谢你的协助!
struct ContentView: View {
var body: some View {
ScrollView() {
ForEach(0..<5, id: \.self) { i in
ListElem()
.highPriorityGesture(TapGesture().onEnded({print("tapped!")}))
.frame(maxWidth: .infinity)
}
}
}
}
struct ListElem: View {
@GestureState var dragging = CGSize.zero
var body: some View {
Circle() …Run Code Online (Sandbox Code Playgroud)