Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift problem #32

Open
bjakus opened this issue Aug 26, 2014 · 1 comment
Open

Swift problem #32

bjakus opened this issue Aug 26, 2014 · 1 comment

Comments

@bjakus
Copy link

bjakus commented Aug 26, 2014

Hi! I have imported this library in my swift project. There is no errors but view does not show anything. Gestures are detected. When i swipe, viewForPage() method is called, but there is no change on the screen.
Can anybody help me?

@matiasvillaverde
Copy link

Hi, I just integrate this clases in my swift project. Soon I will upload a translation to swift 3, but in the mean time you can use this:

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@Class AFKPageFlipper;

@protocol AFKPageFlipperDataSource

  • (NSInteger) numberOfPagesForPageFlipper:(AFKPageFlipper *) pageFlipper;
  • (UIView *) viewForPage:(NSInteger) page inFlipper:(AFKPageFlipper *) pageFlipper;

@EnD

typedef enum {
AFKPageFlipperDirectionLeft,
AFKPageFlipperDirectionRight,
} AFKPageFlipperDirection;

@interface AFKPageFlipper : UIView {
NSObject *dataSource;
NSInteger currentPage;
NSInteger numberOfPages;

// shadows
CALayer *frontLayerShadow;
CALayer *backLayerShadow;
CALayer *leftLayerShadow;
CALayer *rightLayerShadow;
// shadows

CALayer *backgroundAnimationLayer;
CALayer *flipAnimationLayer;

AFKPageFlipperDirection flipDirection;
float startFlipAngle;
float endFlipAngle;
float currentAngle;

BOOL setNextViewOnCompletion;
BOOL animating;

BOOL disabled;

}

@Property (nonatomic,retain) NSObject *dataSource;
@Property (nonatomic,assign) NSInteger currentPage;

@Property (nonatomic, retain) UITapGestureRecognizer *tapRecognizer;
@Property (nonatomic, strong) UIPanGestureRecognizer *panRecognizer;

@Property (nonatomic) NSInteger fixPage;

@Property (nonatomic,assign) BOOL disabled;

  • (void) setCurrentPage:(NSInteger) value animated:(BOOL) animated;

@EnD

#import "AFKPageFlipper.h"

#pragma mark -
#pragma mark UIView helpers

@interface UIView(Extended)

  • (UIImage *) imageByRenderingView;

@EnD

@implementation UIView(Extended)

  • (UIImage *) imageByRenderingView {
    CGFloat oldAlpha = self.alpha;
    self.alpha = 1.0f;
    UIGraphicsBeginImageContext(self.bounds.size);
    if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    else
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.alpha = oldAlpha;
    return resultingImage;
    }

@EnD

#pragma mark -
#pragma mark Private interface

@interface AFKPageFlipper()

@Property (nonatomic,retain) UIView *currentView;
@Property (nonatomic,retain) UIView *nextView;

@EnD

@implementation AFKPageFlipper

@synthesize tapRecognizer = _tapRecognizer;
@synthesize panRecognizer = _panRecognizer;

