草庐IT

objective-c - 在一个 Action 中观察多个 View 上的触摸

coder 2024-01-21 原文

我有一个包含 3 个独立 subview 的父 View 。 subview 在父 View 中展开,没有重叠(并且中间有一些空间)。当用户在屏幕上移动她的手指(没有抬起它)时,我想在他们进入和退出每个 subview 时跟踪触摸。

示例:如果用户开始触摸屏幕上 subview 之外的某处,然后将她的手指滑过 subview 1,离开 subview 1,再滑过 subview 2,然后松手,我会期望触发这些事件:

  1. 触摸开始
  2. 触摸输入的 child 1
  3. 触摸退出的 child 1
  4. 触摸输入的 child 2
  5. 触摸结束

似乎 touchesBegan:withEvent: 和 touchesEnded:withEvent: 方法在这种情况下会有所帮助,但是当我在 subview Controller 上定义它们时,它们并没有完全按照我的意愿进行——如果用户开始在 subview 外部触摸,然后在 subview 上滑动, subview 本身不会触发任何触摸事件。

当前的解决方案:我目前使用的解决方案感觉真的很难实现。我正在观察父级上的 touchesBegan:withEvent:、touchesEnded:withEvent: 和 touchesMoved:withEvent:,获取每个事件的坐标,并确定它们是否位于子级的范围内。如果他们这样做,我会触发相应的事件,如上所述。

这种方法大部分都有效,但感觉效率很低。感觉框架应该为我处理这项工作。我的状态管理代码有时也会错过“进入”或“退出”触发器,我怀疑这是因为触摸事件要么被丢弃,要么以意外的顺序出现。我在这里错过了更好的方法吗?

最佳答案

最简单的解决方案是这样的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];

    // Do any additional setup after loading the view.
}

- (void)pan:(UIPanGestureRecognizer *)sender
{
    static NSInteger startViewIndex;
    static NSInteger endViewIndex;
    CGPoint location = [sender locationInView:self.view];

    if (sender.state == UIGestureRecognizerStateBegan)
    {
        if (CGRectContainsPoint(self.view0.frame, location))
            startViewIndex = 0;
        else if (CGRectContainsPoint(self.view1.frame, location))
            startViewIndex = 1;
        else if (CGRectContainsPoint(self.view2.frame, location))
            startViewIndex = 2;
        else 
            startViewIndex = -1;
    }
    else if (sender.state == UIGestureRecognizerStateEnded)
    {
        if (CGRectContainsPoint(self.view0.frame, location))
            endViewIndex = 0;
        else if (CGRectContainsPoint(self.view1.frame, location))
            endViewIndex = 1;
        else if (CGRectContainsPoint(self.view2.frame, location))
            endViewIndex = 2;
        else 
            endViewIndex = -1;

        if (startViewIndex != -1 && endViewIndex != -1 && startViewIndex != endViewIndex)
        {
            // successfully moved between subviews!
            NSLog(@"Moved from %1d to %1d", startViewIndex, endViewIndex);
        }
    }
}

也许更优雅的方法是定义您自己的自定义手势识别器(这样,如果您不从您的 subview 之一拖动,它将失败,这将允许您可能在其他地方进行的其他手势识别器工作。 .. 可能不是问题,除非你使用多个手势识别器;它还将手势逻辑的细节与 View Controller 的其余部分隔离开来):

@interface PanBetweenSubviewsGestureRecognizer : UIPanGestureRecognizer
{
    NSMutableArray *_arrayOfFrames;
}

@property NSInteger startingIndex;
@property NSInteger endingIndex;

@end

@implementation PanBetweenSubviewsGestureRecognizer

@synthesize startingIndex = _startingIndex;
@synthesize endingIndex   = _endingIndex;

- (void)dealloc
{
    _arrayOfFrames = nil;
}

- (id)initWithTarget:(id)target action:(SEL)action
{
    self = [super initWithTarget:target action:action];
    if (self)
    {
        _arrayOfFrames = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)addSubviewToArrayOfFrames:(UIView *)view
{
    [_arrayOfFrames addObject:[NSValue valueWithCGRect:view.frame]];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];

    for (NSInteger i = 0; i < [_arrayOfFrames count]; i++)
    {
        if (CGRectContainsPoint([[_arrayOfFrames objectAtIndex:i] CGRectValue], location)) 
        {
            self.startingIndex = i;
            return;
        }
    }

    self.startingIndex = -1;
    self.endingIndex = -1;
    self.state = UIGestureRecognizerStateCancelled;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];

    for (NSInteger i = 0; i < [_arrayOfFrames count]; i++)
    {
        if (CGRectContainsPoint([[_arrayOfFrames objectAtIndex:i] CGRectValue], location)) 
        {
            self.endingIndex = i;
            return;
        }
    }

    self.endingIndex = -1;
    self.state = UIGestureRecognizerStateCancelled;
}

@end

然后您可以按如下方式使用:

- (void)viewDidLoad
{
    [super viewDidLoad];

    PanBetweenSubviewsGestureRecognizer *pan = [[PanBetweenSubviewsGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [pan addSubviewToArrayOfFrames:self.view0];
    [pan addSubviewToArrayOfFrames:self.view1];
    [pan addSubviewToArrayOfFrames:self.view2];

    [self.view addGestureRecognizer:pan];

    // Do any additional setup after loading the view.
}

- (void)pan:(PanBetweenSubviewsGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded && sender.startingIndex >= 0 && sender.endingIndex >= 0 && sender.startingIndex != sender.endingIndex)
    {
        // successfully moved between subviews!
        NSLog(@"Moved from %1d to %1d", sender.startingIndex, sender.endingIndex);
    }
}

关于objective-c - 在一个 Action 中观察多个 View 上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11443213/

有关objective-c - 在一个 Action 中观察多个 View 上的触摸的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  7. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  8. ruby-on-rails - rails : How to make a form post to another controller action - 2

    我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak

  9. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  10. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

随机推荐