Coder Social home page Coder Social logo

zlswipeableview's Introduction

ZLSwipeableView

Build Status Gitter

A simple view for building card like interface like Tinder and Potluck. ZLSwipeableView was originally developed for Murmur.

Notice: The Objective-C version of ZLSwipeableView is no longer actively maintained.

Please checkout the swift version: https://github.com/zhxnlai/ZLSwipeableViewSwift

Preview

###Swipe swipe ###Swipe Cancel cancel ###Swipe Programmatically swipeLeft ###Swipe Programmatically II swipeLeftRight

CocoaPods

You can install ZLSwipeableView through CocoaPods adding the following to your Podfile:

pod 'ZLSwipeableView'

Usage

Check out the demo app for an example.

ZLSwipeableView can be added to storyboard or instantiated programmatically:

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

A ZLSwipeableView must have an object that implements ZLSwipeableViewDataSource to act as a data source. ZLSwipeableView will prefetch three views in advance to animate them.

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

#pragma mark - ZLSwipeableViewDataSource
- (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView {
  return [[UIView alloc] init];
}

The demo app includes examples of both creating views programmatically and loading views from Xib files that use Auto Layout.

A ZLSwipeableView can have an optional delegate to receive callback.

// optional delegate
self.swipeableView.delegate = self;

#pragma mark - ZLSwipeableViewDelegate
- (void)swipeableView:(ZLSwipeableView *)swipeableView
         didSwipeView:(UIView *)view
          inDirection:(ZLSwipeableViewDirection)direction {
    NSLog(@"did swipe in direction: %zd", direction);
}
- (void)swipeableView:(ZLSwipeableView *)swipeableView didCancelSwipe:(UIView *)view {
  NSLog(@"did cancel swipe");
}
- (void)swipeableView:(ZLSwipeableView *)swipeableView didStartSwipingView:(UIView *)view atLocation:(CGPoint)location {
    NSLog(@"did start swiping at location: x %f, y%f", location.x, location.y);
}
- (void)swipeableView:(ZLSwipeableView *)swipeableView swipingView:(UIView *)view atLocation:(CGPoint)location  translation:(CGPoint)translation {
	NSLog(@"swiping at location: x %f, y %f, translation: x %f, y %f", location.x, location.y, translation.x, translation.y);
}
- (void)swipeableView:(ZLSwipeableView *)swipeableView didEndSwipingView:(UIView *)view atLocation:(CGPoint)location {
    NSLog(@"did start swiping at location: x %f, y%f", location.x, location.y);
}

To swipe the top view programmatically:

[self.swipeableView swipeTopViewToLeft];
[self.swipeableView swipeTopViewToRight];
...

To discard all views and reload programmatically:

[self.swipeableView discardAllViews];
[self.swipeableView loadViewsIfNeeded];

Requirements

  • iOS 7 or higher.
  • Automatic Reference Counting (ARC).

Credits

  • Thanks iamphill for adding new delegates.
  • Thanks mdznr for making the code style consistent.
  • Thanks coryalder for making dataSource and delegate IBOutlets.

License

ZLSwipeableView is available under MIT license. See the LICENSE file for more info.

zlswipeableview's People

Contributors

coryalder avatar dannyshmueli avatar gitter-badger avatar gorbannastya avatar louis49 avatar mdznr avatar sachinkesiraju avatar zhxnlai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zlswipeableview's Issues

Can we add a method to cancel swipe i-e (cancelSwipe) manually via code? like swipeTopViewToLeft etc

I am implementing up and down functionality to view next and previous items. What I want is when I do swipe up, previous item will be shown And if current item is the first item i-e no previous item. Then I want to cancelSwipe with a shake effect to get the user an idea that there are no items before it.

If you have any other better ideas to do this kind of functionality, please suggest me.

Also, in future this functionality can also be used similiar to pull-to-refresh feature in tableview.

Thanks

Question: manually change location of topSwipeableView

@zhxnlai (or anyone else),

I have a question please: my swipeable view contains a UITextField. Now, when the user taps this text field, I would like to manually move the topSwipeableView a bit up, so the keyboard will not hide the text field.
If I try to do it by changing the frame, the view moves but immediately returns back to its original location (I am not using auto layout).

What do you think would be a good way to work around this issue?

This is my code which gets triggered when the keyboard is about to be shown:

-(void)keyboardWillShow:(NSNotification *)notification
{
    CGRect keyboardBounds;
    [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];

    UIViewAnimationOptions options = (curve << 16) | UIViewAnimationOptionBeginFromCurrentState;

    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:duration delay:0 options:options animations:^{
        CGRect newFrame = weakSelf.swipableContainerView.topSwipeableView.frame;
        newFrame.origin.y -= keyboardBounds.size.height;
        weakSelf.swipableContainerView.topSwipeableView.frame = newFrame;
    } completion:NULL];
}

Any help would be very much appreciated!

UserInteractionEnabled Issue

while i am moving the first card, i can also move the second card.
there is a bug.
card display wrong.
i need help, please fix. thank you very much.

Adding swipe to specific regions

Can I get let's say I just want the swipe right to be available on the bottom part of the card but the swipe left will be available to the whole card? Any tips?

SwipeableView does not move after returning from a segue/modal

The SwipeableView is placed within a ViewController that has a segue to another ViewController. If I perform that segue and come back it often happens that I can't swipe the SwipeableView, but the delegate methods get triggered. If I keep swiping the views it happens that at some point multiple views get swiped off screen.

Update Podspec

Hey,

could you please update the Podspec to include the newest commits?

Thanks

Neatly stacked tiles?

Would be nice if we can have a "sloppiness" float variable where 0.0 would have neatly stacked cards while 1.0 would have completely sloppy stacked tiles.

Undo View swipping

I'm trying to add this method to the ZLSwipeableView.

I started with the following as first draft, and it does bring the view back to the front, but it does not react the Pan gesture. What am I missing?

-(void)addSwipeableViewOnTop:(UIView *)view animated:(BOOL)animated {
    if (view) {
        [self.containerView addSubview:view];
        [self.containerView bringSubviewToFront:view];
        view.center = self.swipeableViewsCenterInitial;

        if (animated) {
            NSTimeInterval maxDelay = 0.3;
            NSTimeInterval delayStep = maxDelay / kNumPrefetchedViews;
            NSTimeInterval aggregatedDelay = maxDelay;
            NSTimeInterval animationDuration = 0.25;

            view.center = CGPointMake(view.center.x, -view.frame.size.height);
            [UIView animateWithDuration:animationDuration
                                  delay:aggregatedDelay
                                options:UIViewAnimationOptionCurveEaseIn
                             animations:^{
                                 view.center = self.swipeableViewsCenter;
                             }
                             completion:nil];
            aggregatedDelay -= delayStep;

            [self performSelector:@selector(animateSwipeableViewsIfNeeded)
                       withObject:nil
                       afterDelay:animationDuration];
        }
    }
}

A way to get the current top view

Is there a way in the API to get the current top view? I'm trying to do a custom transition involving the top view, and would ideally not have to keep a cache and state of what is the current top view for when I animate the view controller's pop.

If it doesn't already exist, it could either through a property of the ZLSwipeableView, or through a delegate method, like swipeableView:didSwipeView:inDirection: becoming swipeableView:didSwipeView:topView:inDirection:, or adding another delegate method like swipeableView:didMoveViewToTop:

If any of these (or another idea) sound good to you, I'd be happy to add a PR.

Thanks!

Need a reloadData method

I would like a reloadData function like UITableView when I have finished getting data from API request, and reload the SwipeableView.

Very slow animations with Swift on iPad

How to reproduce:

  • Create empty project
  • Import lib via bridge header
  • Write CardView class from demo with Swift
  • Put ZLSwipeableView as in demo project

I noticed that behaviour only on iPad, but O-C project works smoothly, and if I use smaller cards(100x200 for example) it works fine too.
Do you think that It is because of bridging? If so, I'll try to rewrite lib on Swift.

Swiping not working

Hi, I downloaded the demo project to test it, but I don't seem to be able to swipe left or right. The only way to do so is by touching the buttons "Swipe left" and "Swipe right", not by dragging it. Any help with that?

Thank you!

UIButton's touchUpInside is sometimes not triggered...

When button is inside the view which is being transferred to the ZLSwipeableView instance.

Any idea?

I tried a lot of stuff already, including override the hitTest:withEvent: method, but nothing seems to solve the problem.

Any idea? Any help would very much appreciated :)

Compiler warning in XCode 6.3.1 using Swift

I am trying to use this library and installed via cocoa pod.
I added the header file to my bridging header, and am able to add the delegate, datasource and use the class throughout my view controller swift file.
However, everytime I try to wire up a ZLSwipeableView (i.e. @IBOutlet weak var swipeableView: ZLSwipeableView!
)
from Storyboard to the view controller, I can no longer compile and get this error:

Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_ZLSwipeableView", referenced from:
  __TMaCSo15ZLSwipeableView in CardViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Thought it might be an issue with the bridging header or something not properly including the class files, but I have no problem compiling with all the code in place, just when I try to wire up a storyboard outlet using the ZLSwipeableView class.

