我一直在复制 2013 WWDC Session 217 “Exploring Scroll Views on iOS 7”。我正在使用 Xcode 7 beta 2,我的项目只有 iOS 9。
我正在尝试以类似于第 217 节中介绍的方式使用 a UIDynamicAnimator
with my来模仿 Messages.app 的感觉。UICollectionViewLayout
我UICollectionViewLayout
是自定义的,出于某种原因,我的细胞似乎在我的项目中以圆周运动反弹。
这是我的自定义布局代码。
// 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, 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 或更高版本),系统会提示您使用滑块,一直向右拖动以可视化 Collection View Cells。