#pragma mark -
#pragma mark Flip functionality

  • (void) initFlip {

    // Create screenshots of view
    UIImage *currentImage = [self.currentView imageByRenderingView];
    UIImage *newImage = [self.nextView imageByRenderingView];

    // Hide existing views

    self.currentView.alpha = 0;
    self.nextView.alpha = 0;

    // Create representational layers

    CGRect rect = self.bounds;
    rect.size.width /= 2;

    backgroundAnimationLayer = [CALayer layer];
    backgroundAnimationLayer.frame = self.bounds;
    backgroundAnimationLayer.zPosition = -300000;

    CALayer *leftLayer = [CALayer layer];
    leftLayer.frame = rect;
    leftLayer.masksToBounds = YES;
    leftLayer.contentsGravity = kCAGravityLeft;

    [backgroundAnimationLayer addSublayer:leftLayer];

    rect.origin.x = rect.size.width;

    CALayer *rightLayer = [CALayer layer];
    rightLayer.frame = rect;
    rightLayer.masksToBounds = YES;
    rightLayer.contentsGravity = kCAGravityRight;

    [backgroundAnimationLayer addSublayer:rightLayer];

    if (flipDirection == AFKPageFlipperDirectionRight) {
    leftLayer.contents = (id) [newImage CGImage];
    rightLayer.contents = (id) [currentImage CGImage];
    } else {
    leftLayer.contents = (id) [currentImage CGImage];
    rightLayer.contents = (id) [newImage CGImage];
    }

    [self.layer addSublayer:backgroundAnimationLayer];

    rect.origin.x = 0;

    flipAnimationLayer = [CATransformLayer layer];
    flipAnimationLayer.anchorPoint = CGPointMake(1.0, 0.5);
    flipAnimationLayer.frame = rect;

    [self.layer addSublayer:flipAnimationLayer];

    CALayer *backLayer = [CALayer layer];
    backLayer.frame = flipAnimationLayer.bounds;
    backLayer.doubleSided = NO;
    backLayer.masksToBounds = YES;

    [flipAnimationLayer addSublayer:backLayer];

    CALayer *frontLayer = [CALayer layer];
    frontLayer.frame = flipAnimationLayer.bounds;
    frontLayer.doubleSided = NO;
    frontLayer.masksToBounds = YES;
    frontLayer.transform = CATransform3DMakeRotation(M_PI, 0, 1.0, 0);

    [flipAnimationLayer addSublayer:frontLayer];

    // shadows
    frontLayerShadow = [CALayer layer];
    frontLayerShadow.frame = backLayer.bounds;
    frontLayerShadow.doubleSided = NO;
    frontLayerShadow.masksToBounds = YES;
    frontLayerShadow.opacity = 0;
    frontLayerShadow.backgroundColor = [UIColor blackColor].CGColor;
    [frontLayer addSublayer:frontLayerShadow];

    backLayerShadow = [CALayer layer];
    backLayerShadow.frame = frontLayer.bounds;
    backLayerShadow.doubleSided = NO;
    backLayerShadow.masksToBounds = YES;
    backLayerShadow.opacity = 0;
    backLayerShadow.backgroundColor = [UIColor blackColor].CGColor;
    [backLayer addSublayer:backLayerShadow];

    leftLayerShadow = [CALayer layer];
    leftLayerShadow.frame = rightLayer.bounds;
    leftLayerShadow.doubleSided = NO;
    leftLayerShadow.masksToBounds = YES;
    leftLayerShadow.opacity = 0.0;
    leftLayerShadow.backgroundColor = [UIColor blackColor].CGColor;
    [leftLayer addSublayer:leftLayerShadow];

    rightLayerShadow = [CALayer layer];
    rightLayerShadow.frame = leftLayer.bounds;
    rightLayerShadow.doubleSided = NO;
    rightLayerShadow.masksToBounds = YES;
    rightLayerShadow.opacity = 0.0;
    rightLayerShadow.backgroundColor = [UIColor blackColor].CGColor;
    [rightLayer addSublayer:rightLayerShadow];
    // shadows

    if (flipDirection == AFKPageFlipperDirectionRight) {
    backLayer.contents = (id) [currentImage CGImage];
    backLayer.contentsGravity = kCAGravityLeft;

    frontLayer.contents = (id) [newImage CGImage];
    frontLayer.contentsGravity = kCAGravityRight;
    
    CATransform3D transform = CATransform3DMakeRotation(0.0, 0.0, 1.0, 0.0);
    transform.m34 = 1.0f / 2500.0f;
    
    flipAnimationLayer.transform = transform;
    
    currentAngle = startFlipAngle = 0;
    endFlipAngle = -M_PI;
    

    } else {
    backLayer.contentsGravity = kCAGravityLeft;
    backLayer.contents = (id) [newImage CGImage];

    frontLayer.contents = (id) [currentImage CGImage];
    frontLayer.contentsGravity = kCAGravityRight;
    
    CATransform3D transform = CATransform3DMakeRotation(-M_PI / 1.1, 0.0, 1.0, 0.0);
    transform.m34 = 1.0f / 2500.0f;
    
    flipAnimationLayer.transform = transform;
    
    currentAngle = startFlipAngle = -M_PI;
    endFlipAngle = 0;
    

    }
    }

  • (void) cleanupFlip {
    [backgroundAnimationLayer removeFromSuperlayer];
    [flipAnimationLayer removeFromSuperlayer];

    backgroundAnimationLayer = Nil;
    flipAnimationLayer = Nil;

    animating = NO;

    if (setNextViewOnCompletion && self.nextView.superview) {
    [self.currentView removeFromSuperview];
    self.currentView = self.nextView;
    self.nextView = Nil;
    } else {
    [self.nextView removeFromSuperview];
    self.nextView = Nil;
    }

    self.currentView.alpha = 1;

    if (!self.currentView.superview) {
    [self addSubview:self.currentView];
    }
    self.userInteractionEnabled = YES;
    self.disabled = NO; //Prevent black page

}

  • (void) setFlipProgress:(float) progress setDelegate:(BOOL) setDelegate animate:(BOOL) animate {
    if (animate) {
    animating = YES;
    }

    float newAngle = startFlipAngle + progress * (endFlipAngle - startFlipAngle);

    float duration = animate ? 0.5 * fabs((newAngle - currentAngle) / (endFlipAngle - startFlipAngle)) : 0;

    currentAngle = newAngle;

    CATransform3D endTransform = CATransform3DIdentity;
    endTransform.m34 = 1.0f / 2500.0f;
    endTransform = CATransform3DRotate(endTransform, newAngle, 0.0, 1.0, 0.0);

    [flipAnimationLayer removeAllAnimations];

    // shadows
    CGFloat newShadowOpacity = progress;
    if (endFlipAngle > startFlipAngle) newShadowOpacity = 1.0 - progress;
    // shadows

    if (duration < 0.09) {
    duration = 0.09;
    }
    [UIView beginAnimations:@"FLIP1" context:nil];
    [UIView setAnimationDuration:duration];
    flipAnimationLayer.transform = endTransform;

    // shadows
    frontLayerShadow.opacity = 1.0 - newShadowOpacity;
    backLayerShadow.opacity = newShadowOpacity;
    leftLayerShadow.opacity = 0.5 - newShadowOpacity;
    rightLayerShadow.opacity = newShadowOpacity - 0.5;
    // shadows
    [UIView commitAnimations];

    if (setDelegate) {
    [self performSelector:@selector(cleanupFlip) withObject:Nil afterDelay:duration];
    }
    }

  • (void) flipPage {
    [self setFlipProgress:1.0 setDelegate:YES animate:YES];
    }

