0

我从https://github.com/DuncanMC/iOS-CAAnimation-group-demo找到了下面列出的代码 。这种特殊方法允许用户使用手势识别器在核心动画序列中“进行中”时停止 UIView。点击视图时,动画停止。如图所示,此代码仅适用于动画视图。我有许多动画视图,我需要与任何视图进行交互。我想我必须设置一组视图(或图层)并循环浏览它们。这个对吗?我怎么能这样做?谢谢!

  /*
 This method gets called from a tap gesture recognizer installed on the view myContainerView.
 We get the coordinates of the tap from the gesture recognizer and use it to hit-test
 myContainerView.layer.presentationLayer to see if the user tapped on the moving image view's
(presentation) layer. The presentation layer's properties are updated as the animation runs, so hit-testing
the presentation layer lets you do tap and/or collision tests on the "in flight"   animation.
*/

- (IBAction)testViewTapped:(id)sender
{
  CALayer *tappedLayer;
  id layerDelegate;
  UITapGestureRecognizer *theTapper = (UITapGestureRecognizer *)sender;
  CGPoint touchPoint = [theTapper locationInView: myContainerView];
  if (animationInFlight)
 {
    tappedLayer = [myContainerView.layer.presentationLayer hitTest: touchPoint];
    layerDelegate = [tappedLayer delegate];

    if (((layerDelegate == imageOne && !doingMaskAnimation)) ||
      (layerDelegate == waretoLogoLarge && doingMaskAnimation))
    {
      if (myContainerView.layer.speed == 0)
        [self resumeLayer: myContainerView.layer];
      else
     {
        [self pauseLayer: myContainerView.layer];

    //Also kill all the pending label changes that we set up using performSelector:withObject:afterDelay
    [NSObject cancelPreviousPerformRequestsWithTarget: animationStepLabel];
      }
    }
  }
}
4

1 回答 1

0

哈哈。那个演示项目是我的。编写代码是为了找到被点击的层,然后使用这样一个事实,即对于支持 UIView 的层,该层的委托就是视图本身。

在代码中找到 layerDelegate 的地方,您应该确保它是KindOfClass UIView,然后使用任何合适的方法来将您的视图与您动画的视图相匹配。您可以使用 IBOutletCollection 来跟踪您正在制作动画的视图,或者手动创建一个可变数组并向其中添加视图对象、使用视图标签或任何对您的应用程序有意义的东西。

于 2013-04-04T00:45:22.877 回答