All Gesture
TAP Gesture :
1 ) First Set Image In Imageview in
The Storyboard :
2) Write Following Code in ViewController.h :
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UIImageView *imgobj;
UITapGestureRecognizer *tapgestureobj;
}
@property(strong,nonatomic)IBOutlet UIImageView *imgobj;
@property(strong,nonatomic)IBOutlet UITapGestureRecognizer *tapgestureobj;
@end
3) Write Following Code in ViewController.m :
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize imgobj,tapgestureobj;
- (void)viewDidLoad
{
[super viewDidLoad];
self.tapgestureobj = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapselect:)];
[tapgestureobj setNumberOfTouchesRequired:1];
[tapgestureobj setNumberOfTapsRequired:1];
[self.imgobj addGestureRecognizer:self.tapgestureobj];
imgobj.userInteractionEnabled = YES;
}
-(void)tapselect : (UITapGestureRecognizer *)recognizer
{
if (recognizer.view.contentMode == UIViewContentModeScaleToFill)
{
recognizer.view.contentMode = UIViewContentModeCenter;
}
else
{
recognizer.view.contentMode = UIViewContentModeScaleToFill;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Swipe Gesture :
1 ) First Set Three View With Different Colour in The Storyboard :
2) Write Following Code in ViewController.h :
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UIView *view1;
UIView *view2;
UIView *view3;
}
@property(strong,nonatomic)IBOutlet UIView *view1;
@property(strong,nonatomic)IBOutlet UIView *view2;
@property(strong,nonatomic)IBOutlet UIView *view3;
@property(strong,nonatomic)UISwipeGestureRecognizer *swipeleftobj;
@property(strong,nonatomic)UISwipeGestureRecognizer *swiperightobj;
@property(strong,nonatomic)UISwipeGestureRecognizer *swipetopobj;
@end
3) Write Following Code in ViewController.m :
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize view1,view2,view3,swipeleftobj,swiperightobj,swipetopobj;
- (void)viewDidLoad
{
[super viewDidLoad];
swipeleftobj = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeleftselect:)];
swipeleftobj.direction = UISwipeGestureRecognizerDirectionLeft;
swiperightobj = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiperightselect:)];
swiperightobj.direction = UISwipeGestureRecognizerDirectionRight;
swipetopobj = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipetopselect:)];
swipetopobj.direction = UISwipeGestureRecognizerDirectionUp;
[self.view1 addGestureRecognizer:swipeleftobj];
[self.view2 addGestureRecognizer:swiperightobj];
[self.view3 addGestureRecognizer:swipetopobj];
}
-(void)swipeleftselect : (UISwipeGestureRecognizer *)recognizer
{
[UIView animateWithDuration:0.5 animations:^{
self.view1.frame = CGRectOffset(self.view1.frame, -320.0, 0.0);
}];
}
-(void)swiperightselect : (UISwipeGestureRecognizer *)recognizer
{
[UIView animateWithDuration:0.5 animations:^{
self.view2.frame = CGRectOffset(self.view2.frame, 320.0, 0.0);
}];
}
-(void)swipetopselect : (UISwipeGestureRecognizer *)recognizer
{
[UIView animateWithDuration:0.5 animations:^{
self.view3.frame = CGRectMake(self.view3.frame.origin.x, -568, self.view3.frame.size.width, self.view3.frame.size.height);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Long Press Gesture :
1 ) First Set One View in The Storyboard :
2) Write Following Code in ViewController.h :
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
{
UIView *view1;
UILongPressGestureRecognizer *longobj;
}
@property(strong,nonatomic)IBOutlet UIView *view1;
@property(strong,nonatomic)UILongPressGestureRecognizer *longobj;
@end
3) Write Following Code in ViewController.m :
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize longobj,view1;
- (void)viewDidLoad
{
[super viewDidLoad];
longobj = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longselect:)];
longobj.minimumPressDuration = 0.3;
longobj.delegate = self ;
[self.view1 addGestureRecognizer:longobj];
}
-(void)longselect : (UILongPressGestureRecognizer *)recognizer
{
if ([recognizer isEqual:self.longobj])
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Gestures" message:@"Long Gesture Detected" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Pinch Gesture :
1 ) First Set Image In Imageview in
The Storyboard :
2) Write Following Code in ViewController.h :
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
{
UIImageView *imgobj;
UIPinchGestureRecognizer *pinchobj;
}
@property(strong,nonatomic)IBOutlet UIImageView *imgobj;
@property(strong,nonatomic)UIPinchGestureRecognizer *pinchobj;
@end
3) Write Following Code in ViewController.m :
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize imgobj,pinchobj;
- (void)viewDidLoad
{
[super viewDidLoad];
self.pinchobj = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchselect:)];
imgobj.userInteractionEnabled = YES;
pinchobj.delegate = self ;
[self.imgobj addGestureRecognizer:self.pinchobj];
}
-(void)pinchselect : (UIPinchGestureRecognizer *)recognizer
{
// self.imgobj.transform = CGAffineTransformScale(self.imgobj.transform, recognizer.scale, recognizer.scale);
CGFloat lastscaler =1;
CGFloat factor =[(UIPinchGestureRecognizer *)recognizer scale];
if (factor >1)
{
imgobj.transform=CGAffineTransformMakeScale
(lastscaler + (factor-1),
lastscaler +(factor-1));
}
else
{
imgobj.transform=CGAffineTransformMakeScale
(lastscaler + (factor),
lastscaler +(factor));
}
if (recognizer.state==UIGestureRecognizerStateEnded)
{
if (factor > 1)
lastscaler+= (factor-1);
else
lastscaler+= factor;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Pen Gesture :
1 ) First Set View and Two Label (Text is null) in The Storyboard :
2) Write Following Code in ViewController.h :
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UIView *view1;
UIPanGestureRecognizer *panobj;
UILabel *lblhori;
UILabel *lblvert;
}
@property(strong,nonatomic)IBOutlet UIView *view1;
@property(strong,nonatomic)UIPanGestureRecognizer *panobj;
@property(strong,nonatomic)IBOutlet UILabel *lblhori;
@property(strong,nonatomic)IBOutlet UILabel *lblvert;
@end
3) Write Following Code in ViewController.m :
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize panobj,view1,lblhori,lblvert;
- (void)viewDidLoad
{
[super viewDidLoad];
panobj = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panselect:)];
[self.view1 addGestureRecognizer:panobj];
}
-(void)panselect:(UIPanGestureRecognizer *)recognizer
{
CGPoint touchLocation = [recognizer locationInView:self.view];
self.view1.center = touchLocation;
CGPoint velocity = [recognizer velocityInView:self.view];
self.lblhori.text = [NSString stringWithFormat:@"Horizontal Velocity: %.2f points/sec", velocity.x];
self.lblvert.text = [NSString stringWithFormat:@"Vertical Velocity: %.2f points/sec", velocity.y];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Rotate Gesture :
1 ) First Set One View in The Storyboard :
2) Write Following Code in ViewController.h :
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UIView *view1;
UIRotationGestureRecognizer *rotateobj;
}
@property(strong,nonatomic)IBOutlet UIView *view1;
@property(strong,nonatomic)UIRotationGestureRecognizer *rotateobj;
@end
3) Write Following Code in ViewController.m :
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize rotateobj,view1;
- (void)viewDidLoad
{
[super viewDidLoad];
rotateobj = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateselect:)];
[self.view1 addGestureRecognizer:rotateobj];
}
-(void)rotateselect:(UIRotationGestureRecognizer *)recognizer
{
self.view1.transform = CGAffineTransformRotate(self.view1.transform, recognizer.rotation);
recognizer.rotation = 0.0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end




No comments:
Post a Comment