[英]UIKit Dynamics doesn't allow me to animate a UIView's centre
I have a UIView
which is attached to a UICollisionBehavior
. 我有一个
UIView
附加到UICollisionBehavior
。 I'm trying to animate its center when a new location is tapped on the screen, but it appears using this block of code is not the way to do it: 当尝试在屏幕上轻按一个新位置时,我正在尝试设置其中心的动画,但是使用此代码块似乎无法做到这一点:
[UIView animateWithDuration:0.7f
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
animations:^{
[self.mainCharacter setCenter:location];
}
completion:nil];
Can anyone suggest a more UIKitDynamics friendly way of doing this? 谁能建议一种更UIKitDynamics友好的方式来做到这一点?
Thanks. 谢谢。
UPDATE UPDATE
So the first part of my question was similar to the one posted and a possible duplicate, however, although technically it solved the issue in the first part of this question, it does seem to interfere with how collision testing in UICollisionBehavior
. 因此,我的问题的第一部分类似于发布的问题,并且可能是重复的,但是,尽管从技术上讲,它解决了该问题的第一部分中的问题,但似乎确实干扰了
UICollisionBehavior
碰撞测试。
The second part of this question was to ask if there was a better way of doing this. 这个问题的第二部分是询问是否有更好的方法可以做到这一点。 I assumed it would be possible to accelerate a
UIDynamicItem
at a speed over a given time using UIPushBehavior
but it seems to be constant. 我以为这将有可能加快
UIDynamicItem
在使用在给定时间的速度UIPushBehavior
,但它似乎是不变的。
Can anyone recommend how to use a UIPushBehavior
to do the above? 任何人都可以推荐如何使用
UIPushBehavior
来完成上述操作吗?
In UIKit Dynamics, when you want to animate changing the center, you'd add a UISnapBehavior
to your UIDynamicAnimator
(removing any prior snap behavior first). 在UIKit Dynamics中,当您要动画化更改中心时,可以将
UISnapBehavior
添加到UIDynamicAnimator
(首先删除所有先前的捕捉行为)。 For example, create a property for the UISnapBehavior
, and then your tap gesture recognizer can snap it to that point (honoring your UICollisionBehavior
), like so: 例如,为
UISnapBehavior
创建一个属性,然后您的轻UISnapBehavior
手势识别器可以将其捕捉到该点(以纪念UICollisionBehavior
),如下所示:
- (void)handleTap:(UITapGestureRecognizer *)gesture
{
if (self.snap)
[self.animator removeBehavior:self.snap];
CGPoint point = [gesture locationInView:gesture.view];
UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.viewToAnimate snapToPoint:point];
[self.animator addBehavior:snap];
self.snap = snap;
}
You can control the "bounciness" by adjusting the damping
property of the UISnapBehavior
. 您可以通过调整
UISnapBehavior
的damping
属性来控制“ UISnapBehavior
。 If you don't like it rotating the view as it's snapping, you can also add a UIDynamicItemBehavior
with allowsRotation
set to NO
. 如果您不喜欢它在捕捉时旋转视图,还可以添加一个
UIDynamicItemBehavior
allowsRotation
设置为NO
allowsRotation
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.