//
// nextViewController.m
#import "nextViewController.h"
#import "my.h"
@interface nextViewController ()
@end
@implementation nextViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createView];
}
#pragma mark ------------------------createView
- (void)createView
{
self.navigationItem.title =@"GESTUR";
UIImageView *vi =[[UIImageView alloc] init];
vi.tag = 100;
vi.frame = CGRectMake(100, 100, 120, 120);
vi.image = [UIImage imageNamed:@"a"];
[self.view addSubview:vi];
vi.userInteractionEnabled = YES ;
#pragma mark ------------------------单击手势
//一个视图可以附着多个手势,一个手势只能附着在一个视图上
// 单击手势
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
//点击次数 默认是1
tap.numberOfTapsRequired =1 ;
//需要几个手指点击 默认是 1
tap.numberOfTouchesRequired = 1;
//附着在视图上
[vi addGestureRecognizer:tap];
#pragma mark ------------------------双击手势
//双击手势
UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(onDouble:)];
doubleTap.numberOfTapsRequired = 2;
[vi addGestureRecognizer:doubleTap];
//单击在双击手势识别失败之后,在去识别手势
[tap requireGestureRecognizerToFail:doubleTap];
#pragma mark ------------------------长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onPress:)];
longPress.minimumPressDuration = 1;
[vi addGestureRecognizer:longPress];
#pragma mark ------------------------拖动手势
//拖动手势 Pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPan:)];
[vi addGestureRecognizer:pan];
#pragma mark ------------------------捏合手势
//捏合 手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(onPinch:)];
[vi addGestureRecognizer:pinch];
//遵守UIGestureRecognizerDelegate协议
pinch.delegate = self;
#pragma mark ------------------------旋转手势
//旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(onRotation:)];
[vi addGestureRecognizer:rotation];
rotation.delegate = self;
#pragma mark ------------------------轻扫手势
//*********************************************************************
UISwipeGestureRecognizer *swipedown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
//设置滑动的方向
swipedown.direction = UISwipeGestureRecognizerDirectionDown;
[vi addGestureRecognizer:swipedown];
//*********************************************************************
UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
swipeleft.direction = UISwipeGestureRecognizerDirectionLeft;
[vi addGestureRecognizer:swipeleft];
//*********************************************************************
UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
swiperight.direction = UISwipeGestureRecognizerDirectionRight;
[vi addGestureRecognizer:swiperight];
//*********************************************************************
UISwipeGestureRecognizer *swipeup = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
swipeup.direction = UISwipeGestureRecognizerDirectionUp;
[vi addGestureRecognizer:swipeup];
}
#pragma mark ------------------------双击手势
- (void)onDouble:(UITapGestureRecognizer *)ondouble
{
NSLog(@"图片被双击..");
}
#pragma mark ------------------------ 单击
- (void)onTap:(UITapGestureRecognizer *)tap
{
NSLog(@"图片被单击了...");
[self creatUIMenuController:tap];
}
#pragma mark------------------------UIMenuController
- (void)creatUIMenuController:(UITapGestureRecognizer *)tap
{
//菜单 是一个单例 NSUserDefaults UIApplication
UIMenuController *menu = [UIMenuController sharedMenuController];
UIMenuItem * item1 = [[UIMenuItem alloc] initWithTitle:@"拷贝" action:@selector(onCopy)];
UIMenuItem * item2 = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(onDelete)];
menu.menuItems = @[item1,item2];
CGPoint point = [tap locationInView:self.view];
NSLog(@"point.x=%f",point.x);
NSLog(@"point.y=%f",point.y);
//设定坐标
[menu setTargetRect:CGRectMake(point.x, point.y, 80, 50) inView:self.view];
//显示menu
[menu setMenuVisible:YES animated:YES];
[self becomeFirstResponder];
}
- (void)onCopy
{
}
- (void)onDelete
{
UIImageView *iv = (id)[self.view viewWithTag:100];
[iv removeFromSuperview];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
#pragma mark ------------------------ 长压
- (void)onPress:(UITapGestureRecognizer *)tap
{
NSLog(@"图片被长压");
}
#pragma mark ------------------------ 拖动
- (void)onPan:(UITapGestureRecognizer *)tap
{
NSLog(@"图片被拖动...");
UIImageView *iv = (id)[self.view viewWithTag:100];
//找到拖动的位置
CGPoint point = [tap locationInView:self.view];
if(point.x>=self.view.bounds.size.width-10)
point.x = self.view.bounds.size.width-10;
iv.frame = CGRectMake(point.x, point.y, iv.frame.size.width, iv.frame.size.height);
NSLog(@"%f,%f",point.x,point.y);
//iv.center = point;
}
#pragma mark ------------------------ 捏合 手势
-(void)onPinch:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"捏合手势");
//放大 缩小
UIImageView *vi =(id)[self.view viewWithTag:100];
vi.transform = CGAffineTransformScale(vi.transform, pinch.scale, pinch.scale); // x的缩放比例 y的的缩放比例 相对与与
[pinch setScale:1]; // 缩放比例 1相当与不缩放
}
#pragma mark ------------------------旋转
- (void)onRotation:(UIRotationGestureRecognizer *)rot
{
NSLog(@"旋转手势");
UIImageView *vi =(id)[self.view viewWithTag:100];
vi.transform = CGAffineTransformRotate(vi.transform, rot.rotation);
[rot setRotation:0];
}
//pan拖动事件的时候轻扫功能不可用
#pragma mark ------------------------轻扫
- (void)onSwipe:(UISwipeGestureRecognizer *)swipe
{
UIImageView *vi =(id)[self.view viewWithTag:100];
CGPoint point = [swipe locationInView:self.view];
if(swipe.direction == UISwipeGestureRecognizerDirectionDown)
{
static int i = 0;
NSLog(@"轻扫手势 swip down = %d次",++i);
vi.frame = CGRectMake(100, point.y+2, vi.frame.size.width, vi.frame.size.height);
}
else if(swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
static int i = 0;
NSLog(@"轻扫手势 swip left = %d次",++i);
}
else if(swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
static int i = 0;
NSLog(@"轻扫手势 swip right = %d次",++i);
}
else if(swipe.direction == UISwipeGestureRecognizerDirectionUp)
{
static int i = 0;
NSLog(@"轻扫手势 swip up = %d次",++i);//???????
NSLog(@"point.y=%f",point.y);
vi.frame = CGRectMake(100, point.y-200, vi.frame.size.width, vi.frame.size.height);
NSLog(@"point.y=%f",point.y);
}
}
#pragma mark ------------------------实现代理协议相似的方法//<UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
//允许相似的手势接受事件,如果方法返回yes就是说允许相似的手势识别事件
return YES;
}
@end