gpt4 book ai didi

ios - 在 UICollectionView 中禁用 UIAttachmnetBehavior 的垂直移动

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:38 24 4
gpt4 key购买 nike

我正在尝试在水平 UICollectionView 中模拟消息应用程序的 Spring 动画

我在我的 UICollectionViewFlowLayout 子类中使用了 UIAttachmentBehavior。但问题是,当我水平滚动时,单元格也会垂直和水平移动,以某种方式旋转!

enter image description here

enter image description here

我已经学习了这个教程:
implementing a bouncy uicollectionviewlayout with uikitdynamics

并在我的 collectionView 中使用。我也关注了 WWDC 2013 session 217-Exploring Scroll Views on iOS 7。但问题仍然存在!
有谁知道我应该如何解决这个问题?

-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self){
_dynamicAnimator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];
}
return self;
}

- (void)prepareLayout{

[super prepareLayout];

CGSize contentSize = [self collectionViewContentSize];
NSArray *items = [super layoutAttributesForElementsInRect:CGRectMake(0, 0, contentSize.width, 500)];

if (items.count != _dynamicAnimator.behaviors.count) {
[_dynamicAnimator removeAllBehaviors];

for (UICollectionViewLayoutAttributes *item in items) {
UIAttachmentBehavior *springBehavior = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:item.center];
springBehavior.length = 0.f;
springBehavior.damping = 0.5f;
springBehavior.frequency = 0.8f;

[_dynamicAnimator addBehavior:springBehavior];
}
}
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
return [_dynamicAnimator itemsInRect:rect];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
return [_dynamicAnimator layoutAttributesForCellAtIndexPath:indexPath];
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
CGFloat scrollDelta = newBounds.origin.x - self.collectionView.bounds.origin.x;
CGPoint touchLocation = [self.collectionView.panGestureRecognizer locationInView:self.collectionView];

for (UIAttachmentBehavior *springBehavior in _dynamicAnimator.behaviors) {
CGPoint anchorPoint = springBehavior.anchorPoint;
CGFloat touchDistance = fabsf(touchLocation.x - anchorPoint.x);
CGFloat resistanceFactor = 0.002;

UICollectionViewLayoutAttributes *attributes = springBehavior.items.firstObject;

CGPoint center = attributes.center;

float resistedScroll = scrollDelta * touchDistance * resistanceFactor;
float simpleScroll = scrollDelta;

float actualScroll = MIN(abs(simpleScroll), abs(resistedScroll));
if(simpleScroll < 0){
actualScroll *= -1;
}

center.x += actualScroll;
attributes.center = center;

[_dynamicAnimator updateItemUsingCurrentState:attributes];
}

return NO;
}

最佳答案

我在我的应用程序中遇到了同样的问题。问题是这一行: UIAttachmentBehavior *springBehavior = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:item.center];

问题是项目的中心返回带小数的 float 。 iOS 然后将它们四舍五入为一个带 0 位小数的 float ,导致 anchor 不能很好地居中,因此项目弹跳。

解决方案是在锚定之前确保您的项目框架的高度和宽度可以被二整除。然后验证 item.center 返回的 X 和 Y 值是没有小数的 float ,并且动画可以正常工作。

注意:即使在 iOS7 中工作完美,我在 iOS8 中也看到了与此类似的问题,并且此解决方案不起作用。

关于ios - 在 UICollectionView 中禁用 UIAttachmnetBehavior 的垂直移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24333371/

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