iOS 常用手势

UIGestureRecognizer 对iOS的各种手势进行了封装,完全满足了用户对手势的需求。

以下是对各种手势的详细应用和说明,希望能对大家有帮助。^_^

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-150)/2, (self.view.frame.size.height-150)/2, 150, 150)];
    _imageView.userInteractionEnabled = YES;//交互使能,允许界面交互
    _imageView.image = [UIImage imageNamed:@"cat.png"];
    [self.view addSubview:_imageView];

    // 单击的 TapRecognizer
    UITapGestureRecognizer *singleTap;
    singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
    singleTap.numberOfTapsRequired = 1; //点击的次数 =1 单击

    [_imageView addGestureRecognizer:singleTap];//给对象添加一个手势监测;

    // 双击的 TapRecognizer
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTap:)];
    doubleTap.numberOfTapsRequired = 2; //点击的次数 =2 双击
    [_imageView addGestureRecognizer:doubleTap];//给对象添加一个手势监测;

    /*
     1.双击手势确定监测失败才会触发单击手势的相应操作,否则双击时第一击时会响应单击事件
     2.会造成单击时要判断是否是双击,调用单击会有所延时。属正常现象。
     */
    [singleTap requireGestureRecognizerToFail:doubleTap];

    //捏合缩放手势 Pinch
    UIPinchGestureRecognizer *pinch;
    pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
    //    [_imageView addGestureRecognizer:pinch];//添加到_imageView的时候,是要把手指放到_imageView操作
    [self.view addGestureRecognizer:pinch];//是self的时候,操作整个view都可以捏合_imageView(在响应事件中操作)
    pinch.delegate = self;

    //旋转手势 Rotation
    UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]
                                                     initWithTarget:self
                                                     action:@selector(handleRotate:)];
    [self.view addGestureRecognizer:rotateRecognizer];//是self的时候,操作整个view都可以捏合_imageView(在响应事件中操作)
    rotateRecognizer.delegate = self;

    //滑动手势 SwipeRecognizer
    UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]
                                                 initWithTarget:self
                                                 action:@selector(handleSwipe:)];
    [self.view addGestureRecognizer:swipeRecognizer];//是self的时候,操作整个view都可以捏合_imageView(在响应事件中操作)
    swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;//操作为左滑
    swipeRecognizer.delegate = self;

    //拖动手势 PanRecognizer
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]
                                             initWithTarget:self
                                             action:@selector(handlePan:)];
    [_imageView addGestureRecognizer:panRecognizer];//关键语句,添加一个手势监测;
    panRecognizer.maximumNumberOfTouches = 1;
    panRecognizer.delegate = self;

    //长按手势 LongPressRecognizer
    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
                                                         initWithTarget:self
                                                         action:@selector(handlelongPress:)];
    [_imageView addGestureRecognizer:longPressRecognizer];
    longPressRecognizer.minimumPressDuration = 1.0f;//触发长按事件时间为:1.0秒
    longPressRecognizer.delegate = self;

}

-(void)SingleTap:(UITapGestureRecognizer*)recognizer
{
    //处理单击操作
    NSLog(@"单击操作");
}

-(void)DoubleTap:(UITapGestureRecognizer*)recognizer
{
    //处理双击操作
    NSLog(@"双击操作");
}

- (void)handlePinch:(UIPinchGestureRecognizer*)recognizer
{
    NSLog(@"缩放操作");//处理缩放操作
    //对imageview缩放
    _imageView.transform = CGAffineTransformScale(_imageView.transform, recognizer.scale, recognizer.scale);
    //对self.view缩放,因为recognizer是添加在self.view上的
    //recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;
}

- (void)handleRotate:(UIRotationGestureRecognizer*) recognizer
{
    NSLog(@"旋转操作");//处理旋转操作
    //对imageview旋转
    _imageView.transform = CGAffineTransformRotate(_imageView.transform, recognizer.rotation);
    //对self.view旋转,因为recognizer是添加在self.view上的
    //    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    recognizer.rotation = 0;
}

- (void)handleSwipe:(UISwipeGestureRecognizer*) recognizer
{
    //处理滑动操作
    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"左滑滑动操作");
    }else if(recognizer.direction==UISwipeGestureRecognizerDirectionRight){
        NSLog(@"右滑滑动操作");
    }
}

-(void)handlePan:(UIPanGestureRecognizer*)recognizer
{
    NSLog(@"拖动操作");
    //处理拖动操作,拖动是基于imageview,如果经过旋转,拖动方向也是相对imageview上下左右移动,而不是屏幕对上下左右
    CGPoint translation = [recognizer translationInView:_imageView];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:_imageView];
}

-(void)handlelongPress:(UILongPressGestureRecognizer*)recognizer
{
    //处理长按操作,开始结束都会调用,所以长按1次会执行2次
    if(recognizer.state == UIGestureRecognizerStateBegan){
        NSLog(@"开始长按操作");
    }else if(recognizer.state == UIGestureRecognizerStateEnded){
        NSLog(@"结束长按操作");
    }
}

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

原文地址:https://www.cnblogs.com/xujinzhong/p/8421723.html

时间: 2024-10-12 12:45:51

iOS 常用手势的相关文章

iOS常用手势识别器

手势识别状态: typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { // 没有触摸事件发生,所有手势识别的默认状态 UIGestureRecognizerStatePossible, // 一个手势已经开始但尚未改变或者完成时 UIGestureRecognizerStateBegan, // 手势状态改变 UIGestureRecognizerStateChanged, // 手势完成 UIGestureRecognizerStateE

IOS常用手势用法

target参数指的是给谁用手势,入button,view等 //1.单击 UITapGestureRecognizer *singleTapGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)]; [self.buttonView addGestureRecognizer:singleTapGR]; - (void)singleTap:(UITapGestureRecognizer 

IOS开发—6种常用手势UIGestureRecognizer介绍

IOS 6种常用手势介绍 // // ViewController.m //  手势 // //  Created by Lotheve on 15/6/13. //  Copyright (c) 2015年Lotheve. All rights reserved. // #import "ViewController.h" @interface ViewController () { UITapGestureRecognizer *_tap;   //点击 UIPanGestureR

ios的手势UIGestureRecognizer

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)

ios各种手势,很有意思

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent

ios的手势操作之UIGestureRecognizer

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)

【转】 ios的手势操作之UIGestureRecognizer浅析

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)

IOS中手势UIGestureRecognizer

通常在对视图进行缩放移动等操作的时候我们可以用UIScrollView,因为它里边自带了这些功能,我们要做的就是告诉UIScrollView的几个相关参数就可以了 但是没有实现旋转的手势即UIRotationGestureRecognizer IOS中手势有很多: UIRotationGestureRecognizer旋转 UITapGestureRecognizer手指点击 UIPinchGestureRecognizer缩放 UISwipeGestureRecognizer手指快速扫过 UI

iOS常用控件尺寸大集合

元素控件 尺寸(pts) Window(含状态栏) 320 x 480 Status Bar的高度 20 Navigation Bar的高度 44 含Prompt的Navigation Bar的高度 74 Navigation Bar的图标 20×20(透明的png) Tool Bar的高度 44 Tool Bar的图标 20×20(透明的png) Tab Bar的高度 49 Tab Bar的图标 30×30(透明的png) 竖直时键盘的高度 216.252(iOS 5+的中文键盘) 水平时键盘