I've been replicating the 2013 WWDC Session 217 "Exploring Scroll Views on iOS 7". I'm using Xcode 7 beta 2 and my project is iOS 9 only.
I’m trying to use a UIDynamicAnimator
with my UICollectionViewLayout
in a way similar to the one presented in session 217 to imitate Messages.app feel. My UICollectionViewLayout
is a custom one and for some reason my cells seem to bounce in circular motion in my project.
This is my custom layout code.
// 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
What could be going on here that's causing this circular motion? I'm only changing the Y axis property of my UIAttachmentAttributes
center.
center.y += MIN(scrollDelta, scrollDelta * scrollResistance);
What am I missing here? (tried this exact layout in other project and seems to work).
EDIT:
I uploaded a sample project(removed), the Custom Collection View Layout Class is called VVSpringCollectionViewFlowLayout.m
, haven't had much time to look into this too much myself since I've had a lot to do at work lately.
When the sample project runs (Xcode 7 beta or up), you'll be prompted with a slider, drag all the way to the right to visualized the Collection View Cells.