Skip to content

Commit

Permalink
fixed demo project
Browse files Browse the repository at this point in the history
  • Loading branch information
zhxnlai committed Nov 2, 2014
1 parent 7818da7 commit 87eb7f9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -29,12 +29,30 @@ ZLSwipeableView *swipeableView = [[ZLSwipeableView alloc] initWithFrame:self.vie
A `ZLSwipeableView` **must** have an object that implements `ZLSwipeableViewDelegate` to act as a data source. `ZLSwipeableView` will prefetch **three** views in advance to animate them.
~~~objective-c
// required data source
self.swipeableView.dataSource = self;
#pragma mark - ZLSwipeableViewDataSource
- (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView {
return [[UIView alloc] init];
}
~~~
A `ZLSwipeableView` can have an optional delegate to receive callback.
~~~objective-c
// optional delegate
self.swipeableView.delegate = self;

#pragma mark - ZLSwipeableViewDelegate
- (void)swipeableView: (ZLSwipeableView *)swipeableView didSwipeLeft:(UIView *)view {
NSLog(@"did swipe left");
}
- (void)swipeableView: (ZLSwipeableView *)swipeableView didSwipeRight:(UIView *)view {
NSLog(@"did swipe right");
}
- (void)swipeableView: (ZLSwipeableView *)swipeableView swipingView:(UIView *)view atLocation:(CGPoint)location {
NSLog(@"swiping at location: x %f, y%f", location.x, location.y);
}
~~~

To swipe the top view programmatically:
~~~objective-c
Expand Down
Expand Up @@ -27,7 +27,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="a3E-tF-qFC" userLabel="Reload">
<rect key="frame" x="275.5" y="542" width="49" height="30"/>
<rect key="frame" x="276" y="542" width="49" height="30"/>
<state key="normal" title="Reload">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
Expand All @@ -44,7 +44,7 @@
<action selector="swipeRightButtonAction:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="tZK-sW-KTp"/>
</connections>
</button>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="C0K-0Y-C7k" userLabel="SwipeableContainerView" customClass="ZLSwipeableView">
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="C0K-0Y-C7k" userLabel="ZLSwipeableView" customClass="ZLSwipeableView">
<rect key="frame" x="36" y="60" width="528" height="442"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
Expand All @@ -64,7 +64,7 @@
</constraints>
</view>
<connections>
<outlet property="containerView" destination="C0K-0Y-C7k" id="rr8-xW-Wlk"/>
<outlet property="swipeableView" destination="C0K-0Y-C7k" id="DWp-Pc-Kwe"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
Expand Down
25 changes: 22 additions & 3 deletions ZLSwipeableViewDemo/ZLSwipeableViewDemo/ViewController.m
Expand Up @@ -11,7 +11,7 @@
#import "UIColor+FlatColors.h"
#import "CardView.h"

@interface ViewController () <ZLSwipeableViewDataSource>
@interface ViewController () <ZLSwipeableViewDataSource, ZLSwipeableViewDelegate>
@property (weak, nonatomic) IBOutlet ZLSwipeableView *swipeableView;

@property (nonatomic, strong) NSArray *colors;
Expand Down Expand Up @@ -47,12 +47,17 @@ - (void)viewDidLoad {
@"Asbestos",
];

// ZLSwipeableContainterView *container = [[ZLSwipeableContainterView alloc] initWithFrame:self.view.frame];
// [self.view addSubview:container];
// ZLSwipeableView *swipeableView = [[ZLSwipeableView alloc] initWithFrame:self.view.frame];
// [self.view addSubview:swipeableView];

[self.swipeableView setNeedsLayout];
[self.swipeableView layoutIfNeeded];

// required data source
self.swipeableView.dataSource = self;

// optional delegate
self.swipeableView.delegate = self;
}
- (IBAction)swipeLeftButtonAction:(UIButton *)sender {
[self.swipeableView swipeTopViewToLeft];
Expand All @@ -66,6 +71,18 @@ - (IBAction)reloadButtonAction:(UIButton *)sender {
[self.swipeableView loadNextSwipeableViewsIfNeeded];
}

#pragma mark - ZLSwipeableViewDelegate
- (void)swipeableView: (ZLSwipeableView *)swipeableView didSwipeLeft:(UIView *)view {
NSLog(@"did swipe left");
}
- (void)swipeableView: (ZLSwipeableView *)swipeableView didSwipeRight:(UIView *)view {
NSLog(@"did swipe right");
}
- (void)swipeableView: (ZLSwipeableView *)swipeableView swipingView:(UIView *)view atLocation:(CGPoint)location {
NSLog(@"swiping at location: x %f, y%f", location.x, location.y);
}

#pragma mark - ZLSwipeableViewDataSource
- (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView {
if (self.colorIndex<self.colors.count) {
CardView *view = [[CardView alloc] initWithFrame:swipeableView.bounds];
Expand All @@ -75,6 +92,8 @@ - (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView {
}
return nil;
}

#pragma mark - ()
- (UIColor *)colorForName:(NSString *)name
{
NSString *sanitizedName = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
Expand Down
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
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 Down

0 comments on commit 87eb7f9

Please sign in to comment.