ios之UIGestureRecognizer手势基础使用解析

UIGestureRecognizer 的子类分别有很多手势,通过 不用的手势可以执行不同的操作,下面来介绍下他们的基本使用方法所有手势配置基本相同,只是针对不同的手势里面有部分属性可以设置,比如说tap点进去看他有两个参数可以设置一个是点击次数,和点击手指数可设置。如果不知道这个手势能配置说明参数,那么点击进入相应的.h 文件查看

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecongnizer:)];//点击手势
    [tapGesture setNumberOfTouchesRequired:1];
    [tapGesture setNumberOfTapsRequired:1];
    [_view addGestureRecognizer:tapGesture];
    [tapGesture release];
- (void)processGestureRecongnizer:(UIGestureRecognizer *)gesture
{
    if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
        [self positionAnimation];
    }
}
#pragma mark -- InitUserInterface
- (void)initUserInterface
{
    _view = [[UIView alloc]init];
    [_view setBounds:CGRectMake(0, 0, 200, 200)];
    [_view setCenter:CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
    [_view setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:_view];
    //两手指拨动手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [pinch setDelegate:self];//设置代理
    [_view addGestureRecognizer:pinch]; //对view添加这个手势
    [pinch release];
    //旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [rotation setDelegate:self];
    [_view addGestureRecognizer:rotation];
    [rotation release];
    //长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    longPress.minimumPressDuration = 2;
    [_view addGestureRecognizer:longPress];
    [longPress release];
    //滑动手势--左
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];//配置滑动方向
    [self.view addGestureRecognizer:leftSwipe];
    [leftSwipe release];
    //滑动手势--右
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.view addGestureRecognizer:rightSwipe];
    [rightSwipe release];
    //拖移手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [pan setDelegate:self];
    [_view addGestureRecognizer:pan];
    [pan release];

}
#pragma mark -- GestureRrecognizer methods
//代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return NO;
}
// 手势action 让对应的手势执行相应地操作
- (void)processGestureRecognizer:(UIGestureRecognizer *)gesture
{
    //判断手势类型
    if ([gesture isKindOfClass:[UIPinchGestureRecognizer class]])
    {
        UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer *)gesture;
        static float lastScale;//静态变量记录上次大小
        if (pinch.state == UIGestureRecognizerStateBegan) {
            lastScale = pinch.scale;// 手势开始把初始scale赋给静态变量方便更新
        }else if (pinch.state == UIGestureRecognizerStateChanged){
            [_view setTransform:CGAffineTransformScale(_view.transform, 1+(pinch.scale - lastScale), 1+(pinch.scale - lastScale))];//让View进行动态的放大或缩小
            lastScale = pinch.scale;// 更新scale的值 --(这样做让view保持变化后的状态而不会是初始状态)
            //此方法不需要更新lastScale的值  都是从原型开始
//            [_view setTransform:CGAffineTransformMakeScale(1+(pinch.scale - lastScale), 1+(pinch.scale - lastScale))];
        }
    }else if ([gesture isKindOfClass:[UIRotationGestureRecognizer class]])
    {
        UIRotationGestureRecognizer *rotation = (UIRotationGestureRecognizer *)gesture;
        static float lastRotation;
        if (rotation.state == UIGestureRecognizerStateBegan) {
            lastRotation = rotation.rotation;
        }else if (rotation.state == UIGestureRecognizerStateChanged){
            [_view setTransform:CGAffineTransformRotate(_view.transform, rotation.rotation - lastRotation)];
            lastRotation = rotation.rotation;
        }
    }
    else if ([gesture isKindOfClass:[UILongPressGestureRecognizer class]])
    {
        UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)gesture;
        if (longPress.state == UIGestureRecognizerStateBegan) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"Long Press Begin" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
        }
    }
    else if ([gesture isKindOfClass:[UISwipeGestureRecognizer class]])
    {
        UISwipeGestureRecognizer *swipe = (UISwipeGestureRecognizer *)gesture;
        if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
            [UIView animateWithDuration:1 animations:^{
                [_view setCenter:CGPointMake(CGRectGetMinX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
            }];
        }else{
            [UIView animateWithDuration:1 animations:^{
                [_view setCenter:CGPointMake(CGRectGetMaxX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
            }];
        }

    }
    else if ([gesture isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gesture;
        static CGPoint lastLocation;
        if (pan.state == UIGestureRecognizerStateBegan) {
            lastLocation = _view.center;
        }else if (pan.state == UIGestureRecognizerStateChanged)
        {
            CGPoint translationPoint = [pan translationInView:self.view];
            _view.center = CGPointMake(translationPoint.x + lastLocation.x, translationPoint.y+ lastLocation.y);
        }else if (pan.state == UIGestureRecognizerStateEnded)
        {
            lastLocation = CGPointZero;
        }
    }
}
时间: 2024-09-27 17:39:04

ios之UIGestureRecognizer手势基础使用解析的相关文章

iOS 基础函数解析 - Foundation Functions Reference

Foundation Functions Reference Framework Foundation/Foundation.h Declared in NSBundle.h NSByteOrder.h NSDecimal.h NSException.h NSObjCRuntime.h NSObject.h NSPathUtilities.h NSRange.h NSZone.h Overview This chapter describes the functions and function

零基础iOS之Json及XML数据解析2

零基础iOS之Json及XML数据解析http://www.cnblogs.com/dingjianjaja/articles/4798604.html

IOS开发之手势—UIGestureRecognizer 共存

IOS开发之手势--UIGestureRecognizer 共存 在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断.用 UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹.UIGestureRecognizer的衍生类別有以下几种: UITapGestureRec

Android 触摸手势基础 官方文档概览2

Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: MotionEvent 兼容版的: MotionEventCompat  (Note that MotionEventCompat is not a replacement for the MotionEvent class. Rather, it provides static utility metho

iOS下的手势密码实现

一.iOS下的手势 1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @property (weak, nonatomic) IBOutlet UILabel *genstureLabel; 6 7 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 16

iOS网络数据下载和JSON解析

iOS网络数据下载和JSON解析 简介 在本文中笔者将要给大家介绍iOS中如何利用NSURLConnection如何从网络中下载数据,如何解析下载下来的JSON数据格式,以及如何显示数据和图片的异步下载显示. 涉及到的知识点: 1.NSURLConnection异步下载和封装 #import "ZJHttpRequest.h" //消除performSelector的警告 #pragma clang diagnostic ignored "-Warc-performSelec

ios 网络数据下载和JSON解析

ios 网络数据下载和JSON解析 简介 在本文中笔者将要给大家介绍ios中如何利用NSURLConnection从网络上下载数据,如何解析下载下来的JSON数据格式,以及如何显示数据和图片的异步下载显示 涉及到得知识: 1.NSURLConnection异步下载和封装 2.JSON格式和JSON格式解析 3.数据显示和使用SDWebImage异步显示图片 内容 1.网络下载基础知识介绍 (1)什么是网络应用? 一般情况下, iPhone的计算机, 照相机不需要从网络上下载数据也能运行, 所以这

iOS开发之手势gesture详解(二)

与其他用户界面控件交互 UIControl子类会覆盖parentView的gesture.例如当用户点击UIButton时,UIButton会接受触摸事件,它的parentView不会接收到.这仅适用于手势识别重叠的默认动作的控制,其中包括: 一根手指单击动作:UIButton, UISwitch, UIStepper, UISegmentedControl, and UIPageControl. 一根手指擦碰动作:UISlider 一根手指拖动动作:UISwitch 包含多点触摸的事件 在iO

iOS开发之手势gesture详解(一)

前言 在iOS中,你可以使用系统内置的手势识别(GestureRecognizer),也可以创建自己的手势.GestureRecognizer将低级别的转换为高级别的执行行为,是你绑定到view的对象,当发生手势,绑定到的view对象会响应,它确定这个动作是否对应一个特定的手势(swipe,pinch,pan,rotation).如果它能识别这个手势,那么就会向绑定它的view发送消息,如下图 UIKit框架提供了一些预定义的GestureRecognizer.包含下列手势 UITapGestu