Powered By Blogger

Thursday, September 27, 2012

UIPanGestureRecognizer scrolling example for iOS

Hi everyone,
Today, I'm giving you the source code of UIPanGestureRecognizer for iPhone.



Follow 2-3 simple steps as given below:

1) Put below code into your .header file of any viewcontroller class.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
{
    IBOutlet UIView *viewOrange;
    IBOutlet UIView *viewBrown;
    IBOutlet UIView *viewPurple;
}
@property(nonatomic,retain) IBOutlet UIView *viewOrange;
@property(nonatomic,retain) IBOutlet UIView *viewBrown;
@property(nonatomic,retain) IBOutlet UIView *viewPurple;

@end

2) Put below code into your .m file of same viewcontroller class.

#import "ViewController.h"

@implementation ViewController
@synthesize viewOrange;
@synthesize viewBrown;
@synthesize viewPurple;

CGPoint translatedPoint;
NSInteger _firstX;
NSInteger _firstY;
NSInteger numberofView;
CGSize viewSize;
float currentView;

BOOL isBegan;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
  
    numberofView = 3;
    viewSize = CGSizeMake(320.0, 480.0);
   
    viewOrange.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);
    viewPurple.frame = CGRectMake(320.0, 0.0, 320.0, 480.0);
    viewBrown.frame = CGRectMake(640.0, 0.0, 320.0, 480.0);
   
    UISwipeGestureRecognizer *swipegestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
    [swipegestureRecognizer setDelegate:self];
    [self.view addGestureRecognizer:swipegestureRecognizer];
    [swipegestureRecognizer release];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


