gpt4 book ai didi

ios - UITapGestureRecognizer - 让它在触地时工作,而不是触地?

转载 作者:IT老高 更新时间:2023-10-28 11:24:05 30 4
gpt4 key购买 nike

我使用点击事件对时间非常敏感,所以我很好奇是否可以在用户简单地触摸时激活 UITapGestureRecognizer,而不是要求他们也触摸?

最佳答案

使用 UILongPressGestureRecognizer 并将其 minimumPressDuration 设置为 0。它会在 UIGestureRecognizerStateBegan 状态期间起到触地的作用。

对于 Swift 4+

func setupTap() {
let touchDown = UILongPressGestureRecognizer(target:self, action: #selector(didTouchDown))
touchDown.minimumPressDuration = 0
view.addGestureRecognizer(touchDown)
}

@objc func didTouchDown(gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
doSomething()
}
}

对于 Objective-C

-(void)setupLongPress
{
self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPress:)];
self.longPress.minimumPressDuration = 0;
[self.view addGestureRecognizer:self.longPress];
}

-(void)didLongPress:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan){
[self doSomething];
}
}

关于ios - UITapGestureRecognizer - 让它在触地时工作,而不是触地?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15628133/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com