簡體   English   中英

僅在x軸上的UISnapBehaviour

[英]UISnapBehaviour in x-axis only

無論如何,是否要使UISnapBevahiour僅沿線性x軸工作?

我想要UISnapBehaviour的確切行為,但不希望它“晃動”到x和y軸(僅x軸)的位置。

這是針對我的表格單元格contentView內部的UIView。

 // UIKitDynamics self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self]; CGPoint centerPoint = self.contentView.center; self.snapBehaviour = [[UISnapBehavior alloc] initWithItem:self.frontView snapToPoint:centerPoint]; [self.snapBehaviour setDamping:1.0f]; [self.animator addBehavior:self.snapBehaviour]; UIDynamicItemBehavior *itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[self.frontView]]; itemBehaviour.elasticity = 0.0f; itemBehaviour.allowsRotation = NO; [self.animator addBehavior:itemBehaviour]; 

UIDynamicBehavior文檔中可以看到它的action塊:

@property(nonatomic, copy) void (^action)(void)

動態動畫師在每個動畫步驟上調用動作塊。

因此,在創建行為時,請執行以下操作:

UIView *view = ...
UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:view attachedToAnchor:point];

CGFloat xCoordinate = view.frame.origin.x;
attachment.action = ^{
   if (view.frame.origin.x != xCoordinate) {
        CGRect frame = view.frame;
        frame.origin.x = xCoordinate;
        view.frame = frame;
    }
};

[self.dynamicAnimator addBehavior:attachment];

在我的示例中,我使用UIAttachmentBehavior,但此方法也適用於UIDynamicBehavior的任何其他子類,在您的情況下為UISnapBehavior。

我將發布用於執行類似操作的代碼,正如我在評論中提到的那樣,它使用的UIView動畫在下面利用了UIKit Dynamics,而不是直接使用它們。

[UIView animateWithDuration:0.5
                      delay:0.0
     usingSpringWithDamping:0.5
      initialSpringVelocity:0.3
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                   [toView setFrame:_modalFrame];
                 }
                 completion:^(BOOL finished) {
                   [self.context completeTransition:YES];
                 }];

這給出了模態視圖,該模態視圖在呈現時會彈出並振盪一點。

暫無
暫無

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

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