JMW*_*ker 2 ios uisnapbehavior uikit-dynamics
有没有办法让 UISnapBevahiour 只沿着线性 x 轴工作?
我想要 UISnapBehaviour 的确切行为,但不希望它在 x 和 y 轴上“摇动”到位置,而只是在 x 轴上摇动。
这是针对表格单元格的 contentView 内的 UIView 的。
Run Code Online (Sandbox Code Playgroud)// 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];
Run Code Online (Sandbox Code Playgroud)
在我的示例中,我使用 UIAttachmentBehavior,但此方法也适用于 UIDynamicBehavior 的任何其他子类,在您的情况下为 UISnapBehavior。
| 归档时间: |
|
| 查看次数: |
1038 次 |
| 最近记录: |