UIGestureRecognizer
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
{
UIImageView *imageView ;
NSInteger flag;
}
@property (nonatomic,strong) UIView *panView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"DSC00592.JPG"];
CGFloat interval = (self.view.frame.size.width - 240) / 2.0;
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(interval, 230, 240, 160);
imageView.image = image;
imageView.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] init];
[pinchRecognizer addTarget:self action:@selector(pinch:)];
[imageView addGestureRecognizer:pinchRecognizer];
pinchRecognizer.delegate = self;
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc]
initWithTarget:self action:@selector(rotate:)];
[imageView addGestureRecognizer:rotationRecognizer];
rotationRecognizer.delegate = self;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tap:)];
tapRecognizer.numberOfTapsRequired = 3;
[imageView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(longPress:)];
[self.view addGestureRecognizer:longPressRecognizer];
for (int i = 0; i < 4; i++) {
UISwipeGestureRecognizerDirection direction[] = {
UISwipeGestureRecognizerDirectionDown,
UISwipeGestureRecognizerDirectionLeft,
UISwipeGestureRecognizerDirectionUp,
UISwipeGestureRecognizerDirectionRight
};
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.direction = direction[i];
[self.view addGestureRecognizer:swipe];
}
_panView = [[UIView alloc] initWithFrame:CGRectMake(30, 100, 30, 30)];
_panView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:_panView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_panView addGestureRecognizer:pan];
[self.view addSubview:imageView];
}
-(void)pinch:(UIPinchGestureRecognizer *)recognizer{
imageView.transform = CGAffineTransformScale(imageView.transform,
recognizer.scale,
recognizer.scale);
recognizer.scale = 1;
}
-(void)rotate:(UIRotationGestureRecognizer *)recognizer{
imageView.transform = CGAffineTransformRotate(imageView.transform, recognizer.rotation);
recognizer.rotation = 0;
}
-(void)tap:(UITapGestureRecognizer *)recognizer{
[imageView removeFromSuperview];
}
-(void)longPress:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(random() % 300, rand() % 600, 50, 50)];
view.backgroundColor = [UIColor orangeColor];
CGPoint point = [recognizer locationInView:self.view];
view.center = point;
[self.view addSubview:view];
}
}
-(void)swipe:(UISwipeGestureRecognizer *)recognizer{
switch (recognizer.direction) {
case UISwipeGestureRecognizerDirectionUp:{
NSLog(@"up");
}
break;
case UISwipeGestureRecognizerDirectionLeft:{
NSLog(@"left");
}
break;
case UISwipeGestureRecognizerDirectionDown:{
NSLog(@"down");
}
break;
case UISwipeGestureRecognizerDirectionRight:{
NSLog(@"right");
}
break;
default:
break;
}
}
-(void)pan:(UIPanGestureRecognizer *)recognizer{
CGPoint point = [recognizer locationInView:self.view];
_panView.center = point;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
@end