ios - 如何使用 touchesBegan 和 touchesEnded 检测点击手势
问题描述
我创建了一个直接从类派生的自定义控件UIView
。现在,如果用户点击我视图的特定部分,我想执行一个操作。所以我已经覆盖了方法touchesBegan
,touchesEnded
和touchesCancelled
。问题是,touchesEnded
如果我只是点击显示屏,则永远不会调用该方法。该方法touchesCancelled
被称为 insted。touchesEnded
仅当我执行某些手势(滑动、移动……)时才会调用。
我是否需要配置我的视图以启用点击手势?
我的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touchesBegan");
self->touchDown = YES;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.value = 1.0;
} completion:nil];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (self->touchDown) {
NSLog(@"touchesEnded");
self->touchDown = NO;
[UIView animateWithDuration:0.3 animations:^{
self.value = 0.0;
} completion:nil];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
if (self->touchDown) {
NSLog(@"touchesCancelled");
self->touchDown = NO;
[UIView animateWithDuration:0.3 animations:^{
self.value = 0.5;
} completion:nil];
}
}
对于点击手势,我得到:
2018-07-17 09:55:20.994645+0200 iOS 测试[33049:2763212] touchesBegan
2018-07-17 09:55:21.092409+0200 iOS 测试[33049:2763212] touchesCancelled
解决方案
您是否尝试过设置recognizer.cancelsTouchesInView = NO;
您的视图
一个布尔值,影响在识别手势时是否将触摸传递到视图。
推荐阅读
- c# - 如何在文本框中显示 2 个值?
- javascript - 如何计算网格上所有可能的路径
- javascript - 是否存在允许用户像 chrome 标签一样打开和管理多个浏览器的应用程序?
- svg - SVG 模式缩放模式不被切断的方式
- ios - 如何将字符串中的标准颜色名称转换为 UIColor 值
- python - 定义 2 列 x 1 行子图的问题
- python - 我的 Scrapy 蜘蛛无法从下一页提取数据
- java - ImageViews 代表喜欢和不喜欢不改变颜色,也不是在 Firebase 中创建的新节点
- javascript - 我的节点包的质量分数在更新时从 99% 变为 65%
- asp.net-core - 在 SignalR.Core 中处理并发请求