ios – UIDynamicAnimator自定义UICollectionViewLayout导致永久循环运动

我一直在复制2013 WWDC Session 217“探索iOS 7上的滚动视图”.我正在使用 Xcode 7 beta 2,而我的项目只有iOS 9.

我试图用UICollectionViewLayout使用一个UIDynamicAnimator,类似于会话217中提出的模拟Messages.app的感觉.我的UICollectionViewLayout是一个自定义的,由于某种原因,我的单元格似乎在我的项目中以循环运动反弹.

这是一个更加清晰的video的问题.

这是我的自定义布局代码.

// Didn't write this code myself,but should be pretty simple to follow. @Goles  


#import "VVSpringCollectionViewFlowLayout.h"  

@interface VVSpringCollectionViewFlowLayout()  
@property (nonatomic,strong) UIDynamicAnimator *animator;  
@end  

@implementation VVSpringCollectionViewFlowLayout  

-(id)init {  
    if (self = [super init]) {  
        _springdamping = 0.5;  
        _springFrequency = 0.8;  
        _resistanceFactor = 500;  
    }  
    return self;  
}  

- (id)initWithCoder:(nonnull NSCoder *)aDecoder {  
  self = [super initWithCoder:aDecoder];  
  if (self) {  
        _springdamping = 0.5;  
        _springFrequency = 0.8;  
        _resistanceFactor = 500;  
  }  
  return self;  
}  

-(void)prepareLayout {  
    [super prepareLayout];  

    if (!_animator) {  
        _animator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];  
        CGSize contentSize = [self collectionViewContentSize];  
        NSArray *items = [super layoutAttributesForElementsInRect:CGRectMake(0,contentSize.width,contentSize.height)];  

        for (UICollectionViewLayoutAttributes *item in items) {  
            UIAttachmentBehavior *spring = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:item.center];  

            spring.length = 0;  
            spring.damping = self.springdamping;  
            spring.frequency = self.springFrequency;  

            [_animator addBehavior:spring];  
        }  
    }  
}  

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

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

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

    for (UIAttachmentBehavior *spring in _animator.behaviors) {  
       CGPoint anchorPoint = spring.anchorPoint;  
       CGFloat distanceFromTouch = fabs(touchLocation.y - anchorPoint.y);  
       CGFloat scrollResistance = distanceFromTouch / self.resistanceFactor;  

       id<UIDynamicItem> item = [spring.items firstObject];  
       CGPoint center = item.center;  

       if (scrollDelta > 0) {  
            center.y += MIN(scrollDelta,scrollDelta * scrollResistance);  
       }  

       item.center = center;  
       [_animator updateItemUsingCurrentState:item];  
    }  
    return NO;  
}  

@end

这可能导致这个循环议案呢?我只是改变我的UIAttachmentAttributes中心的Y轴属性.

center.y += MIN(scrollDelta,scrollDelta * scrollResistance);

在这里缺少什么? (在其他项目中尝试这种精确的布局,似乎工作).

编辑:

上传一个示例项目(删除),自定义集合视图布局类被称为VVSpringCollectionViewFlowLayout.m,没有太多的时间来看这个太多了,因为我最近在工作上做了很多工作.

当示例项目运行(Xcode 7 beta或更高版本)时,系统将提示您使用滑块,向右拖动可视化集合视图单元格.

解决方法

以下代码可以帮助您/指向正确的方向.它还有一些额外的东西 – 像清理不必要的动画师行为,如果不在视线中,跟踪触摸这样的动画师的行为.剥离了一个旧的项目,所以应该只是工作.包含演示创建视频的Github示例项目 – https://github.com/serendipityapps/SpringyCollectionView
@interface VVSpringCollectionViewFlowLayout ()

@property (nonatomic,strong) UIDynamicAnimator *dynamicAnimator;
@property (nonatomic,strong) NSMutableSet *visibleIndexPathsSet;
@property (nonatomic,assign) CGFloat latestDelta;

@end

@implementation VVSpringCollectionViewFlowLayout

- (id)init {
    if (self = [super init]) {
        self.dynamicAnimator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];
        self.visibleIndexPathsSet = [NSMutableSet set];
    }
    return self;
}

