物品之间的碰撞不起作用 [英] Collision not working between items

查看:74
本文介绍了物品之间的碰撞不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用UIDynamics.我的应用有两个正方形.一个是固定的,另一个可以移动(通过应用平移手势).当我尝试碰撞它们时,它们不会碰撞.委托方法永远不会被调用.这是我的代码,希望我能指出问题所在.

I'm using UIDynamics in my app. my app has two squares. one is fixed and the other can be moved (by applying a pan gesture). when i try to collide them they don't collide. the delegate methods never get called. here's my code and i hope someone can point out the problem.

    UIDynamicAnimator* animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
  UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.square1, self.square2]];
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.square1 addGestureRecognizer:pan];
    [collisionBehavior setCollisionMode:UICollisionBehaviorModeEverything];
    [animator addBehavior:collisionBehavior];
    collisionBehavior.collisionDelegate = self;

推荐答案

由于ARC,函数结束时,您的UIDynamicAnimator *动画师被销毁.这就是为什么您看不到任何动画的原因.将其设置为实例变量,就可以了.

Your UIDynamicAnimator* animator is destroyed when function ends due to ARC. Thats why you are not able to see any animations. Make it a instance variable and you will be fine.

这篇关于物品之间的碰撞不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