iOS - 检测UIView中的触摸? [英] iOS - Detecting touches in a UIView?

查看:146
本文介绍了iOS - 检测UIView中的触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个UIView的子类,它可以检测触摸。仅当触摸在当前视图内开始时,视图检测才会触摸。当触摸从视图外部开始并且它们在我的自定义视图中移动时,touchesMoved不会被调用。检测当前视图中尚未启动的移动触摸的任何解决方案?

So I have a Subclass of UIView that is suppose to detect touches. The view detect touches only if the touches started inside the current view. When the touches start outside of the view and they move inside my custom view touchesMoved doesn't get called. Any solution to detect moving touches that have not started in the current view?

@implementation MycustomView

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   // This only gets called if touches have started in the current View
} 

@end


推荐答案

以下解决方案有效。我有MyCustomView的多个实例;随着触摸移动我想要检测正在触摸的视图

The following solution worked. I have multiple instances of MyCustomView; as the touches move I want to detect the views that are being touched

我最终将触摸检测从MyCustomView移动到其superView,因此以下代码不再出现在MyCustomView中class:

I ended up moving touch detection from MyCustomView to its superView, so the following code is no longer in MyCustomView class:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.contentView];

    for (UIView *view in self.contentView.subviews)
    {
        if ([view isKindOfClass:[MyCustomView class]] &&
            CGRectContainsPoint(view.frame, touchLocation))
        {

        }
    }
}

这篇关于iOS - 检测UIView中的触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