[Objective-C] Swipe… the same view

Easy way to apply swipe effect to the same view.

Like this:

How to, add QuartzCore framework and use CATransition to animate!

Add the UISwipeGestureRecognizer to your swipeable view:

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (void)viewDidLoad {
[super viewDidLoad];

// swipe L/R
UISwipeGestureRecognizer *swipeL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
UISwipeGestureRecognizer *swipeR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
swipeL.direction = UISwipeGestureRecognizerDirectionLeft;
swipeR.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeL];
[self.view addGestureRecognizer:swipeR];
}[/code]

and exchange the views when it’s reconized.

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (void) swipeRecognized:(UISwipeGestureRecognizer*)sw
{
CATransition *animation  = [CATransition animation];
animation.duration       = 0.15f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type           = kCATransitionPush;
animation.subtype        = sw.direction == UISwipeGestureRecognizerDirectionLeft ? kCATransitionFromRight : kCATransitionFromLeft;

[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[self.view.layer addAnimation:animation forKey:@"animation"];
}[/code]

Very simple!
enjoy swipe.

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.