Ios 有没有一种方法可以给一个CALayer';s操作字典在其UIView委托上的永久进位?

Ios 有没有一种方法可以给一个CALayer';s操作字典在其UIView委托上的永久进位?,ios,cocoa-touch,core-animation,calayer,Ios,Cocoa Touch,Core Animation,Calayer,我有以下情况: 在视图控制器中,我将创建任意数量的透明UIButton,用于在控制器的主视图上调整UIView的外观和消失。现在,当我的“放大”和“缩小”方法被调用时,有一段代码用于修改两个CALayer属性的默认动画(仅限于此)。然后,每次操作发生时都会调用此代码。但是我想在层内设置动画代码一次,而不是在缩放方法中设置。在展示基本理念后,我将进一步解释: 按钮/传感器创建代码太长,无法发布(与实际问题无关),但与此伪代码大致相同: - (void) createSensors:[number

我有以下情况:

在视图控制器中,我将创建任意数量的透明UIButton,用于在控制器的主视图上调整UIView的外观和消失。现在,当我的“放大”和“缩小”方法被调用时,有一段代码用于修改两个CALayer属性的默认动画(仅限于此)。然后,每次操作发生时都会调用此代码。但是我想在层内设置动画代码一次,而不是在缩放方法中设置。在展示基本理念后,我将进一步解释:

按钮/传感器创建代码太长,无法发布(与实际问题无关),但与此伪代码大致相同:

- (void) createSensors:[number of sensors]
{
  for(number of sensors)
  {
    create UIVIew with some content;
    create transparent UIBUtton as sensor and place somewhere in main view;
    calculate a transform to "shrink" the UIView's layer to the size and position of the button.
    apply transform;
    set the UIVIew's layer opacity to 0;
    add UIVIew to main view 
    (but the rest of my interface is in a container view, so I can dim the screen and have this view displayed in a modal-like way.)
    do some tagging so the button's tag can later be used to retrieve this view and transform.
    }
}
现在,单击传感器时,我会对与传感器/视图关联的视图执行“放大”。实际上,我将屏幕暗显为白色,并设置视图(层的真实位置)的动画,并缩放到屏幕中心。单击视图中的一个按钮,我会做相反的操作来关闭它。代码如下:

- (void)animateZoomIn:(UIButton*)sender
{  
  NSString *sourceTagKey = [NSString stringWithFormat:@"%i",sender.tag]; 
  AnimationSettings *settings = [zoomTransforms objectForKey:sourceTagKey];

  UIView *newBox = [[self.view subviews] objectAtIndex:sender.tag]; 
  activeZoomBox = newBox;

  CABasicAnimation *a = [CABasicAnimation animation];
  a.duration = .4;
  [mainCanvas.layer addAnimation:a forKey:@"opacity"];

  a = [CABasicAnimation animation];
  a.duration = 1;
  [activeZoomBox.layer addAnimation:a forKey:@"opacity"];

  a = [CABasicAnimation animation];
  a.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  a.duration = .5;
  [activeZoomBox.layer addAnimation:a forKey:@"transform"];  

  [mainCanvas setUserInteractionEnabled:NO];

  if([settings wantsBackgroundFade])
    [mainCanvas.layer setOpacity:.2];

  [activeZoomBox.layer setOpacity:1];  
  [activeZoomBox.layer setTransform:CATransform3DIdentity];
}


- (void)animateZoomOut:(UIButton*)sender
{
  NSString *sourceTagKey = [NSString stringWithFormat:@"%i",sender.tag]; 
  AnimationSettings *settings = [zoomTransforms objectForKey:sourceTagKey];

  CABasicAnimation *a = [CABasicAnimation animation];
  a.duration = .4;
  [mainCanvas.layer addAnimation:a forKey:@"opacity"];

  a = [CABasicAnimation animation];
  a.duration = 1;
  [activeZoomBox.layer addAnimation:a forKey:@"opacity"];

  a = [CABasicAnimation animation];
  a.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  a.duration = .5;
  [activeZoomBox.layer addAnimation:a forKey:@"transform"];


  [mainCanvas setUserInteractionEnabled:YES];

  if([settings wantsBackgroundFade])
    [mainCanvas.layer setOpacity:1];

  [activeZoomBox.layer setOpacity:0];  
  [activeZoomBox.layer setTransform:[settings transform]];
}
那么,问题是:

我不希望有这种代码:

CABasicAnimation *a = [CABasicAnimation animation];
  a.duration = .4;
  [mainCanvas.layer addAnimation:a forKey:@"opacity"];
在行动方法上。我宁愿替换动画一次,并使其对创建的每个层和mainCanvas视图“永久”。例如,我更希望在控制器的init方法上使用一次此代码,然后忘记它:

CABasicAnimation *a = [CABasicAnimation animation];
    a.duration = .4;
    NSMutableDictionary *animations = [NSMutableDictionary dictionaryWithDictionary:[mainCanvas.layer actions]];
    [animations setObject:a forKey:@"opacity"];
    [mainCanvas.layer setActions:animations];
然后,无论何时调用[mainCanvas setOpacity:],默认情况下都会持续0.4秒。使用代理较少的CALayer是可行的。但在这种情况下,我的层将其视图作为代理,其优先级高于操作字典,我需要将视图作为代理


那么有没有办法让层响应字典呢?如果没有,那么“永久替换”或向这些层添加动画的最佳方式是什么,以便动画包含在层对象中(这就是为什么我不希望覆盖CAAction协议的视图委托方法)?

为什么不在UIView上覆盖
-actionForLayer:forKey:
,以便在重要键上提供所需的操作(并为其他键调用super)?无法更改优先级,因此您只能选择实现
-actionForLayer:forKey:
或使用手动动画