#import "RootViewController.h"
#import "RootView.h"
#import "SecondViewController.h"

@interface RootViewController ()<UIGestureRecognizerDelegate>// 设置代理
@property (nonatomic, strong) RootView *rootView;
@end

@implementation RootViewController

- (void)loadView
{
    self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //添加轻拍点击事件
    [self tapGesture];
    //添加平移手势
//    [self panGesture];
    // 添加捏合手势
    [self pinchGesture];
    // 添加旋转手势
    [self rotationGesture];
    // 添加清扫手势
    [self swipeGesture];
    // 添加长按按钮
    [self longPressGesture];
    // 添加边缘清扫
    [self screenEdgePanGesture];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 轻拍 UITapGestureRecognizer

// 添加轻拍手势

- (void)tapGesture
{
    //1. 创建手势识别器对象
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
    
    //2. 讲手势添加到相应的视图上
    [self.rootView.myImageView addGestureRecognizer:tap];
}

// 实现轻拍手势事件
- (void)tapGestureAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"轻拍");
}

#pragma mark 平移 UIPanGestureRecognizer
- (void)panGesture
{
    // 1.创建手势对象
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
    // 2. 将手势添加到相应的视图上
    [self.rootView.myImageView addGestureRecognizer:pan];
    
}

- (void)panGestureAction:(UIPanGestureRecognizer *)pan
{
    // 在view上面挪动的距离
    CGPoint p = [pan translationInView:pan.view];
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
    // 清空移动距离
    [pan setTranslation:CGPointZero inView:pan.view];
    
    NSLog(@"平移");
}

#pragma mark 缩放手势 (捏合) UIPinchGestureRecognizer

- (void)pinchGesture
{
    // 1.创建手势对象

    UIPinchGestureRecognizer *pich = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureAction:)];
    // 设置代理
    pich.delegate = self;
    
    
    // 2. 将手势添加到相应的视图上
    [self.rootView.myImageView addGestureRecognizer:pich];
    
}

- (void)pinchGestureAction:(UIPinchGestureRecognizer *)pich
{
    
    pich.view.transform = CGAffineTransformScale(pich.view.transform, pich.scale, pich.scale);
    pich.scale = 1;//(比例大小一样)
    
    NSLog(@"捏合");
}

#pragma mark 旋转手势 UIRotationGestureRecognizer

- (void)rotationGesture
{
    
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureAction:)];
    // 设置代理 (目的)旋转和捏合一起用
    rotation.delegate = self;
    
    [self.rootView.myImageView addGestureRecognizer:rotation];
    
}

- (void)rotationGestureAction:(UIRotationGestureRecognizer *)rotation
{
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    rotation.rotation = 0;//(旋转的角度清零)
    
    NSLog(@"旋转");
    
}

// 实现协议方法, 同时识别多个手势

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

#pragma mark 长按手势 UILongPressGestureRecognizer

- (void)longPressGesture
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)];
    longPress.minimumPressDuration = 1.0;
//    longPressGr.minimumPressDuration = 1.0;
//    [self.tableView addGestureRecognizer:longPressGr];
//    [longPressGr release];
    [self.rootView.myImageView addGestureRecognizer:longPress];
    
}

- (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
//        longPress.rootView.myImageView.backgroundColor = [UIColor greenColor];
//        CGPoint point = [longPress locationInView:self.rootView.myImageView];
//                NSIndexPath * indexPath = [self.rootView.myImageView indexPathForRowAtPoint:point];
//
        NSLog(@"长按按钮");
        
//                if(indexPath == nil) return ;
    }
 //    longPressGesture.view.backgroundColor = [UIColor random];
//    if(gesture.state == UIGestureRecognizerStateBegan)
//    {
//        CGPoint point = [gesture locationInView:self.tableView];
//        
//        NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];
//        
//        if(indexPath == nil) return ;
}

#pragma mark 清扫手势 UISwipeGestureRecognizer

- (void)swipeGesture
{
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.rootView.myImageView addGestureRecognizer:swipe];
}


- (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipe
{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self presentViewController:secondVC animated:YES completion:nil];
    NSLog(@"向左扫");
}


#pragma mark 屏幕边缘轻扫识别器 UIScreenEdgePanGestureRecognizer

- (void)screenEdgePanGesture
{
    UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
    screen.edges = UIRectEdgeRight;
    [self.rootView addGestureRecognizer:screen];
    
}

- (void)screenEdgePanGestureAction:(UIScreenEdgePanGestureRecognizer *)screen
{
    NSLog(@"");
}

 

版权声明:本文为leikun1113原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/leikun1113/p/5539460.html