Skip to content

Commit

Permalink
Merge pull request #1 from iamphill/delegate-methods
Browse files Browse the repository at this point in the history
Added two delegate methods (Thanks iamphill!)
  • Loading branch information
zhxnlai committed Nov 11, 2014
2 parents 48d67f0 + 8d96394 commit 78d6f1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
2 changes: 2 additions & 0 deletions ZLSwipeableView/ZLSwipeableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
@optional
- (void)swipeableView: (ZLSwipeableView *)swipeableView didSwipeLeft:(UIView *)view;
- (void)swipeableView: (ZLSwipeableView *)swipeableView didSwipeRight:(UIView *)view;
- (void)swipeableView: (ZLSwipeableView *)swipeableView didStartSwipingView:(UIView *)view atLocation:(CGPoint)location;
- (void)swipeableView: (ZLSwipeableView *)swipeableView swipingView:(UIView *)view atLocation:(CGPoint)location;
- (void)swipeableView: (ZLSwipeableView *)swipeableView didEndSwipingView:(UIView *)view atLocation:(CGPoint)location;
@end


Expand Down
51 changes: 29 additions & 22 deletions ZLSwipeableView/ZLSwipeableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ -(void)setup {
self.reuseCoverContainerView = [[UIView alloc] initWithFrame:self.bounds];
self.reuseCoverContainerView.userInteractionEnabled = false;
[self addSubview:self.reuseCoverContainerView];

// Default properties
self.isRotationEnabled = YES;
self.rotationDegree = 1;
self.rotationRelativeYOffsetFromCenter = 0.3;

self.pushVelocityMagnitude = 1000;
self.escapeVelocityThreshold = 750;
self.relativeDisplacementThreshold = 0.25;

self.manualSwipeRotationRelativeYOffsetFromCenter = -0.2;
self.swipeableViewsCenter = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
self.collisionRect = [self defaultCollisionRect];
}
-(void)layoutSubviews {
[super layoutSubviews];

self.anchorContainerView.frame = CGRectMake(0, 0, 1, 1);
self.containerView.frame = self.bounds;
self.reuseCoverContainerView.frame = self.bounds;
Expand Down Expand Up @@ -133,12 +133,12 @@ - (void)loadNextSwipeableViewsIfNeeded:(BOOL)animated {
- (void)animateSwipeableViewsIfNeeded {
UIView *topSwipeableView = [self topSwipeableView];
if (!topSwipeableView) {return;}

for (UIView *cover in self.containerView.subviews) {
cover.userInteractionEnabled = NO;
}
topSwipeableView.userInteractionEnabled = YES;

for (UIGestureRecognizer *recognizer in topSwipeableView.gestureRecognizers) {
if (recognizer.state != UIGestureRecognizerStatePossible) {
return;
Expand Down Expand Up @@ -167,20 +167,23 @@ - (void)animateSwipeableViewsIfNeeded {
-(void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self];
CGPoint location = [recognizer locationInView:self];

UIView *swipeableView = recognizer.view;

if (recognizer.state == UIGestureRecognizerStateBegan) {
[self createAnchorViewForCover:swipeableView atLocation:location shouldAttachAnchorViewToPoint:YES];
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableView:didStartSwipingView:atLocation:)]) {
[self.delegate swipeableView:self didStartSwipingView:swipeableView atLocation:location];
}
}

if (recognizer.state == UIGestureRecognizerStateChanged) {
self.anchorViewAttachmentBehavior.anchorPoint = location;
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableView:swipingView:atLocation:)]) {
[self.delegate swipeableView:self swipingView:swipeableView atLocation:location];
}
}

