禁用UISnapBehavior上的旋转?

时间:2013-10-02 15:51:48

标签: animation ios7 uikit uikit-dynamics

我喜欢那个UISnapBehavior代码段,但我真的想用它在一个方向上滑动,稍微摆动。

有没有办法为这种行为关闭轮播? 由于SpriteKit具有allowsRotation属性,因此可以轻松关闭。

5 个答案:

答案 0 :(得分:27)

您可以向UIDynamicItemBehavior添加UIDynamicAnimator,然后将allowsRotation属性设置为NO,这样做可以做到这一点:

UIDynamicItemBehavior * dynamicItem = [[UIDynamicItemBehavior alloc] initWithItems:@[self.viewToSnap]];
dynamicItem.allowsRotation = NO;
[self.animator addBehavior:dynamicItem];

答案 1 :(得分:10)

不需要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];

答案 2 :(得分:10)

这是一个更好的答案:UISnapBehavior有一个action属性,它接受一个在每一步都被调用的块。像这样设置这个块......

snapBehavior.action = ^{ view.transform = CGAffineTransformIdentity; };

...使旋转变为无效,没有任何其他副作用。

答案 3 :(得分:1)

我自己也在寻找相同的解决方案,但在allowsRotation上设置UIDynamicItemBehavior并没有给我带来我想要的效果。

我能够通过放置UISnapBehavior并在视图的两侧放置两个边界来阻止UICollisionBehavior的任何移动,我不想移动(在我的情况下,我配置了边界视图的左侧和右侧以防止X轴移动)。为了获得良好的弹性效果,我还将friction设置为0(使用UIDynamicItemBehavior)并调整damping上的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];

答案 4 :(得分:0)

UIView符合UIDynamicItem,它具有在进行旋转时调用的transform属性。所以重写setTransform:with nothingness ...

- (void)setTransform:(CGAffineTransform)transform
{
}

...停止旋转,但可能带有未知和不良副作用。需要一些方法来检查动态动画是否正在进行中。

在前往新地点的途中仍然会有一些与运动方向平行的弹性运动。

如果我们可以控制旋转/弹性效果的数量会很好,因为它非常卡通。