简体   繁体   English

禁用UISnapBehavior上的旋转?

[英]Disable rotation on a UISnapBehavior?

I like that UISnapBehavior snippet, but I really want to use it to slide in one direction only with a slight oscillation. 我喜欢那个UISnapBehavior片段,但是我真的想用它在一个方向上滑动, 只是略微摆动。

Is there a way to turn off rotation for this behavior? 有没有办法关闭这种行为的轮换? As SpriteKit has allowsRotation property which can be easily turned off. 由于SpriteKit具有allowsRotation属性,因此可以轻松关闭。

You can do this by adding a UIDynamicItemBehavior to your UIDynamicAnimator and then setting its allowsRotation property to NO like so: 您可以通过添加一个做到这一点UIDynamicItemBehaviorUIDynamicAnimator ,然后设置其allowsRotation属性NO像这样:

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

No need for UIKitDynamics for this. 这不需要UIKitDynamics

Just simply had to use: 只需简单地使用:

[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];

Here's a better answer: A UISnapBehavior has an action property, which takes a block which gets called at every step. 这是一个更好的答案:UISnapBehavior有一个action属性,它接受一个在每一步都被调用的块。 Setting this block like this... 像这样设置这个块......

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

... causes the rotation to be nulled without any other side effects. ...使旋转变为无效,没有任何其他副作用。

I was looking for the same solution myself, but setting allowsRotation on a UIDynamicItemBehavior didn't quite give me the effect I was looking for. 我自己也在寻找相同的解决方案,但在allowsRotation上设置UIDynamicItemBehavior并没有给我带来我想要的效果。

I was able to prevent any movement from UISnapBehavior by placing using UICollisionBehavior and placing two boundaries on either side of the view I didn't want to move (in my case, I configured boundaries on left and right side of the view to prevent x-axis movement). 通过使用UICollisionBehavior并在我不想移动的视图的两侧放置两个边界,我能够阻止来自UISnapBehavior的任何移动(在我的情况下,我在视图的左侧和右侧配置边界以防止x-轴运动)。 To get a nice bouncy effect I also set friction to 0 (using UIDynamicItemBehavior ) and adjusted the damping on UISnapBehavior to get the right amount of bounce. 为了获得良好的弹性效果,我还将friction设置为0(使用UIDynamicItemBehavior )并调整UISnapBehavior上的damping以获得正确的反弹量。

Here's an example of what worked for me: 这是一个对我有用的例子:

[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];

UIView conforms to UIDynamicItem, which has a transform property that gets called when a rotation is made. UIView符合UIDynamicItem,它具有在进行旋转时调用的transform属性。 So overriding setTransform: with nothingness... 所以重写setTransform:虚无......

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

... stops the rotation, but probably with unknown and undesirable side effects. ...停止旋转,但可能具有未知和不良副作用。 Need some way to check if the dynamic animation is in progress. 需要一些方法来检查动态动画是否正在进行中。

There will still be some bouncy motion parallel to the direction of movement on the way to the new location. 在前往新位置的路上,仍然会有一些与运动方向平行的弹性运动。

It would be great if we could control the amount of the rotation/bouncy effect as it's quite cartoonish as is. 如果我们可以控制旋转/弹性效果的数量会很好,因为它非常卡通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM