Coder Social home page Coder Social logo

longchliu / cxswipegesturerecognizer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dclelland/cxswipegesturerecognizer

0.0 1.0 0.0 13 KB

UIPanGestureRecognizer subclass that takes much of the effort out of recognizing directional swipes.

License: MIT License

Ruby 8.11% Objective-C 91.89%

cxswipegesturerecognizer's Introduction

CXSwipeGestureRecognizer

UIGestureRecognizer subclass that takes much of the effort out of managing directional swipes.

CXSwipeGestureRecognizer *gestureRecognizer = [[CXSwipeGestureRecognizer alloc] init];
gestureRecognizer.delegate = self;
[self.view addGestureRecognizer:gestureRecognizer];

✓ Keeps track of both the current direction and the direction the gesture was started in.

if (gestureRecognizer.initialDirection == CXSwipeGestureDirectionUpwards) {
    if (gestureRecognizer.currentDirection == CXSwipeGestureDirectionDownwards) {
        NSLog(@"Gesture recognizer started swiping upwards and then changed direction");
    }
}

✓ Delegate protocol methods for start, update, cancel, and finish.

- (void)swipeGestureRecognizerDidStart:(CXSwipeGestureRecognizer *)gestureRecognizer
{
    NSLog("Gesture recognizer started");
}

- (void)swipeGestureRecognizerDidUpdate:(CXSwipeGestureRecognizer *)gestureRecognizer
{
    NSLog("Gesture recognizer updated");
}

- (void)swipeGestureRecognizerDidCancel:(CXSwipeGestureRecognizer *)gestureRecognizer
{
    NSLog("Gesture recognizer cancelled");
}

- (void)swipeGestureRecognizerDidFinish:(CXSwipeGestureRecognizer *)gestureRecognizer
{
    NSLog("Gesture recognizer finished");
}

✓ Convenience methods for location, translation, velocity, and progress (note that these work by calling locationInView: etc on self.view.superview, in case you are using the gesture recognizer to translate the view directly underneath itself).

NSLog(@"location: %f", gestureRecognizer.location);
NSLog(@"translation: %f", gestureRecognizer.translation);
NSLog(@"velocity: %f", gestureRecognizer.velocity);
NSLog(@"progress: %f", gestureRecognizer.progress);

✓ Delegate method for cancellation.

/* Cancels the gesture if it has moved less than 64 points, or if it is moving at less than 256 points per second */
- (BOOL)swipeGestureRecognizerShouldCancel:(CXSwipeGestureRecognizer *)gestureRecognizer
{
    return gestureRecognizer.translation < 64.0 && gestureRecognizer.velocity < 256.0;
}

✓ Delegate method for bouncing (returning YES causes the progress value to be halved, useful when emulating a UIScrollView-style bounce effect).

/* Bounces the gesture if it has moved backwards past its point of origin */
- (BOOL)swipeGestureRecognizerShouldBounce:(CXSwipeGestureRecognizer *)gestureRecognizer
{
    return gestureRecognizer.translation < 0.0;
}

Full API:

CXSwipeGestureRecognizerDelegate

- (void)swipeGestureRecognizerDidStart:(CXSwipeGestureRecognizer *)gestureRecognizer;
- (void)swipeGestureRecognizerDidUpdate:(CXSwipeGestureRecognizer *)gestureRecognizer;
- (void)swipeGestureRecognizerDidCancel:(CXSwipeGestureRecognizer *)gestureRecognizer;
- (void)swipeGestureRecognizerDidFinish:(CXSwipeGestureRecognizer *)gestureRecognizer;

- (BOOL)swipeGestureRecognizerShouldCancel:(CXSwipeGestureRecognizer *)gestureRecognizer;
- (BOOL)swipeGestureRecognizerShouldBounce:(CXSwipeGestureRecognizer *)gestureRecognizer;

CXSwipeGestureRecognizer : UIPanGestureRecognizer

@property (unsafe_unretained) id <CXSwipeGestureRecognizerDelegate> delegate;

- (CXSwipeGestureDirection)initialDirection;
- (CXSwipeGestureDirection)currentDirection;

- (CGFloat)location;
- (CGFloat)locationInDirection:(CXSwipeGestureDirection)direction;
- (CGFloat)locationInDirection:(CXSwipeGestureDirection)direction inView:(UIView *)view;

- (CGFloat)translation;
- (CGFloat)translationInDirection:(CXSwipeGestureDirection)direction;
- (CGFloat)translationInDirection:(CXSwipeGestureDirection)direction inView:(UIView *)view;

- (CGFloat)velocity;
- (CGFloat)velocityInDirection:(CXSwipeGestureDirection)direction;
- (CGFloat)velocityInDirection:(CXSwipeGestureDirection)direction inView:(UIView *)view;

- (CGFloat)progress;
- (CGFloat)progressInDirection:(CXSwipeGestureDirection)direction;
- (CGFloat)progressInDirection:(CXSwipeGestureDirection)direction inView:(UIView *)view;

cxswipegesturerecognizer's People

Contributors

dclelland avatar

Watchers

James Cloos avatar

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.