簡體   English   中英

禁用UISnapBehavior上的旋轉?

[英]Disable rotation on a UISnapBehavior?

我喜歡那個UISnapBehavior片段,但是我真的想用它在一個方向上滑動, 只是略微擺動。

有沒有辦法關閉這種行為的輪換? 由於SpriteKit具有allowsRotation屬性,因此可以輕松關閉。

您可以通過添加一個做到這一點UIDynamicItemBehaviorUIDynamicAnimator ,然后設置其allowsRotation屬性NO像這樣:

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

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

這是一個更好的答案:UISnapBehavior有一個action屬性,它接受一個在每一步都被調用的塊。 像這樣設置這個塊......

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

...使旋轉變為無效,沒有任何其他副作用。

我自己也在尋找相同的解決方案,但在allowsRotation上設置UIDynamicItemBehavior並沒有給我帶來我想要的效果。

通過使用UICollisionBehavior並在我不想移動的視圖的兩側放置兩個邊界,我能夠阻止來自UISnapBehavior的任何移動(在我的情況下,我在視圖的左側和右側配置邊界以防止x-軸運動)。 為了獲得良好的彈性效果,我還將friction設置為0(使用UIDynamicItemBehavior )並調整UISnapBehavior上的damping以獲得正確的反彈量。

這是一個對我有用的例子:

[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符合UIDynamicItem,它具有在進行旋轉時調用的transform屬性。 所以重寫setTransform:虛無......

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

...停止旋轉,但可能具有未知和不良副作用。 需要一些方法來檢查動態動畫是否正在進行中。

在前往新位置的路上,仍然會有一些與運動方向平行的彈性運動。

如果我們可以控制旋轉/彈性效果的數量會很好,因為它非常卡通。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM