gpt4 book ai didi

ios - 如何识别两个 ImageView 在动画移动时的碰撞?

转载 作者:可可西里 更新时间:2023-11-01 04:23:30 25 4
gpt4 key购买 nike

我致力于在两个 View 发生碰撞时获得警报,这里我使用了两个朝不同方向移动的 ImageView ,它们将在一个点上发生碰撞。我使用基本的动画代码在各自的方向上移动这些图像。(注意:- 不仅像下图一样,但是如果它们在方面发生碰撞,我们需要显示警报)

enter image description here enter image description here

最佳答案

如果使用标准 View 动画(例如基于 block 的 animateWithDuration),您可以设置一个 CADisplayLink(有点像计时器,但每次显示时调用已更新),然后比较两个 View 的 frame 值,看看它们是否相交。不过,诀窍在于,在动画期间, View 的 frame 是“最终目的地”帧,因此如果您想要 frame动画正在进行中,你可以看看它的“表现层”。

- (void)startDisplayLink
{
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)stopDisplayLink
{
[self.displayLink invalidate];
self.displayLink = nil;
}

- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
CALayer *view1PresentationLayer = self.view1.layer.presentationLayer;
CALayer *view2PresentationLayer = self.view2.layer.presentationLayer;

if (CGRectIntersectsRect(view1PresentationLayer.frame, view2PresentationLayer.frame)) {

NSLog(@"collision"); // report the collision; show alert if you want

[self.view1.layer removeAllAnimations]; // stop the animation if you want
[self.view2.layer removeAllAnimations];

self.view1.frame = view1PresentationLayer.frame; // but make sure to reset the frame to be the current position
self.view2.frame = view2PresentationLayer.frame;

[self stopDisplayLink]; // stop the display link if we don't need it any more
}
}

在上面的方法中,我假设您想停止动画(这不仅涉及删除动画,还涉及重置最终目的地以反射(reflect)当前 View 所在的位置),但可以做任何您想做的事情。

因此,您可以开始显示链接,启动动画,显示链接选择器方法将告诉我们是否存在碰撞:

[self startDisplayLink];

[UIView animateWithDuration:1.0 animations:^{
self.view1.frame = CGRectMake(...);
self.view2.frame = CGRectMake(...);
}];

请注意,在 iOS 7 中,您可能需要查看 UIKit Dynamics,您可以在其中添加行为来驱动 View 的移动,您还可以定义一个 UICollisionBehavior 来识别何时发生碰撞。使用 UIKit Dynamics 可能超出了这个问题的范围,但你应该引用 Apple 的 WWDC 2013 视频 Getting Started with UIKit DynamicsAdvanced Techniques with UIKit Dynamics .

但是,例如,在 iOS 7 中,您可以放弃 animateWithDuration,而是使用 UIKit Dynamics 制作动画,您可以告诉两个 View 捕捉到屏幕上的特定位置,但检测它们何时碰撞:

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.view1, self.view2]];
itemBehavior.allowsRotation = NO;
itemBehavior.resistance = 10.0; // you can change this to affect the speed of the attachment behaviors
[self.animator addBehavior:itemBehavior];

UISnapBehavior *snap1 = [[UISnapBehavior alloc] initWithItem:self.view1 snapToPoint:newPoint1];
[self.animator addBehavior:snap1];

UISnapBehavior *snap2 = [[UISnapBehavior alloc] initWithItem:self.view2 snapToPoint:newPoint2];
[self.animator addBehavior:snap2];

// You can, alternatively use the following attachment behaviors, instead of the above
// snap behaviors, and you can then use resistance to control the speed with which they move
//
// UIAttachmentBehavior *attachment1 = [[UIAttachmentBehavior alloc] initWithItem:self.view1 attachedToAnchor:self.view1.center];
// [self.animator addBehavior:attachment1];
// attachment1.frequency = 1.0;
// attachment1.anchorPoint = newPoint1;
//
// UIAttachmentBehavior *attachment2 = [[UIAttachmentBehavior alloc] initWithItem:self.view2 attachedToAnchor:self.view2.center];
// [self.animator addBehavior:attachment2];
// attachment2.frequency = 1.0;
// attachment2.anchorPoint = newPoint2;

UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.view1, self.view2]];
collision.collisionDelegate = self;
[self.animator addBehavior:collision];

并且有一个 UICollisionBehaviorDelegate 方法在发生碰撞时做一些事情,例如删除行为,有效地结束动画:

- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item1 withItem:(id<UIDynamicItem>)item2 atPoint:(CGPoint)p
{
[self.animator removeAllBehaviors]; // for example, if they collide, you could remove the behaviors so they stop

// show alert if you want to
}

关于ios - 如何识别两个 ImageView 在动画移动时的碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22138752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com