-(IBAction)handleSwipe:(UIPanGestureRecognizer *)sender
{
    translatedPoint = [sender translationInView:self.view];

        if([sender state] == UIGestureRecognizerStateBegan)
        {
            if (self.viewOrange.frame.origin.x >= -((numberofView-1)*viewSize.width) && self.viewOrange.frame.origin.x <= 0.0)
            {
                _firstX = self.viewOrange.frame.origin.x;
                _firstY = self.viewOrange.frame.origin.y;
               
                if (viewOrange.frame.origin.x==0) {
                    currentView=0;
                }else if (viewPurple.frame.origin.x==0) {
                    currentView=1;
                }else if (viewBrown.frame.origin.x==0) {
                    currentView=2;
                }

            }
        }
        else if([sender state] == UIGestureRecognizerStateChanged)
        {
            if ([sender velocityInView:self.view].x >= 600.0)
            {
                if (!isBegan)
                {
                    if (self.viewOrange.frame.origin.x < 0.0)
                    {
                        isBegan = YES;
                        NSLog(@"Velocity Changed.");
                        [UIView beginAnimations:nil context:NULL];
                        [UIView setAnimationDuration:0.2];
                        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                        [viewOrange setFrame:CGRectMake(viewOrange.frame.origin.x + viewOrange.frame.size.width, 0.0, viewOrange.frame.size.width, viewOrange.frame.size.height)];
                        viewPurple.frame = CGRectMake(viewOrange.frame.origin.x + viewOrange.frame.size.width, 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                        viewBrown.frame = CGRectMake(viewPurple.frame.origin.x + viewPurple.frame.size.width, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                        [UIView commitAnimations];
                    }
                }
            }
            else if ([sender velocityInView:self.view].x <= -600.0)
            {
                if (!isBegan)
                {
                    if (self.viewOrange.frame.origin.x > -((numberofView-1)*viewSize.width))
                    {
                        isBegan = YES;
                        NSLog(@"Velocity Changed.");
                        [UIView beginAnimations:nil context:NULL];
                        [UIView setAnimationDuration:0.2];
                        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                        [viewOrange setFrame:CGRectMake(viewOrange.frame.origin.x - viewOrange.frame.size.width, 0.0, viewOrange.frame.size.width, viewOrange.frame.size.height)];
                        viewPurple.frame = CGRectMake(viewOrange.frame.origin.x + viewOrange.frame.size.width, 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                        viewBrown.frame = CGRectMake(viewPurple.frame.origin.x + viewPurple.frame.size.width, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                        [UIView commitAnimations];
                    }
                }
            }
            else
            {
                if (self.viewOrange.frame.origin.x >= -((numberofView-1)*viewSize.width) && self.viewOrange.frame.origin.x <= 0.0)
                {
                    NSLog(@"State Changed.");
                    [UIView beginAnimations:nil context:NULL];
                    [UIView setAnimationDuration:0.2];
                    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                    translatedPoint = CGPointMake(_firstX + translatedPoint.x, 0.0);
                    [viewOrange setFrame:CGRectMake(translatedPoint.x, 0.0, viewOrange.frame.size.width, viewOrange.frame.size.height)];
                    viewPurple.frame = CGRectMake(viewOrange.frame.origin.x + viewOrange.frame.size.width, 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                    viewBrown.frame = CGRectMake(viewPurple.frame.origin.x + viewPurple.frame.size.width, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                    [UIView commitAnimations];
                }
            }
        }
        else if([sender state] == UIGestureRecognizerStateEnded)
        {
            NSLog(@"State Ended On That ");
            if (self.viewOrange.frame.origin.x >= -((numberofView-1)*viewSize.width) && self.viewOrange.frame.origin.x <= 0.0)
            {
                NSLog(@"%f",viewOrange.frame.origin.x);
                NSLog(@"%f",viewPurple.frame.origin.x);
                NSLog(@"%f",viewBrown.frame.origin.x);
                if (currentView==0) {
                    if (viewOrange.frame.origin.x<0 ){
                        if (viewOrange.frame.origin.x<-160) {
                            [UIView beginAnimations:nil context:NULL];
                            [UIView setAnimationDuration:0.2];
                            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                            [viewOrange setFrame:CGRectMake(-320, 0.0, viewOrange.frame.size.width, viewOrange.frame.size.height)];
                            viewPurple.frame = CGRectMake(viewOrange.frame.size.width+viewOrange.frame.origin.x , 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                            [UIView commitAnimations];
                        }else{
                           
                            [UIView beginAnimations:nil context:NULL];
                            [UIView setAnimationDuration:0.2];
                            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                            [viewOrange setFrame:CGRectMake(0.0, 0.0, viewOrange.frame.size.width, viewOrange.frame.size.height)];
                            viewPurple.frame = CGRectMake(320, 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                            [UIView commitAnimations];
                        }
                    }
                }else if(currentView==1){
                    if (viewPurple.frame.origin.x<0){
                        if (viewPurple.frame.origin.x<-160) {
                           
                            [UIView beginAnimations:nil context:NULL];
                            [UIView setAnimationDuration:0.2];
                            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                            [viewOrange setFrame:CGRectMake(-640, 0.0, viewOrange.frame.size.width,  viewOrange.frame.size.height)];
                            viewPurple.frame = CGRectMake(-320 , 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                            viewBrown.frame = CGRectMake(0.0, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                            [UIView commitAnimations];
                           
                          

                        }else {
                            [UIView beginAnimations:nil context:NULL];
                            [UIView setAnimationDuration:0.2];
                            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                            [viewOrange setFrame:CGRectMake(-320.0, 0.0, viewOrange.frame.size.width,  viewOrange.frame.size.height)];
                            viewPurple.frame = CGRectMake(0.0 , 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                            viewBrown.frame = CGRectMake(320.0, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                            [UIView commitAnimations];
                        }
                    }else{
                         if (viewPurple.frame.origin.x>160) {
                             [UIView beginAnimations:nil context:NULL];
                             [UIView setAnimationDuration:0.2];
                             [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                             [viewOrange setFrame:CGRectMake(0.0, 0.0, viewOrange.frame.size.width, viewOrange.frame.size.height)];
                             viewPurple.frame = CGRectMake(320 , 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                             viewBrown.frame = CGRectMake(640.0, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                             [UIView commitAnimations];

                         }else{
                            
                             [UIView beginAnimations:nil context:NULL];
                             [UIView setAnimationDuration:0.2];
                             [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                             [viewOrange setFrame:CGRectMake(-320.0, 0.0, viewOrange.frame.size.width,  viewOrange.frame.size.height)];
                             viewPurple.frame = CGRectMake(0.0 , 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                             viewBrown.frame = CGRectMake(320.0, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                             [UIView commitAnimations];
                            
                        }
                    }
                }else if(currentView==2){
                    if (viewBrown.frame.origin.x>0){
                       
                        if (viewBrown.frame.origin.x>160) {
                            [UIView beginAnimations:nil context:NULL];
                            [UIView setAnimationDuration:0.2];
                            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                            [viewOrange setFrame:CGRectMake(-320.0, 0.0, viewOrange.frame.size.width, viewOrange.frame.size.height)];
                            viewPurple.frame = CGRectMake(0.0 , 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                            viewBrown.frame = CGRectMake(320.0, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                            [UIView commitAnimations];
                        }else{
                            [UIView beginAnimations:nil context:NULL];
                            [UIView setAnimationDuration:0.2];
                            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                            [viewOrange setFrame:CGRectMake(-640, 0.0, viewOrange.frame.size.width,  viewOrange.frame.size.height)];
                            viewPurple.frame = CGRectMake(-320 , 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                            viewBrown.frame = CGRectMake(0.0, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                            [UIView commitAnimations];
                           
                        }
                      
                    }
                }
            }
            isBegan = NO;
            if (self.viewOrange.frame.origin.x <-((numberofView-1)*viewSize.width))
            {
                NSLog(@"State Changed.");
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:0.2];
                [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                [viewOrange setFrame:CGRectMake(-640.0, 0.0, viewOrange.frame.size.width, viewOrange.frame.size.height)];
                viewPurple.frame = CGRectMake(viewOrange.frame.origin.x + viewOrange.frame.size.width, 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                viewBrown.frame = CGRectMake(viewPurple.frame.origin.x + viewPurple.frame.size.width, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                [UIView commitAnimations];
            }
            else if (self.viewOrange.frame.origin.x > 0)
            {
                NSLog(@"State Changed.");
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:0.2];
                [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                [viewOrange setFrame:CGRectMake(0.0, 0.0, viewOrange.frame.size.width, viewOrange.frame.size.height)];
                viewPurple.frame = CGRectMake(viewOrange.frame.origin.x + viewOrange.frame.size.width, 0.0, viewPurple.frame.size.width, viewPurple.frame.size.height);
                viewBrown.frame = CGRectMake(viewPurple.frame.origin.x + viewPurple.frame.size.width, 0.0, viewBrown.frame.size.width, viewBrown.frame.size.height);
                [UIView commitAnimations];
            }
        }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end

 Thanks,
Nilesh Prajapati

No comments:

Post a Comment