Animation 在UISnapBehavior上禁用旋转?
我喜欢这个Animation 在UISnapBehavior上禁用旋转?,animation,ios7,uikit,uikit-dynamics,Animation,Ios7,Uikit,Uikit Dynamics,我喜欢这个UISnapBehavior片段,但我真的想用它只朝一个方向滑动一点 有没有办法关闭此行为的旋转? 由于SpriteKit具有可轻松关闭的AllowRotation属性。因此无需使用UIKitDynamics 只需使用: [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.65 initialSpringVeloci
UISnapBehavior
片段,但我真的想用它只朝一个方向滑动一点
有没有办法关闭此行为的旋转?
由于SpriteKit具有可轻松关闭的
AllowRotation
属性。因此无需使用UIKitDynamics
只需使用:
[UIView animateWithDuration:0.5
delay:0.0
usingSpringWithDamping:0.65
initialSpringVelocity:0.5
options:0
animations:^
{
self.transform = (self.expanded) ? self.openedTransition : self.closedTransition;
}
completion:nil];
UIView符合UIDynamicItem,它有一个在进行旋转时调用的transform属性。所以覆盖setTransform的是:虚无
- (void)setTransform:(CGAffineTransform)transform
{
}
。。。停止旋转,但可能有未知和不良副作用。需要一些方法来检查动态动画是否正在进行中
在前往新位置的途中,仍会有一些与运动方向平行的反弹运动
这将是伟大的,如果我们可以控制旋转/反弹效果的数量,因为它是相当卡通的 这里有一个更好的答案:UISnapBehavior有一个action属性,它接受一个在每一步都被调用的块。像这样设置这个块
snapBehavior.action = ^{ view.transform = CGAffineTransformIdentity; };
。。。使旋转为空而没有任何其他副作用。您可以通过向
uidynamiccanimator
添加uidynamictenbehavior
来执行此操作,然后将其allowrotation
属性设置为NO
,如下所示:
UIDynamicItemBehavior * dynamicItem = [[UIDynamicItemBehavior alloc] initWithItems:@[self.viewToSnap]];
dynamicItem.allowsRotation = NO;
[self.animator addBehavior:dynamicItem];
我自己也在寻找同样的解决方案,但在
uidynamictembehavior
上设置allowsRotation
并没有给我想要的效果
我可以通过使用UICollisionBehavior
放置并在我不想移动的视图的任一侧放置两个边界来防止UISnapBehavior
的任何移动(在我的例子中,我在视图的左侧和右侧配置了边界以防止x轴移动)。为了获得良好的反弹效果,我还将摩擦力设置为0(使用uidynamictembehavior
),并在UISnapBehavior
上调整阻尼
,以获得正确的反弹量
下面是一个对我有用的例子:
[self.animator removeAllBehaviors];
UISnapBehavior *snapBackBehavior = [[UISnapBehavior alloc] initWithItem:self.snappingView snapToPoint:self.slidePanelCenter];
snapBackBehavior.damping = 0.2;
[self.animator addBehavior:snapBackBehavior];
UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.snappingView]];
itemBehavior.friction = 0.0;
[self.animator addBehavior:itemBehavior];
CGPoint leftBoundaryStart = CGPointMake(self.snappingView.frame.origin.x, 0.0);
CGPoint leftBoundaryEnd = CGPointMake(leftBoundaryStart.x, self.view.bounds.size.height);
CGPoint rightBoundaryStart = CGPointMake(leftBoundaryStart.x + self.snappingView.frame.size.width, 0.0);
CGPoint rightBoundaryEnd = CGPointMake(rightBoundaryStart.x, leftBoundaryEnd.y);
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.snappingView]];
[collisionBehavior addBoundaryWithIdentifier:@"leftEdge" fromPoint:leftBoundaryStart toPoint:leftBoundaryEnd];
[collisionBehavior addBoundaryWithIdentifier:@"rightEdge" fromPoint:rightBoundaryStart toPoint:rightBoundaryEnd];
[self.animator addBehavior:collisionBehavior];
同意,如果您想要的只是弹簧效果,这是一种比使用UIKit Dynamics更简单的方法。同意,这是一种在UISnapBehavior
中停止旋转的技术。如果您有其他需要遵守的UIKit Dynamics行为(例如,UICollisionBehavior
),那么这是正确的答案,因为其他地方建议的animateWithDuration
将不会遵守您可能已经执行的其他行为。