Powered By Blogger
Showing posts with label UIGestureRecognizerDelegate. Show all posts
Showing posts with label UIGestureRecognizerDelegate. Show all posts

Saturday, July 20, 2013

Custom switch in iPhone

Hi,
In my recent project, I need to make a custom switch having different colors depending upon theme selection.
Now , I have a solution for that... Make two class files with extension ".h" & ".m". Original Content is available at  HERE. I have made some modifications to the original contents as per my requirements.

SECTION 1 :
 -------------------------------------------------

#import <UIKit/UIKit.h>

@interface CustomSwitch : UIControl

@property(nonatomic, retain) UIColor *tintColor;
@property(nonatomic, retain) UIColor *onTintColor;
@property(nonatomic, assign) UIColor *offTintColor;
@property(nonatomic, assign) UIColor *thumbTintColor;
@property(nonatomic,getter=isOn) BOOL on;


- (id)initWithFrame:(CGRect)frame;
- (void)setOn:(BOOL)on animated:(BOOL)animated;

@end


SECTION 2 :
  -------------------------------------------------

#import "CustomSwitch.h"
#import <QuartzCore/QuartzCore.h>


@interface CustomSwitch () <UIGestureRecognizerDelegate> 

{
    CAShapeLayer *_thumbLayer;
    CAShapeLayer *_fillLayer;
    CAShapeLayer *_backLayer;
    BOOL _dragging;
    BOOL _on;
}
@property (nonatomic, assign) BOOL pressed;
- (void) setBackgroundOn:(BOOL)on animated:(BOOL)animated;
- (void) showFillLayer:(BOOL)show animated:(BOOL)animated;
- (CGRect) thumbFrameForState:(BOOL)isOn;
@end

@implementation
CustomSwitch

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

- (void) awakeFromNib {
    [self configure];
}

- (void) configure {
    //Check width > height
    if (self.frame.size.height > self.frame.size.width*0.65) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, ceilf(0.6*self.frame.size.width));
    }
   
    [self setBackgroundColor:[UIColor clearColor]];
    self.onTintColor = [UIColor colorWithRed:0.27f green:0.85f blue:0.37f alpha:1.00f];
    self.tintColor = [UIColor colorWithRed:0.90f green:0.90f blue:0.90f alpha:1.00f];
    _on = NO;
    _pressed = NO;
    _dragging = NO;
   
   
    _backLayer = [[CAShapeLayer layer] retain];
    _backLayer.backgroundColor = [[UIColor clearColor] CGColor];
    _backLayer.frame = self.bounds;
    _backLayer.cornerRadius = self.bounds.size.height/2.0;
    CGPathRef path1 = [UIBezierPath bezierPathWithRoundedRect:_backLayer.bounds cornerRadius:floorf(_backLayer.bounds.size.height/2.0)].CGPath;
    _backLayer.path = path1;
    [_backLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isOn"];
    _backLayer.fillColor = [_tintColor CGColor];
    [self.layer addSublayer:_backLayer];
   
    _fillLayer = [[CAShapeLayer layer] retain];
    _fillLayer.backgroundColor = [[UIColor clearColor] CGColor];
    _fillLayer.frame = CGRectInset(self.bounds, 1.5, 1.5);
    CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:_fillLayer.bounds cornerRadius:floorf(_fillLayer.bounds.size.height/2.0)].CGPath;
    _fillLayer.path = path;
    [_fillLayer setValue:[NSNumber numberWithBool:YES] forKey:@"isVisible"];
    _fillLayer.fillColor = [[UIColor whiteColor] CGColor];
    [self.layer addSublayer:_fillLayer];
   
   
    _thumbLayer = [[CAShapeLayer layer] retain];
    _thumbLayer.backgroundColor = [[UIColor clearColor] CGColor];
    _thumbLayer.frame = CGRectMake(1.0, 1.0, self.bounds.size.height-2.0, self.bounds.size.height-2.0);
    _thumbLayer.cornerRadius = self.bounds.size.height/2.0;
    CGPathRef knobPath = [UIBezierPath bezierPathWithRoundedRect:_thumbLayer.bounds cornerRadius:floorf(_thumbLayer.bounds.size.height/2.0)].CGPath;
    _thumbLayer.path = knobPath;
    _thumbLayer.fillColor = [UIColor whiteColor].CGColor;
    _thumbLayer.shadowColor = [UIColor blackColor].CGColor;
    _thumbLayer.shadowOffset = CGSizeMake(0.0, 2.0);
    _thumbLayer.shadowRadius = 3.0;
    _thumbLayer.shadowOpacity = 0.1;
    [self.layer addSublayer:_thumbLayer];
   
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                            action:@selector(tapped:)];
    [tapGestureRecognizer setDelegate:self];
    [self addGestureRecognizer:tapGestureRecognizer];
   
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                            action:@selector(toggleDragged:)];
    //[panGestureRecognizer requireGestureRecognizerToFail:tapGestureRecognizer];
    [panGestureRecognizer setDelegate:self];
    [self addGestureRecognizer:panGestureRecognizer];
   
    [tapGestureRecognizer release];
    [panGestureRecognizer release];
}

