快速提问:我如何检测点击手势识别器是否在添加到视图的子视图内?例如.如果我点击一个对象,例如已经作为子视图添加到已添加了点击手势识别器的背景的方块,如何检测到它已被点击?
我刚刚在IB中发现了手势识别器,我想利用它们.我已经尝试过Tap Gesture Recognizer但它似乎不起作用.这就是我所做的......
将"Tap Gesture Recognizer"拖放到IB中的视图控制器中.
在.m文件中添加了以下方法...
- (IBAction)viewTapped:(UIGestureRecognizer *)sender
{
// Do some stuff
}
Run Code Online (Sandbox Code Playgroud)声明.h文件中的方法...
- (IBAction)viewTapped:(UIGestureRecognizer *)sender;
Run Code Online (Sandbox Code Playgroud)最后,我将IBAction与IB中的"Tap Gesture Recognizer"联系起来.
当我尝试运行时,我没有成功 - 我错过了什么吗?
iphone interface-builder ibaction uigesturerecognizer uitapgesturerecognizer
我在我的简单故事板IPad项目上有UIViewController,它包含放置在整个表面(1024 x 768)上的UIScrollView.我创建了3个XIB文件,这些文件是我的应用程序在viewDidLoad中启动时加载的UIViews,并将它们添加到UIScrollView中.这3个XIB文件中的每一个只包含一个UIButton.
这是层次结构:
~UIViewController(UIViewControllerClass是这个UIViewController的类)
~~ UIScrollView(包含3个相同的UIViews)
~~~ UIView(UIViewClass是此XIB文件的文件所有者)
~~~~ UIButton
我希望我的UIViewControllerClass能够识别两者:触摸UIScrollView组件上的任何位置,如果触摸了UIScrollView,如果触摸了UIScrollView中UIView内的UIButton,则可以获得完全触摸该按钮的信息.
我在UIViewClass中创建了IBAction,用于触摸UIScrollView中UIView内的UIButton,当我在所有元素(UIViewController,UIView和UIScrollView)上设置User Interaction Enabled = YES时,会调用此方法.
但此时我的UIViewControllerClass并不知道在UIButton上的UIScrollView内发生了触摸.我做了这样的触摸识别器:
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouch)];
touch.numberOfTouchesRequired = 1;
Run Code Online (Sandbox Code Playgroud)
并将其添加到UIScrollView组件.通过这种方式,我能够在UIViewControllerClass中检测UIScrollView组件上的触摸事件,但是UIView中的UIButton触摸事件处理程序不再被调用.
所以,我需要在UIViewControllerClass中有这两个信息:
我认为将触摸事件识别器附加到整个UIScrollView组件不是解决方案,因为它禁用了我在UIViewClass中编写的所有触摸事件处理程序.
我认为解决方案是,在UIScrollView中的UIView组件上进行的某些操作应该发送到UIViewControllerClass,但我没有找到一种方法来执行此操作.
如果有人能帮助我,我会非常感激.提前致谢.
[编辑#1:郑的回答]
点击手势必须将cancelsTouchesInView选项设置为NO!
对于我的上述情况,这条线解决了一切:
touch.cancelsTouchesInView = NO;
Run Code Online (Sandbox Code Playgroud)
非常感谢郑.
我试图忽略UITapGestureRecognizer在UITableView上点击以下内容:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UITableViewCellContentView class]]) {
return NO; // ignore the touch
}
return YES; // handle the touch
}
Run Code Online (Sandbox Code Playgroud)
它不会编译:"使用未声明的标识符'UITableViewCellContentView'
没有文档的课程?需要子类?更好的方法来完成这个?
谢谢你的帮助.
例如,我有一个视图,我想要两个不同的手势:
点击做动作A.双击做动作B.
问题在于UITapGestureRecognizer我只能设置最小所需的点击次数.单击手势识别器在双击手势识别器识别双击之前识别轻击.
例:
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognized:)];
_tapGestureRecognizer.numberOfTouchesRequired = 1;
_tapGestureRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:_tapGestureRecognizer];
_doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGestureRecognized:)];
_doubleTapGestureRecognizer.numberOfTouchesRequired = 1;
_doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[self addGestureRecognizer:_doubleTapGestureRecognizer];
Run Code Online (Sandbox Code Playgroud)
它总是识别单击,即使我非常快地双击.如何设置它以使轻敲手势识别器等待并看到双击手势识别器是否识别?
我们,
今天我已将iPhone更新到iOS 9,现在手机识别器出现问题.这是错误:
警告:在storyboard/xib中设置了一个手势识别器(; target = <(action = onVideoTap:,target =)>>),一次添加到多个视图( - >; layer =>),这个从未被允许过,现在已经执行.从iOS 9.0开始,它将被放入加载到的第一个视图中.
我对iOS8没有这个问题.该视图包含UIImageView和TextView.识别器被放入ImageView,并且只有这个视图的引用出口.
我真的不明白这个问题.有人能帮助我吗?谢谢 :)
我有2个视图,但我想让1个视图(虚拟)更大.如果我将tapGesture放在v1上,则点击手势可以使用更大的命中区域但是如果我将我的tapGesture放在v2上则不起作用(实际上它根本不识别tapGesture,甚至不能识别原始边界内)甚至虽然我循环我的TestView1 hittest方法,点包含在框架中.
#import "ViewController.h"
@interface TestView1 : UIView
@end
@implementation TestView1
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGFloat radius = 100.0;
CGRect frame = CGRectMake(0, 0,
self.frame.size.width + radius,
self.frame.size.height + radius);
if (CGRectContainsPoint(frame, point)) {
return self;
}
return nil;
}
@end
@interface TestView2 : UIView
@end
@implementation TestView2
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGFloat radius = 100.0;
CGRect frame = CGRectMake(0, 0,
self.frame.size.width + radius,
self.frame.size.height + radius);
if (CGRectContainsPoint(frame, point)) {
return self;
}
return nil; …Run Code Online (Sandbox Code Playgroud) 我是iOS开发的新手.我不知道是否已经问过这个问题,我尝试在stackoverflow上搜索解决方案,但没有得到任何结果.
题 :
我有一个UILabel叫myLabel文字:"点击这里继续"
现在问题是我想在用户点击"点击"时执行操作.
我知道如何使用UITapGestureRecognizer,但它对整体有所回应UILabel.是否可以只检测用户何时只点击字符串"Click"?
我怎么知道手指何时宕机以及何时摔倒UITapGestureRecognizer?
该文件说我应该只处理UIGestureRecognizerStateEnded的自来水,因此表示有UIGestureRecognizerStateBegin当手指下降,但我得到的是UIGestureRecognizerStateEnded.
我用来注册识别器的代码是:
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]
Run Code Online (Sandbox Code Playgroud) 我如何添加UITapGestureRecognizer到一个UITextView,但仍然获得了通过对这些触摸UITextView为正常?
目前,只要我向textView添加自定义手势,它就会阻止点击UITextView默认操作,例如定位光标.
var tapTerm:UITapGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
textView.addGestureRecognizer(tapTerm)
}
func tapTextView(sender:UITapGestureRecognizer) {
println("tapped term – but blocking the tap for textView :-/")
…
}
Run Code Online (Sandbox Code Playgroud)
我如何处理点击但保持像光标定位一样的textView行为?
ios ×9
objective-c ×4
iphone ×3
hittest ×1
ibaction ×1
ios9 ×1
swift ×1
touch ×1
uikit ×1
uilabel ×1
uiscrollview ×1
uitableview ×1
uitextview ×1
uiview ×1