- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我致力于在两个 View 发生碰撞时获得警报,这里我使用了两个朝不同方向移动的 ImageView ,它们将在一个点上发生碰撞。我使用基本的动画代码在各自的方向上移动这些图像。(注意:- 不仅像下图一样,但是如果它们在方面发生碰撞,我们需要显示警报)
最佳答案
如果使用标准 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 Dynamics和 Advanced 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/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!