if(recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
CGPoint velocity = [recognizer velocityInView:self];
CGFloat velocityMagnitude = sqrt(pow(velocity.x,2)+pow(velocity.y,2));
Expand All @@ -198,11 +201,15 @@ -(void)handlePan:(UIPanGestureRecognizer *)recognizer {
} else {
[self.animator removeBehavior:self.swipeableViewAttachmentBehavior];
[self.animator removeBehavior:self.anchorViewAttachmentBehavior];

[self.anchorView removeFromSuperview];
self.swipeableViewSnapBehavior = [self snapBehaviorThatSnapView:swipeableView toPoint:self.swipeableViewsCenter];
[self.animator addBehavior:self.swipeableViewSnapBehavior];
}

if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableView:didEndSwipingView:atLocation:)]) {
[self.delegate swipeableView:self didEndSwipingView:swipeableView atLocation:location];
}
}
}
- (void)swipeTopViewToLeft {
Expand All @@ -214,7 +221,7 @@ - (void)swipeTopViewToRight {
- (void)swipeTopViewToLeft:(BOOL)left {
UIView *topSwipeableView = [self topSwipeableView];
if (!topSwipeableView) {return;}

CGPoint location = CGPointMake(topSwipeableView.center.x, topSwipeableView.center.y*(1+self.manualSwipeRotationRelativeYOffsetFromCenter));
[self createAnchorViewForCover:topSwipeableView atLocation:location shouldAttachAnchorViewToPoint:YES];
CGVector direction = CGVectorMake((left?-1:1)*self.escapeVelocityThreshold, 0);
Expand Down Expand Up @@ -269,7 +276,7 @@ - (UIAttachmentBehavior *)attachmentBehaviorThatAnchorsView:(UIView *) aView toV
- (UIAttachmentBehavior *)attachmentBehaviorThatAnchorsView:(UIView *) aView toPoint:(CGPoint )aPoint
{
if (!aView) {return nil;}

CGPoint p = aView.center;
UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc]
initWithItem:aView
Expand All @@ -284,15 +291,15 @@ - (UIAttachmentBehavior *)attachmentBehaviorThatAnchorsView:(UIView *) aView toP
- (void)createAnchorViewForCover:(UIView *)swipeableView atLocation: (CGPoint)location shouldAttachAnchorViewToPoint: (BOOL) shouldAttachToPoint{
[self.animator removeBehavior:self.swipeableViewSnapBehavior];
self.swipeableViewSnapBehavior = nil;

self.anchorView = [[UIView alloc] initWithFrame: CGRectMake(location.x-500, location.y-500, 1000, 1000)];
[self.anchorView setBackgroundColor:[UIColor blueColor]];
[self.anchorView setHidden:!self.isAnchorViewVisiable];
[self.anchorContainerView addSubview: self.anchorView];
UIAttachmentBehavior *attachToView = [self attachmentBehaviorThatAnchorsView:swipeableView toView:self.anchorView];
[self.animator addBehavior:attachToView];
self.swipeableViewAttachmentBehavior = attachToView;

if (shouldAttachToPoint) {
UIAttachmentBehavior *attachToPoint = [self attachmentBehaviorThatAnchorsView:self.anchorView toPoint:location];
[self.animator addBehavior:attachToPoint];
Expand All @@ -311,28 +318,28 @@ - (void)pushAnchorViewForCover:(UIView *)swipeableView inDirection:(CGVector)dir
}
// NSLog(@"pushing cover to direction: %f, %f", direction.dx, direction.dy);
[self.animator removeBehavior:self.anchorViewAttachmentBehavior];

UICollisionBehavior *collisionBehavior = [self collisionBehaviorThatBoundsView:self.anchorView inRect:collisionRect];
collisionBehavior.collisionDelegate = self;
[self.animator addBehavior:collisionBehavior];

UIPushBehavior *pushBehavior = [self pushBehaviorThatPushView:self.anchorView toDirection:direction];
[self.animator addBehavior:pushBehavior];

[self.reuseCoverContainerView addSubview:self.anchorView];
[self.reuseCoverContainerView addSubview:swipeableView];
[self.reuseCoverContainerView sendSubviewToBack:swipeableView];

self.anchorView = nil;

[self loadNextSwipeableViewsIfNeeded:NO];
}
#pragma mark - UICollisionBehaviorDelegate
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier
{
// NSLog(@"ended contact");
NSMutableSet *viewsToRemove = [[NSMutableSet alloc] init];

for (id aBehavior in self.animator.behaviors) {
if ([aBehavior isKindOfClass:[UIAttachmentBehavior class]]) {
NSArray *items = ((UIAttachmentBehavior *)aBehavior).items;
Expand Down Expand Up @@ -360,7 +367,7 @@ - (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(i
}
}
}

for (UIView *view in viewsToRemove) {
[view removeFromSuperview];
}
Expand Down

0 comments on commit 78d6f1f

Please sign in to comment.