- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个按钮。我想在初始触摸时更改图像。如果触摸时间少于 1 秒,我希望它执行 X。如果触摸时间长于 1 秒,我希望它执行 Y。
我不知道如何处理这个问题。事实证明 UIButton 很麻烦,所以我想我可以使用 UIGestureRecognizers
或 touchesBegin:
最初的想法是使用 UITapGestureRecognizer
来检测快速点击以执行 X,并使用 UILongTapGestureRecognizer
来处理长按以执行 Y。
问题是 UITapGestureRecognizer
没有标记 UIGestureRecognizerStateBegan
,它只会为 UIGestureRecognizerStateEnd
发送通知。
所以我决定尝试组合覆盖 touchesBegin:
和 touchesEnd:
方法并使用 UILongPressGestureRecognizer
:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// change image
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// do X
// change image to original image
}
-(IBAction)longPressDetected:(UILongPressGestureRecognizer *)recognizer {
DLog(@"fired");
if (recognizer.state == UIGestureRecognizerStateBegan) {
// Do y
// change image to original image
}
else if (recognizer.state == UIGestureRecognizerStateCancelled) {
}
else if (recognizer.state == UIGestureRecognizerStateEnded) {
}
}
如果 UILongPressGestureRecognzier
触发,它会取消初始 touchesBegan:
(不会触发 touchesEnded:
方法)。
但我遇到了 touchesBegin:
方法启动缓慢的问题。触发方法有 0.5 秒的延迟。让我感到困惑的是,如果我将 UILongPressGestureRecognizer
与 longTap.minimumPressDuration = 0
一起使用,它会立即触发。
这是在我需要的程序中。在虚拟区域中使用它,touchesBegins:
也会立即触发。
什么会导致它在程序中滞后?
是否有其他方法可以获得所需的效果?
最佳答案
也许你可以使用计时器和长按手势识别器
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeLongPressGesture:)];
longPress.minimumPressDuration = 0;
[self.button addGestureRecognizer:longPress];
...
- (void)didRecognizeLongPressGesture:(UILongPressGestureRecognizer*)gesture
{
static NSTimer *timer = nil;
if (gesture.state == UIGestureRecognizerStateBegan)
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.1 target:self selector:@selector(changeImage:) userInfo:nil repeats:NO];
//change the image here
}
else if ( gesture.state == UIGestureRecognizerStateEnded)
{
if ([timer isValid])
{
//the user has pressed the button less than a second
[timer invalidate];
timer = nil;
}
}
}
- (void)changeImage:(NSTimer*)timer
{
[timer invalidate];
timer = nil;
//change to the other image here
}
关于ios - 使用 touchesBegins 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18795810/
我有一个按钮。我想在初始触摸时更改图像。如果触摸时间少于 1 秒,我希望它执行 X。如果触摸时间长于 1 秒,我希望它执行 Y。 我不知道如何处理这个问题。事实证明 UIButton 很麻烦,所以我想
我正在尝试在 ios 中进行拖动。如果拖动移动的距离很短,我将通过比较起始 x、y(从 touchesBegan 开始)与 touchesEnd 中的 x、y 来将拖动计为单击事件。似乎 touche
因此,当您按下并拖动时,我的应用程序运行良好。我还在 Interface Builder 中将 UIButtons 设置为 Touch Down。 同样,当您拖动时,您需要从 UIButton 的外部
我在屏幕上有多个可通过触摸拖动的图像,目前我正在使用 touchesBegin:、touchesMoved: 和 touchesEnded: 来使这些图像可移动。我的目标是让用户能够通过拖动在屏幕上选
更新[11/1/2018]: 我尝试一步步测试代码,发现: 从未为剩余的 UIView 调用方法touchesEnded。 touchesCancelled 方法也是如此 我正在关注有关实现多点触控应
我是一名优秀的程序员,十分优秀!