#pragma mark -
#pragma mark Animations


- (BOOL) isOn {
    return _on;
}

- (void) setOn:(BOOL)on {
    [self setOn:on animated:NO];
}

- (void)setOn:(BOOL)on animated:(BOOL)animated {
   
    if (_on != on) {
        _on = on;
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
    if (animated) {
        [CATransaction begin];
        [CATransaction setAnimationDuration:0.3];
        [CATransaction setDisableActions:NO];
        _thumbLayer.frame = [self thumbFrameForState:_on];
        [CATransaction commit];
    }else {
        [CATransaction setDisableActions:YES];
        _thumbLayer.frame = [self thumbFrameForState:_on];
    }
    [self setBackgroundOn:_on animated:animated];
    [self showFillLayer:!_on animated:animated];
}

- (void) setBackgroundOn:(BOOL)on animated:(BOOL)animated {
    BOOL isOn = [[_backLayer valueForKey:@"isOn"] boolValue];
    if (on != isOn) {
        [_backLayer setValue:[NSNumber numberWithBool:on] forKey:@"isOn"];
        if (animated) {
            CABasicAnimation *animateColor = [CABasicAnimation animationWithKeyPath:@"fillColor"];
            animateColor.duration = 0.22;
            animateColor.fromValue = on ? (id)_tintColor.CGColor : (id)_onTintColor.CGColor;
            animateColor.toValue = on ? (id)_onTintColor.CGColor : (id)_tintColor.CGColor;
            animateColor.removedOnCompletion = NO;
            animateColor.fillMode = kCAFillModeForwards;
            [_backLayer addAnimation:animateColor forKey:@"animateColor"];
            [CATransaction commit];
        }else {
            [_backLayer removeAllAnimations];
            _backLayer.fillColor = on ? _onTintColor.CGColor : _tintColor.CGColor;
        }
    }
}

- (void) showFillLayer:(BOOL)show animated:(BOOL)animated {
    BOOL isVisible = [[_fillLayer valueForKey:@"isVisible"] boolValue];
    if (isVisible != show) {
        [_fillLayer setValue:[NSNumber numberWithBool:show] forKey:@"isVisible"];
        CGFloat scale = show ? 1.0 : 0.0;
        if (animated) {
            CGFloat from = show ? 0.0 : 1.0;
            CABasicAnimation *animateScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            animateScale.duration = 0.22;
            animateScale.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(from, from, 1.0)];
            animateScale.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(scale, scale, 1.0)];
            animateScale.removedOnCompletion = NO;
            animateScale.fillMode = kCAFillModeForwards;
            animateScale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [_fillLayer addAnimation:animateScale forKey:@"animateScale"];
        }else {
            [_fillLayer removeAllAnimations];
            _fillLayer.transform = CATransform3DMakeScale(scale,scale,1.0);
        }
    }
}

- (void) setPressed:(BOOL)pressed {
    if (_pressed != pressed) {
        _pressed = pressed;
       
        if (!_on) {
            [self showFillLayer:!_pressed animated:YES];
        }
    }
}

#pragma mark -
#pragma mark Appearance


