草庐IT

objective-c - If 语句在多个 CALayer 之间进行选择

coder 2024-01-21 原文

我在 boxLayer 上有一个 Core Animation 图像,我正在复制它,改变第二个 (boxLayer2) 的 Action 并移动它的位置,以便有人可以在两个图像之间进行选择。

我希望用户能够点击 boxLayer 的图像,而 boxLayer2 图像除了 boxLayer 移动外什么都不做(除了接收触摸我没有包含我的动画代码),反之亦然。

我无法使用 if 语句。我尝试了多种变体 self.layer == boxLayer 或 CALayer == boxlayer ... sublayer 是一个数组,所以就这样了。如果我知道我遗漏了什么,任何帮助/解释都将不胜感激。

谢谢!

UIView *BounceView 在 VC 中声明

在 BounceView 中,我声明了 2 个 CALayer:boxlayer 和 boxlayer2

BounceView.m

- (id)initWithFrame:(CGRect)frame       
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];


        // Create the new layer object
        boxLayer = [[CALayer alloc] init];
        boxLayer2 = [[CALayer alloc] init];

        // Give it a size
        [boxLayer setBounds:CGRectMake(0.0, 0.0, 185.0, 85.0)];
        [boxLayer2 setBounds:CGRectMake(0.0, 0.0, 185.0, 85.0)];

        // Give it a location
        [boxLayer setPosition:CGPointMake(150.0, 140.0)];
        [boxLayer2 setPosition:CGPointMake(150.0, 540.0)];

        // Create a UIImage
        UIImage *layerImage = [UIImage imageNamed:@"error-label.png"];
        UIImage *layerImage2 = [UIImage imageNamed:@"error-label.png"];

        // Get the underlying CGImage
        CGImageRef image = [layerImage CGImage];
        CGImageRef image2 = [layerImage2 CGImage];

        // Put the CGImage on the layer
        [boxLayer setContents:(__bridge id)image];
        [boxLayer2 setContents:(__bridge id)image2];

        // Let the image resize (without changing the aspect ratio) 
        // to fill the contentRect
        [boxLayer setContentsGravity:kCAGravityResizeAspect];
        [boxLayer2 setContentsGravity:kCAGravityResizeAspect];


        // Make it a sublayer of the view's layer
        [[self layer] addSublayer:boxLayer];
        [[self layer] addSublayer:boxLayer2];

    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches
           withEvent:(UIEvent *)event
{
  if (CAlayer == boxLayer)
  {
  // do something
  }

  else
  {
  // do something else
  }
}

最佳答案

在我看来,您似乎是想知道用户在内部点击的是哪一层开始的,这就是您的问题。

如何找出被点击的图层

CALayer有一个实例方法 - (CALayer *)hitTest:(CGPoint)thePoint那个

Returns the farthest descendant of the receiver in the layer hierarchy (including itself) that contains a specified point.

所以要找出你点击的图层,你应该做类似的事情

- (void)touchesBegan:(NSSet *)touches
           withEvent:(UIEvent *)event {
    UITouch *anyTouch = [[event allTouches] anyObject];
    CGPoint pointInView = [anyTouch locationInView:self];

    // Now you can test what layer that was tapped ...
    if ([boxLayer hitTest:pointInView]) {
        // do something with boxLayer
    } 
    // the rest of your code
}

这是有效的,因为如果点在层的边界之外,hitTest 将返回 nil

关于objective-c - If 语句在多个 CALayer 之间进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11153272/

有关objective-c - If 语句在多个 CALayer 之间进行选择的更多相关文章

  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 - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  3. 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上找到一个类似的问题

  4. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

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

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

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

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

  7. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  8. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  9. 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

  10. 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

随机推荐