- (id)initWithCoder:(nonnull NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.dynamicAnimator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];
        self.visibleIndexPathsSet = [NSMutableSet set];
    }
    return self;
}

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

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

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(Nsstring *)kind atIndexPath:(NSIndexPath *)indexPath {
    return [self.dynamicAnimator layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
}


-(void)prepareLayout {
    [super prepareLayout];

    // Need to enlarge visible rect slightly to avoid flickering.
    CGRect visibleRect = CGRectInset((CGRect){.origin = self.collectionView.bounds.origin,.size = self.collectionView.frame.size},-100,-100);

    NSArray *itemsInVisibleRectArray = [super layoutAttributesForElementsInRect:visibleRect];

    NSArray *cells = [itemsInVisibleRectArray filteredArrayUsingPredicate:[nspredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *item,NSDictionary *bindings) {
        return !item.representedElementKind;
    }]];

    NSSet *itemsIndexPathsInVisibleRectSet = [NSSet setWithArray:[cells valueForKey:@"indexPath"]];

    // Remove any behavIoUrs that are no longer visible.
    NSArray *noLongerVisibleBehavIoUrsCells = [self.dynamicAnimator.behaviors filteredArrayUsingPredicate:[nspredicate predicateWithBlock:^BOOL(UIAttachmentBehavior *behavIoUr,NSDictionary *bindings) {

        UICollectionViewLayoutAttributes *item= (UICollectionViewLayoutAttributes*)[[behavIoUr items] firstObject];
        if (!item.representedElementKind) {
            BOOL currentlyVisible = [itemsIndexPathsInVisibleRectSet member:[item indexPath]] != nil;
            return !currentlyVisible;
        }
        else {
            return NO;
        }
    }]];

    [noLongerVisibleBehavIoUrsCells enumerateObjectsUsingBlock:^(UIAttachmentBehavior *behavIoUr,NSUInteger index,BOOL *stop) {
        UICollectionViewLayoutAttributes *item = (UICollectionViewLayoutAttributes*)[[behavIoUr items] firstObject];
        [self.dynamicAnimator removeBehavior:behavIoUr];
        [self.visibleIndexPathsSet removeObject:[item indexPath]];
    }];


    // Add any newly visible behavIoUrs.
    CGPoint touchLocation = [self.collectionView.panGestureRecognizer locationInView:self.collectionView];

    // A "newly visible" item is one that is in the itemsInVisibleRect(Set|Array) but not in the visibleIndexPathsSet
    NSArray *newlyVisibleItems = [cells filteredArrayUsingPredicate:[nspredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *item,NSDictionary *bindings) {
        BOOL currentlyVisible = [self.visibleIndexPathsSet member:item.indexPath] != nil;
        return !currentlyVisible;
    }]];

    [newlyVisibleItems enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes *item,NSUInteger idx,BOOL *stop) {
        CGPoint center = item.center;
        UIAttachmentBehavior *springBehavIoUr = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:center];

        springBehavIoUr.length = 0.0f;
        springBehavIoUr.damping = 0.8f;
        springBehavIoUr.frequency = 1.0f;

        // If our touchLocation is not (0,0),we'll need to adjust our item's center "in flight"
        if (!CGPointEqualToPoint(CGPointZero,touchLocation)) {
            CGFloat ydistanceFromTouch = fabs(touchLocation.y - springBehavIoUr.anchorPoint.y);
            CGFloat xdistanceFromTouch = fabs(touchLocation.x - springBehavIoUr.anchorPoint.x);
            CGFloat scrollResistance = (ydistanceFromTouch + xdistanceFromTouch) / 1500.0f;

            if (self.latestDelta < 0) {
                center.y += MAX(self.latestDelta,self.latestDelta*scrollResistance);
            }
            else {
                center.y += MIN(self.latestDelta,self.latestDelta*scrollResistance);
            }
            item.center = center;
        }

        [self.dynamicAnimator addBehavior:springBehavIoUr];
        [self.visibleIndexPathsSet addobject:item.indexPath];
    }];
}


-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {

    UIScrollView *scrollView = self.collectionView;
    CGFloat delta = newBounds.origin.y - scrollView.bounds.origin.y;

    self.latestDelta = delta;

    CGPoint touchLocation = [self.collectionView.panGestureRecognizer locationInView:self.collectionView];

    __block UIDynamicAnimator *weakDynamicAnimator = self.dynamicAnimator;

    [self.dynamicAnimator.behaviors enumerateObjectsUsingBlock:^(UIAttachmentBehavior *springBehavIoUr,BOOL *stop) {

        CGFloat ydistanceFromTouch = fabs(touchLocation.y - springBehavIoUr.anchorPoint.y);
        CGFloat xdistanceFromTouch = fabs(touchLocation.x - springBehavIoUr.anchorPoint.x);
        CGFloat scrollResistance = (ydistanceFromTouch + xdistanceFromTouch) / 1500.0f;

        UICollectionViewLayoutAttributes *item = (UICollectionViewLayoutAttributes*)[springBehavIoUr.items firstObject];
        CGPoint center = item.center;
        if (delta < 0) {
            center.y += MAX(delta,delta*scrollResistance);
        }
        else {
            center.y += MIN(delta,delta*scrollResistance);
        }
        item.center = center;

        [weakDynamicAnimator updateItemUsingCurrentState:item];
    }];

    return NO;
}

@end

SampleCode的答案:
示例代码显示了这种奇怪的摆动效应,因为项目的大小是以编程方式生成的,并且不是四舍五入的 – 计算的精度会为UIDynamics和物理引擎创建问题,并且无法达到均衡.简单地舍入生成的项目大小给物理学一个机会.见NoteCollectionViewController.swift第77行.

func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let w = round(CellAspectRatio.width * collectionView.frame.width)
    let h = round(CellAspectRatio.height * collectionView.frame.height)
    return CGSizeMake(w,h)
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...