简体   繁体   English

如何从两个不同的视图层次结构“链接”两个图层?

[英]How to 'link' two layers from two different view hierarchies?

I would like to know whether it's possible - and if so, how - to "link" or chain together two layers from two different views, so that when one of the layers moves (because it's being animated) it 'drags' the other layer in the same direction and with the same timing. 我想知道是否有可能(如果可以,如何)从两个不同的视图“链接”或链接两个图层,以便当其中一个图层移动时(因为它是动画的),它会“拖动”另一图层在相同的方向和相同的时机。

The reason I want to do this is because I have two translucent views, both of the same size and initially one on top of the other, but at some point the topmost view is animated and I need to mask off the contents of the bottom view with the 'shape' of the topmost one. 之所以要执行此操作,是因为我有两个半透明的视图,它们的大小相同,并且最初在另一个视图的顶部,但是在某些时候,最顶部的视图是动态的,因此我需要遮盖底部视图的内容与最顶部的“形状”。 I already created a layer mask for the bottom view which is the same size as the top view, but I need that layer mask to be dragged along with it when it starts moving. 我已经为底视图创建了一个与顶视图大小相同的图层蒙版,但是当它开始移动时,我需要将该图层蒙版与它一起拖动。

Best, 最好,

There is no way to listen, so to speak, for an animation to occur, however, you could setup a display link and just check/poll the layers to see if anything is animating. 可以说,没有声音可以播放动画,但是,您可以设置显示链接并检查/轮询图层以查看是否有动画。 I think it would create a lot of overhead, but it could potentially give you what you're looking for. 我认为这会产生很多开销,但有可能为您提供所需的东西。

I would probably prefer to just animate both layers at the same time. 我可能希望同时设置两个图层的动画。 I don't know what event triggers a layer to start animating in your scenario, but it's there where I would look to try to animate the second layer along with it. 我不知道是什么事件触发了一个图层开始在您的场景中设置动画,但是我希望在那里尝试与第二个图层一起设置动画。

If you want to try to go the display link route you can do this: 如果您想尝试使用显示链接路线,则可以执行以下操作:

CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self 
                                                         selector:
                                                               @selector(
                                                         displayLinkDidUpdate:)];

// Add to the run loop
[displayLink  addToRunLoop:[NSRunLoop mainRunLoop] 
                   forMode:NSDefaultRunLoopMode];

Then, implement the display update method: 然后,实现显示更新方法:

- (void)displayLinkDidUpdate:(CADisplayLink *)sender
{
  CALayer *presentationLayer = (CALayer*)[_animatingLayer presentationLayer];

  // Do something with the presentation layer

}

If the layer is animating, the presentation layer will have the current mid-flight animation values. 如果该图层正在设置动画,则表示图层将具有当前的飞行中动画值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个不同层次的两个CABasicAnimations - Two CABasicAnimations on Two Different Layers Simultaneously 如何在两个不同部分的两个不同NSMutableArray中的单元格中显示内容(两个标签和一个图像视图)。 - How to display contents(two labels and one image view) in cell from two different NSMutableArray in two different sections. CGRectIntersectsRect来自不同视图层次的两个框架 - CGRectIntersectsRect two frames from different view hierarchy 如何交换两个不同表视图的两个单元格? - How to swap two cells of two different tables view? Appium iOS-我可以为XPath中的不同层次结构使用两个@name标识符吗? - Appium iOS - Can I use two @name identifiers for different hierarchies in XPath? 在不同的层次结构中将一个视图动画化为另一个视图 - Animating One View to Another In Different Hierarchies 从两个不同的视图控制器更新表视图单元 - Update a Table View Cell from two different view controllers 如何在iOS的两个独立图层上绘制核心图形 - How to draw on two separate layers iOS, coregraphics 如何在两层或节点之间划分屏幕? - How to divide screen between two layers or nodes? 在两个现有视图层之间插入模糊层 - Insert blur layer between two existing view layers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM