Skip to content

Commit

Permalink
added direction check
Browse files Browse the repository at this point in the history
  • Loading branch information
zhxnlai committed Nov 3, 2014
1 parent 87eb7f9 commit cc19762
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 544 deletions.
4 changes: 2 additions & 2 deletions ZLSwipeableView.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ZLSwipeableView"
s.version = "0.0.1"
s.version = "0.0.3"
s.summary = "A simple view for building card like interface like Tinder and Potluck."
s.description = <<-DESC
ZLSwipeableView is a simple view for building card like interface like Tinder and Potluck. It uses dataSource pattern and is highly customizable.
Expand All @@ -10,7 +10,7 @@ Pod::Spec.new do |s|
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Zhixuan Lai" => "zhxnlai@gmail.com" }
s.platform = :ios, "7.0"
s.source = { :git => "https://github.com/zhxnlai/ZLSwipeableView.git", :tag => "0.0.1" }
s.source = { :git => "https://github.com/zhxnlai/ZLSwipeableView.git", :tag => "0.0.3" }
s.source_files = "ZLSwipeableView", "ZLSwipeableView/**/*.{h,m}"
# s.exclude_files = "Classes/Exclude"
s.framework = "UIKit"
Expand Down
7 changes: 3 additions & 4 deletions ZLSwipeableView/ZLSwipeableView.h
Expand Up @@ -11,12 +11,11 @@
@class ZLSwipeableView;

// Delegate
@protocol ZLSwipeableContainerViewDelegate <NSObject>
@protocol ZLSwipeableViewDelegate <NSObject>
@optional

- (void)swipeableView: (ZLSwipeableView *)swipeableView didSwipeLeft:(UIView *)view;
- (void)swipeableView: (ZLSwipeableView *)swipeableView didSwipeRight:(UIView *)view;

- (void)swipeableView: (ZLSwipeableView *)swipeableView swipingView:(UIView *)view atLocation:(CGPoint)location;
@end


Expand All @@ -28,7 +27,7 @@

@interface ZLSwipeableView : UIView
@property (nonatomic, weak) id <ZLSwipeableViewDataSource> dataSource;
@property (nonatomic, weak) id <ZLSwipeableContainerViewDelegate> delegate;
@property (nonatomic, weak) id <ZLSwipeableViewDelegate> delegate;

/**
* Enable this to rotate the views behind the top view. Default to YES.
Expand Down
14 changes: 10 additions & 4 deletions ZLSwipeableView/ZLSwipeableView.m
Expand Up @@ -30,7 +30,7 @@ @interface ZLSwipeableView () <UICollisionBehaviorDelegate, UIDynamicAnimatorDel
@implementation ZLSwipeableView

#pragma mark - Init
- (id)initWithFrame:(CGRect)frame {
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
Expand All @@ -55,8 +55,6 @@ -(void)setup {
self.reuseCoverContainerView.userInteractionEnabled = false;
[self addSubview:self.reuseCoverContainerView];

NSLog(@"self frame: %f %f %f %f", self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
NSLog(@"self bounds: %f %f %f %f", self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height);
// Default properties
self.isRotationEnabled = YES;
self.rotationDegree = 1;
Expand Down Expand Up @@ -178,6 +176,9 @@ -(void)handlePan:(UIPanGestureRecognizer *)recognizer {

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) {
Expand All @@ -186,7 +187,9 @@ -(void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint normalizedVelocity = CGPointMake(velocity.x/velocityMagnitude, velocity.y/velocityMagnitude);
if ((ABS(translation.x) > self.relativeDisplacementThreshold*self.bounds.size.width //displacement
|| velocityMagnitude > self.escapeVelocityThreshold) //velocity
&& ABS(normalizedVelocity.y)<0.8f) { //direction
&& (signum(translation.x)==signum(normalizedVelocity.x)) //sign X
&& (signum(translation.y)==signum(normalizedVelocity.y)) //sign Y
&& ABS(normalizedVelocity.y)<0.8f) { // confine veritcal direction
CGFloat scale = velocityMagnitude > self.escapeVelocityThreshold ? self.pushVelocityMagnitude:self.pushVelocityMagnitude*0.66;
CGFloat translationMagnitude = sqrtf(translation.x*translation.x+translation.y*translation.y);
CGVector direction = CGVectorMake(translation.x/translationMagnitude*scale, translation.y/translationMagnitude*scale);
Expand Down Expand Up @@ -374,6 +377,8 @@ - (CGFloat) radiansToDegrees: (CGFloat) radians
return radians * 180 / M_PI;
};

int signum(float n) { return (n < 0) ? -1 : (n > 0) ? +1 : 0; }

- (CGRect)defaultCollisionRect {
CGSize viewSize = [UIScreen mainScreen].applicationFrame.size;
CGFloat collisionSizeScale = 6;
Expand Down Expand Up @@ -406,4 +411,5 @@ - (UIView *)topSwipeableView {
return self.containerView.subviews.lastObject;
}


@end
31 changes: 16 additions & 15 deletions ZLSwipeableViewDemo/ZLSwipeableViewDemo.xcodeproj/project.pbxproj
Expand Up @@ -16,8 +16,8 @@
092F0F4E1A05B474007CDDCC /* ZLSwipeableViewDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 092F0F4D1A05B474007CDDCC /* ZLSwipeableViewDemoTests.m */; };
092F0F661A05C9CA007CDDCC /* UIColor+FlatColors.m in Sources */ = {isa = PBXBuildFile; fileRef = 092F0F651A05C9CA007CDDCC /* UIColor+FlatColors.m */; };
092F0F691A05DAF9007CDDCC /* CardView.m in Sources */ = {isa = PBXBuildFile; fileRef = 092F0F681A05DAF8007CDDCC /* CardView.m */; };
092F0F701A05F8B3007CDDCC /* ZLPanGestureRecognizer.m in Sources */ = {isa = PBXBuildFile; fileRef = 092F0F6D1A05F8B3007CDDCC /* ZLPanGestureRecognizer.m */; };
092F0F711A05F8B3007CDDCC /* ZLSwipeableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 092F0F6F1A05F8B3007CDDCC /* ZLSwipeableView.m */; };
0971D4601A08425E00325B8D /* ZLPanGestureRecognizer.m in Sources */ = {isa = PBXBuildFile; fileRef = 0971D45D1A08425E00325B8D /* ZLPanGestureRecognizer.m */; };
0971D4611A08425E00325B8D /* ZLSwipeableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0971D45F1A08425E00325B8D /* ZLSwipeableView.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -48,10 +48,10 @@
092F0F651A05C9CA007CDDCC /* UIColor+FlatColors.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIColor+FlatColors.m"; sourceTree = "<group>"; };
092F0F671A05DAF8007CDDCC /* CardView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CardView.h; sourceTree = "<group>"; };
092F0F681A05DAF8007CDDCC /* CardView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CardView.m; sourceTree = "<group>"; };
092F0F6C1A05F8B3007CDDCC /* ZLPanGestureRecognizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZLPanGestureRecognizer.h; sourceTree = "<group>"; };
092F0F6D1A05F8B3007CDDCC /* ZLPanGestureRecognizer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZLPanGestureRecognizer.m; sourceTree = "<group>"; };
092F0F6E1A05F8B3007CDDCC /* ZLSwipeableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZLSwipeableView.h; sourceTree = "<group>"; };
092F0F6F1A05F8B3007CDDCC /* ZLSwipeableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZLSwipeableView.m; sourceTree = "<group>"; };
0971D45C1A08425E00325B8D /* ZLPanGestureRecognizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZLPanGestureRecognizer.h; sourceTree = "<group>"; };
0971D45D1A08425E00325B8D /* ZLPanGestureRecognizer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZLPanGestureRecognizer.m; sourceTree = "<group>"; };
0971D45E1A08425E00325B8D /* ZLSwipeableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZLSwipeableView.h; sourceTree = "<group>"; };
0971D45F1A08425E00325B8D /* ZLSwipeableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZLSwipeableView.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -94,7 +94,7 @@
isa = PBXGroup;
children = (
092F0F631A05C9CA007CDDCC /* UIColor+FlatColors */,
092F0F6B1A05F8B3007CDDCC /* ZLSwipeableView */,
0971D45B1A08425E00325B8D /* ZLSwipeableView */,
092F0F351A05B474007CDDCC /* AppDelegate.h */,
092F0F361A05B474007CDDCC /* AppDelegate.m */,
092F0F381A05B474007CDDCC /* ViewController.h */,
Expand Down Expand Up @@ -144,15 +144,16 @@
path = "UIColor+FlatColors";
sourceTree = "<group>";
};
092F0F6B1A05F8B3007CDDCC /* ZLSwipeableView */ = {
0971D45B1A08425E00325B8D /* ZLSwipeableView */ = {
isa = PBXGroup;
children = (
092F0F6C1A05F8B3007CDDCC /* ZLPanGestureRecognizer.h */,
092F0F6D1A05F8B3007CDDCC /* ZLPanGestureRecognizer.m */,
092F0F6E1A05F8B3007CDDCC /* ZLSwipeableView.h */,
092F0F6F1A05F8B3007CDDCC /* ZLSwipeableView.m */,
0971D45C1A08425E00325B8D /* ZLPanGestureRecognizer.h */,
0971D45D1A08425E00325B8D /* ZLPanGestureRecognizer.m */,
0971D45E1A08425E00325B8D /* ZLSwipeableView.h */,
0971D45F1A08425E00325B8D /* ZLSwipeableView.m */,
);
path = ZLSwipeableView;
name = ZLSwipeableView;
path = ../../ZLSwipeableView;
sourceTree = "<group>";
};
/* End PBXGroup section */
Expand Down Expand Up @@ -256,12 +257,12 @@
buildActionMask = 2147483647;
files = (
092F0F3A1A05B474007CDDCC /* ViewController.m in Sources */,
092F0F711A05F8B3007CDDCC /* ZLSwipeableView.m in Sources */,
092F0F661A05C9CA007CDDCC /* UIColor+FlatColors.m in Sources */,
092F0F371A05B474007CDDCC /* AppDelegate.m in Sources */,
092F0F341A05B474007CDDCC /* main.m in Sources */,
092F0F691A05DAF9007CDDCC /* CardView.m in Sources */,
092F0F701A05F8B3007CDDCC /* ZLPanGestureRecognizer.m in Sources */,
0971D4601A08425E00325B8D /* ZLPanGestureRecognizer.m in Sources */,
0971D4611A08425E00325B8D /* ZLSwipeableView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit cc19762

Please sign in to comment.