Any suggestions?
Thanks!

Slow animation on iPhone 6 Plus simulator

I'm having slow animations on the iPhone 6 Plus simulator when testing the demo project.

I added shouldRasterize to the cardView view, but didn't seem to solve the issue.

- (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView {
    if (self.colorIndex >= self.colors.count) {
        self.colorIndex = 0;
    }

    CardView *view = [[CardView alloc] initWithFrame:swipeableView.bounds];
    view.backgroundColor = [self colorForName:self.colors[self.colorIndex]];
    self.colorIndex++;

    view.layer.shouldRasterize = YES;

...

Any ideas ?

Prevent view from escaping in specific direction(s)

For example, I would like to allow user to swipe the view in all possible directions, but allow it to escape only when the direction is left or right.

I do see the "direction" property, but from what I understand its goal is not that.
Is there a way to do it?

Thanks for the great project!

Adding a subview to Card View

When I add a subview to the Card View (in the demo, this is CardView.m), such as a label, irrespective of the frame of that label, it appears very small, and about 200 pixels for the y value, and about 20 for the x value (not what I set). I am adding it by, in CardView.m, writing [self addSubview:___];

Any reason why. I am working off the demo app, so it should be working.

Thanks for your help in advance!

Assertion failure

When I run the demo project I get this error and the app shuts down.

2014-12-01 19:51:09.944 ZLSwipeableViewDemo[368:60b] *** Assertion failure in -[UIDynamicAnimator _registerBodyForItem:shape:], /SourceCache/UIKit/UIKit-2935.137/UIDynamicAnimator.m:723
2014-12-01 19:51:09.951 ZLSwipeableViewDemo[368:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid size {264, 0} for item <CardView: 0x17d393e0; frame = (0 0; 264 0); gestureRecognizers = <NSArray: 0x17d40e40>; layer = <CALayer: 0x17d40640>> in Dynamics'

*** First throw call stack:
(0x2fd4dfd3 0x3a5c6ccf 0x2fd4dead 0x306fad5b 0x32b67ee5 0x32b6db89 0x32b65d91 0x1535b 0x14b75 0x142a9 0x1100f 0x3256e4cb 0x3256e289 0x32574ed9 0x32572847 0x325dc35d 0x325d8fcd 0x325d358b 0x3256f709 0x3256e871 0x325d2cc9 0x34bdbaed 0x34bdb6d7 0x2fd18ab7 0x2fd18a53 0x2fd17227 0x2fc81f0f 0x2fc81cf3 0x325d1ef1 0x325cd16d 0x13065 0x3aad3ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

Crash while swiping

My app crashes while swiping ZLSwipapleView.Its not giving exact location in code where is exception generated. Reproducability is low. Error message:Uncaught exception: CALayer position contains NaN: [nan nan]
Following are the crash logs:

  • thread #1: tid = 0x4bcc28, 0x32a87c64 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    • frame #0: 0x32a87c64 libobjc.A.dylibobjc_exception_throw frame #1: 0x252913e4 CoreFoundation+[NSException raise:format:] + 112
      frame #2: 0x28171c7a QuartzCoreCA::Layer::set_position(CA::Vec2<double> const&, bool) + 242 frame #3: 0x28171b7e QuartzCore-[CALayer setPosition:] + 54
      frame #4: 0x287a82fc UIKit-[UIView setCenter:] + 124 frame #5: 0x28e32e2e UIKit-[UIDynamicAnimator _defaultMapper:position:angle:itemType:] + 774
      frame #6: 0x28e344ce UIKit__48-[UIDynamicAnimator _registerBodyForItem:shape:]_block_invoke + 106 frame #7: 0x2e0a63ec PhysicsKit-[PKPhysicsWorld stepWithTime:velocityIterations:positionIterations:] + 724
      frame #8: 0x28e35442 UIKit-[UIDynamicAnimator _animatorStep:] + 170 frame #9: 0x28e35544 UIKit-[UIDynamicAnimator _displayLinkTick:] + 124
      frame #10: 0x28e30c0c UIKit-[UIDynamicAnimatorTicker _displayLinkTick:] + 60 frame #11: 0x281c7802 QuartzCoreCA::Display::DisplayLinkItem::dispatch() + 98
      frame #12: 0x281c766a QuartzCoreCA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 366 frame #13: 0x2caad82a IOMobileFramebufferIOMobileFramebufferVsyncNotifyFunc + 90
      frame #14: 0x261b74e0 IOKitIODispatchCalloutFromCFMessage + 256 frame #15: 0x252470a4 CoreFoundation__CFMachPortPerform + 132
      frame #16: 0x25257572 CoreFoundation__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34 frame #17: 0x2525750e CoreFoundation__CFRunLoopDoSource1 + 346
      frame #18: 0x25255b10 CoreFoundation__CFRunLoopRun + 1608 frame #19: 0x251a33c0 CoreFoundationCFRunLoopRunSpecific + 476
      frame #20: 0x251a31d2 CoreFoundationCFRunLoopRunInMode + 106 frame #21: 0x2c5a10a8 GraphicsServicesGSEventRunModal + 136
      frame #22: 0x287b2fa0 UIKit`UIApplicationMain + 1440

Support an "Undo" Action

This means, for example, when swiping a view away through the right side, the view should be returned from the right.

As for the implementation, I think the best would be if the ZLSwipeableView instance will always save a reference to the last instance which was swiped away, with the direction it was swiped away to. In addition, it will expose an undo method.

Rotation support

I noticed a strange behaviour: after the device rotates, the frame of the card goes offscreen (see screenshots).
img_0553
img_0552

How can this be resolved?

AutoLayout / XIB

Hey awesome piece of code! Problem... When you load a card from XIB.. and move it around stuff within the card does not stay within the bounds.. hard to explain but let me send a screen show.

How to reduce the memory footprint

Hello, you share a very powerful work .but I found a great memory occupied, when i use it.I want to know how to reduce the memory and release that view which is not on the screen. if can ,how to release.

Delegate Not Working

I'm having a problem getting the callback functions for swiping right and left to work.

  • (void)swipeableView:(ZLSwipeableView *)swipeableView didSwipeLeft:(UIView *)view
  • (void)swipeableView:(ZLSwipeableView *)swipeableView didSwipeRight:(UIView *)view

I think the setting of the delegate isn't working, but cannot work out why?

view scaled

view scaled and shape transformed when swipe

Don't show cards below the current one unless you swipe

When you are looking at cards in a resting state you can see some edges of cards behind the current one around the edges. Can you make it possible to not do that so that you can only see the current card assuming all cards are the same size until you start swiping?

Crash on IOS 7.1

2014-11-28 17:28:37.620 ZLSwipeableViewDemo[10178:60b] *** Assertion failure in -[UIDynamicAnimator _registerBodyForItem:shape:], /SourceCache/UIKit/UIKit-2935.137/UIDynamicAnimator.m:723
2014-11-28 17:28:37.631 ZLSwipeableViewDemo[10178:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid size {264, 0} for item <CardView: 0x170166a80; frame = (0 0; 264 0); gestureRecognizers = <NSArray: 0x1700511c0>; layer = <CALayer: 0x1702205c0>> in Dynamics'
*** First throw call stack:
(0x1896da950 0x195be01fc 0x1896da810 0x18a212db4 0x18cd37498 0x18cd3dff4 0x18cd35200 0x100054d28 0x100054568 0x100053d44 0x100050b2c 0x18c6a4670 0x18c6a43f4 0x18c6ab7a4 0x18c6a8d78 0x18c71b83c 0x18c718148 0x18c7119ec 0x18c6a58cc 0x18c6a4ad0 0x18c711044 0x18f2bf504 0x18f2bf030 0x18969a890 0x18969a7f0 0x189698a14 0x1895d96d0 0x18c7101c8 0x18c70afdc 0x100052c34 0x1961d3aa0)
libc++abi.dylib: terminating with uncaught exception of type NSException

Stack layout

Hi there, been using the library for a bit and really liking it.

I was wondering how the layout the views that are behind the top are determined? In this stack, sometimes they're all from the left, sometimes they're peeking in from the right. What I'd really like to mimic is the way it was in the demo, where sometimes they're peeking from the left, sometimes from the right, in an almost concentric fashion.
layout

Thanks so much for library!

how to smoothly swipe cards programmatically?

hello, i'm trying to swipe cards programmatically, when i call swipeTopViewToLeft method in a for-loop, my app don't swipe smoothly. i think it is because i call swipeTopViewToLeft method in a very short time, the previous card doesn't finish its animation. so, how can i implement the "swipe programmatically" effect like the gif you post in introduction.
really appreciate your reply.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.