#pragma mark -
#pragma mark Animation management

  • (void)animationDidStop:(NSString *) animationID finished:(NSNumber *) finished context:(void *) context {
    [self cleanupFlip];
    }

#pragma mark -
#pragma mark Properties

@synthesize currentPage;

  • (BOOL) doSetCurrentPage:(NSInteger) value {
    if (value == currentPage) {
    return FALSE;
    }

    flipDirection = value < currentPage ? AFKPageFlipperDirectionRight : AFKPageFlipperDirectionLeft;

    currentPage = value;

    self.nextView = [self.dataSource viewForPage:value inFlipper:self];
    [self addSubview:self.nextView];

    return TRUE;
    }

  • (void) setCurrentPage:(NSInteger) value {
    if (![self doSetCurrentPage:value]) {
    return;
    }

    setNextViewOnCompletion = YES;
    animating = YES;

    self.nextView.alpha = 0;

    [UIView beginAnimations:@"" context:Nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    self.nextView.alpha = 1;
    [UIView commitAnimations];
    }

  • (void) setCurrentPage:(NSInteger) value animated:(BOOL) animated {
    if (![self doSetCurrentPage:value]) {
    return;
    }

    setNextViewOnCompletion = YES;
    animating = YES;

    if (animated) {
    [self initFlip];
    [self performSelector:@selector(flipPage) withObject:Nil afterDelay:0.001];
    } else {
    [self animationDidStop:Nil finished:[NSNumber numberWithBool:NO] context:Nil];
    }

}

@synthesize dataSource;

  • (void) setDataSource:(NSObject *) value {
    if (dataSource) {
    dataSource = nil;
    }

    dataSource = value;
    numberOfPages = [dataSource numberOfPagesForPageFlipper:self];
    currentPage = 0;
    self.currentPage = 1;
    }

@synthesize disabled;

  • (void) setDisabled:(BOOL) value {
    disabled = value;

    self.userInteractionEnabled = !value;

    for (UIGestureRecognizer *recognizer in self.gestureRecognizers) {
    recognizer.enabled = !value;
    }
    }

#pragma mark -
#pragma mark Touch management

  • (void) tapped:(UITapGestureRecognizer *) recognizer {
    if (animating || self.disabled) {
    return;
    }

    if (recognizer.state == UIGestureRecognizerStateRecognized) {
    NSInteger newPage;

    if ([recognizer locationInView:self].x < (self.bounds.size.width - self.bounds.origin.x) / 2) {
        newPage = MAX(1, self.currentPage - 1);
    } else {
        newPage = MIN(self.currentPage + 1, numberOfPages);
    }
    
    [self setCurrentPage:newPage animated:YES];
    

    }
    }

  • (void) panned:(UIPanGestureRecognizer *) recognizer {

    if (animating) {
    return;
    }

    static BOOL hasFailed;
    static BOOL initialized;

    static NSInteger oldPage;

    float translation = [recognizer translationInView:self].x;
    float progress = translation / self.bounds.size.width;

    if (flipDirection == AFKPageFlipperDirectionLeft) {
    progress = MIN(progress, 0);
    } else {
    progress = MAX(progress, 0);
    }

    switch (recognizer.state) {
    case UIGestureRecognizerStateBegan:
    hasFailed = FALSE;
    initialized = FALSE;
    animating = NO;
    setNextViewOnCompletion = NO;
    break;

    case UIGestureRecognizerStateChanged:
    
        if (hasFailed) {
            return;
        }
        //NSLog([NSString stringWithFormat:@"TranslationX: %f velocidad: %f progreso: %f Widgth: %f", translation ,[recognizer velocityInView:self].x , progress , self.bounds.size.width]);
    
        if (!initialized) {
            oldPage = self.currentPage;
    
            if (translation > 0) {
                if (self.currentPage > 1) {
                    [self doSetCurrentPage:self.currentPage - 1];
                } else {
                    hasFailed = TRUE;
                    return;
                }
            } else {
                if (self.currentPage < numberOfPages) {
                    [self doSetCurrentPage:self.currentPage + 1];
                } else {
                    hasFailed = TRUE;
                    return;
                }
            }
    
            hasFailed = NO;
            initialized = TRUE;
            setNextViewOnCompletion = NO;
    
            [self initFlip];
        }
        [self setFlipProgress:fabs(progress) setDelegate:NO animate:NO];
    
        break;
    
    
    case UIGestureRecognizerStateFailed:
        [self setFlipProgress:0.0 setDelegate:YES animate:YES];
        currentPage = oldPage;
        break;
    
    case UIGestureRecognizerStateRecognized:
        if (hasFailed) {
            [self setFlipProgress:0.0 setDelegate:YES animate:YES];
            currentPage = oldPage;
            //NSLog(@"recognized - fail");
            return;
        }
    
        if (fabs((translation + [recognizer velocityInView:self].x / 4) / self.bounds.size.width) > 0.1) {
            setNextViewOnCompletion = YES;
            //NSLog([NSString stringWithFormat:@"%d , currentPage: %d fixPage: %d",numberOfPages, currentPage , _fixPage]);
            _fixPage = currentPage;
            [self setFlipProgress:1.0 setDelegate:YES animate:YES];
           // NSLog(@"bug");
    
            return;
        } else {
            [self setFlipProgress:0.0 setDelegate:YES animate:YES];
            currentPage = oldPage;
            return;
        }
    
        break;
    default:
        [self setFlipProgress:0.0 setDelegate:YES animate:YES];
        currentPage = oldPage;
        return;
    
        break;
    

    }
    }

#pragma mark -
#pragma mark Frame management

/*

  • (void) setFrame:(CGRect) value {
    super.frame = value;

    numberOfPages = [dataSource numberOfPagesForPageFlipper:self];

    if (self.currentPage > numberOfPages) {
    self.currentPage = numberOfPages;
    }

    }*/

#pragma mark -
#pragma mark Initialization and memory management

  • (Class) layerClass {
    return [CATransformLayer class];
    }

  • (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
    [self initRecognizers];
    }
    return self;
    }

  • (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder: aDecoder])) {
    [self initRecognizers];
    }
    return self;
    }

  • (void) initRecognizers {
    //_tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
    _panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)];

    //[_tapRecognizer requireGestureRecognizerToFail:_panRecognizer];

    //[self addGestureRecognizer:_tapRecognizer];
    [self addGestureRecognizer:_panRecognizer];
    }

@EnD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants