gpt4 book ai didi

ios - 限制 UICollectionView 内 UIAttachmentBehavior 的垂直移动

转载 作者:IT王子 更新时间:2023-10-29 08:16:19 26 4
gpt4 key购买 nike

我有一个水平的 UICollectionView 和一个自定义的 UICollectionViewFlowLayout ,它在每个单元格上设置了一个 UIAttachmentBehavior 以在向左滚动时给它一个有弹性的感觉和权利。该行为具有以下属性:

attachmentBehavior.length = 1.0f;
attachmentBehavior.damping = 0.5f;
attachmentBehavior.frequency = 1.9f;

当一个新的单元格被添加到 Collection View 时,它被添加到底部,然后也使用 UIAttachmentBehavior 动画到它的位置。它自然会上下弹跳一点,直到它停在原位。到目前为止,一切都按预期工作。

enter image description here

当 Collection View 在新添加的单元格停止之前向左或向右滚动时,我的问题开始出现。将左右弹跳添加到单元格已添加的上下弹跳。这导致细胞中出现非常奇怪的圆周运动。

enter image description here

我的问题是,是否可以在滚动 Collection View 时停止 UIAttachmentBehavior 的垂直运动?我尝试了不同的方法,例如使用多种附件行为并在 Collection View 中禁用滚动,直到新添加的单元格停止,但似乎没有一种方法可以阻止这种情况。

最佳答案

解决此问题的一种方法是使用附件行为的继承 .action 属性。

您需要先设置几个变量,例如(从内存中提取,未经测试的代码):

BOOL limitVerticalMovement = TRUE;
CGFloat staticCenterY = CGRectGetHeight(self.collectionView.frame) / 2;

将这些设置为自定义 UICollectionViewFlowLayout 的属性

当您创建依恋行为时:

UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:center];
attachment.damping = 1.0f;
attachment.frequency = 1.5f;
attachment.action = ^{
if (!limitVerticalMovement) return;

CGPoint center = item.center;
center.y = staticCenterY;
item.center = center;
};

然后可以通过适当设置limitVerticalMovement来开启和关闭限位功能。

关于ios - 限制 UICollectionView 内 UIAttachmentBehavior 的垂直移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23478070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com