- (void) setTintColor:(UIColor *)tintColor {
    _tintColor = [tintColor retain];
    if (![[_backLayer valueForKey:@"isOn"] boolValue]) {
        _backLayer.fillColor = [_tintColor CGColor];
    }
}

- (void) setOnTintColor:(UIColor *)onTintColor {
    _onTintColor = [onTintColor retain];
    if ([[_backLayer valueForKey:@"isOn"] boolValue]) {
        _backLayer.fillColor = [_onTintColor CGColor];
    }
}

- (void) setOffTintColor:(UIColor *)offTintColor {
    _fillLayer.fillColor = [offTintColor CGColor];
}

- (UIColor *) offTintColor {
    return [UIColor colorWithCGColor:_fillLayer.fillColor];
}

- (void) setThumbTintColor:(UIColor *)thumbTintColor {
    _thumbLayer.fillColor = [thumbTintColor CGColor];
}

- (UIColor *) thumbTintColor {
    return [UIColor colorWithCGColor:_thumbLayer.fillColor];
}

#pragma mark -
#pragma mark Interaction


- (void)tapped:(UITapGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateEnded)
        [self setOn:!self.on animated:YES];
}

- (void)toggleDragged:(UIPanGestureRecognizer *)gesture
{
    CGFloat minToggleX = 1.0;
    CGFloat maxToggleX = self.bounds.size.width-self.bounds.size.height+1.0;
   
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        self.pressed = YES;
        _dragging = YES;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [gesture translationInView:self];
       
        [CATransaction setDisableActions:YES];
       
        self.pressed = YES;
       
        CGFloat newX = _thumbLayer.frame.origin.x + translation.x;
        if (newX < minToggleX) newX = minToggleX;
        if (newX > maxToggleX) newX = maxToggleX;
        _thumbLayer.frame = CGRectMake(newX,
                                       _thumbLayer.frame.origin.y,
                                       _thumbLayer.frame.size.width,
                                       _thumbLayer.frame.size.height);
       
        if (CGRectGetMidX(_thumbLayer.frame) > CGRectGetMidX(self.bounds)
            && ![[_backLayer valueForKey:@"isOn"] boolValue]) {
            [self setBackgroundOn:YES animated:YES];
        }else if (CGRectGetMidX(_thumbLayer.frame) < CGRectGetMidX(self.bounds)
                  && [[_backLayer valueForKey:@"isOn"] boolValue]){
            [self setBackgroundOn:NO animated:YES];
        }
       
       
        [gesture setTranslation:CGPointZero inView:self];
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        CGFloat toggleCenter = CGRectGetMidX(_thumbLayer.frame);
        [self setOn:(toggleCenter > CGRectGetMidX(self.bounds)) animated:YES];
        _dragging = NO;
        self.pressed = NO;
    }
   
    CGPoint locationOfTouch = [gesture locationInView:self];
    if (CGRectContainsPoint(self.bounds, locationOfTouch))
        [self sendActionsForControlEvents:UIControlEventTouchDragInside];
    else
        [self sendActionsForControlEvents:UIControlEventTouchDragOutside];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
   
    self.pressed = YES;
   
    [self sendActionsForControlEvents:UIControlEventTouchDown];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    if (!_dragging) {
        self.pressed = NO;
    }
    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    if (!_dragging) {
        self.pressed = NO;
    }
    [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
}

#pragma mark -
#pragma mark Thumb Frame


- (CGRect) thumbFrameForState:(BOOL)isOn {
    return CGRectMake(isOn ? self.bounds.size.width-self.bounds.size.height+1.0 : 1.0,
                      1.0,
                      self.bounds.size.height-2.0,
                      self.bounds.size.height-2.0);
}

#pragma mark -
#pragma mark Dealloc

- (void) dealloc {
    [_tintColor release], _tintColor = nil;
    [_onTintColor release], _onTintColor = nil;
   
    [_thumbLayer release], _thumbLayer = nil;
    [_fillLayer release], _fillLayer = nil;
    [_backLayer release], _backLayer = nil;
    [super dealloc];
}

@end
 

Thanks ,
Nilesh M. Prajapati

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