swipe Rotation Pan

1.

swipe手势

简单代码:

_mySwipeGR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
    //设定轻扫手势方向:
    //只检测符合设定的这个方向的手势
    _mySwipeGR.direction = UISwipeGestureRecognizerDirectionLeft;
    //设定轻扫手势手指数:
    //必须需要的手指数
    _mySwipeGR.numberOfTouchesRequired = 1;
    //添加到需要手势的View
    [self.view addGestureRecognizer:_mySwipeGR];

更多设置详读开发文档

2.Rotation手势

对继承自 UIView 的 UI 元素进行旋转,你可以将旋转手势识别器的 rotation 属性传 递给 CGAffineTransformMakeRotation 方法,以制作一个仿射转场。如下面的实例所示

- (void)ViewRotation{
    _helloWorldLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _helloWorldLabel.text = @"Hello World!";
    _helloWorldLabel.font = [UIFont systemFontOfSize:16.0f];
    [_helloWorldLabel sizeToFit];
    _helloWorldLabel.center = self.view.center;
    [self.view addSubview:_helloWorldLabel];

    _rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotation:)];
    [self.view addGestureRecognizer:_rotationGestureRecognizer];
}
- (void)handleRotation:(UIRotationGestureRecognizer *)paramSender{
    if (_helloWorldLabel == nil) {
        return;
    }
    //设置label的仿射旋转:随时变化角度
    _helloWorldLabel.transform = CGAffineTransformMakeRotation(self.rotationAngleInRotations + paramSender.rotation);
    if (paramSender.state == UIGestureRecognizerStateEnded) {
        self.rotationAngleInRotations += paramSender.rotation;
    }
}

3.Pan 手势

Pan 手势是手指在屏幕上连续的移动。也就是说 pan 手势识别器的目标方法会被重复的调用(从识别处理的开始直到结束)

例子:

- (void)ViewPan{
    CGRect labelFrame = CGRectMake(0, 0, 150, 100);
    panLabel = [[UILabel alloc]initWithFrame:labelFrame];
    panLabel.text = @"Hello World";
    panLabel.backgroundColor = [UIColor blackColor];
    panLabel.textColor = [UIColor whiteColor];
    panLabel.textAlignment = NSTextAlignmentCenter;
    //必须打开交互开关
    panLabel.userInteractionEnabled = YES;
    [self.view addSubview:panLabel];

    //给panlabel添加拖拽手势
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGestures:)];
    //设置拖拽时需要的最大最小手指数
    panGR.minimumNumberOfTouches = 1;
    panGR.maximumNumberOfTouches = 1;
    [panLabel addGestureRecognizer:panGR];
}

一般当拖动手势产生的时候会顺序的经过如下几个状态。

1)UIGestureRecognizerStateBegan  2)UIGestureRecognizerStateChanged  3)UIGestureRecognizerStateEnde

我们一般会实现手势识别时的处理方法,如下的代码,将会实现利用手指来移动屏幕中间的一个标签

- (void)handlePanGestures:(UIPanGestureRecognizer *)paramSender{
    if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed) {
        //为了能达到移动我们屏幕中的标签,我们需要获取手指在屏幕中的坐标, 并不是在标签中的坐标。
        CGPoint location = [paramSender locationInView:paramSender.view.superview];
        paramSender.view.center = location;
    }
}

通过使用 locationInView 这个方法,来获取到我们手势的坐标,为了能够捕获到多个手 指的位置,我们就需要使用 locationOfTouch:inView: 这个方法。然后通过使用 UIPanGestureRecognizer 这 个 对 象 的 minimumNumberOfTouches 和 maximumNumberOfTouches 参数。你就可以在同一个时间来捕获多个手指的拖拽的动作了。

4.Press 手势

iOS SDK 提供了一个类来监听长按手势事件,这个类就是 UILongTapGestureRecognizer,当用户用一个手指或者多个手指长按在一个 UIView 上时,会触发长按事件 ,这个类中的几个重要属性:

numberOfTapsRequired

  这个属性就保存了用户点击的次数,当这个手势动作触发之前,并没有一个手势是在屏幕是上的,当第一次把手指点击在屏幕上,然后拿开,这个动作可以称为一次点击时间。

numberOfTouchesRequired

  这个属性保存了有多少个手指点击了屏幕,因此你要确保你每次的点击手指数目是一样 的,默认值是为 0.

allowableMovement

  用于指定可以移动的最大 pixels。
minimumPressDuration
  

  这个参数表示,两次点击之间间隔的时间长度。

- (void) viewPress{
    dummyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    dummyButton.frame = CGRectMake(100.0f, 150.0f, 72.0f, 37.0f);
    dummyButton.titleLabel.text = @"长按屏幕";
    [self.view addSubview:dummyButton];
    //创建长按手势
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestures:)];
    //设置必须需要长按的手指数
    longPressGR.numberOfTouchesRequired = 2;
    //按下去后打破长按手势的移动距离 默认为10
    longPressGR.allowableMovement = 10.0f;
    //最短按的秒数
    longPressGR.minimumPressDuration = 1.0f;
    [self.view addGestureRecognizer:longPressGR];
}
- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{
    CGPoint touchPoint1 = [paramSender locationOfTouch:0 inView:paramSender.view];
    CGPoint touchPoint2 = [paramSender locationOfTouch:1 inView:paramSender.view];
    CGFloat midPointX = (touchPoint1.x + touchPoint2.x)/2.0f;
    CGFloat midPointY = (touchPoint1.y + touchPoint2.y)/2.0f;
    CGPoint midPoint = CGPointMake(midPointX, midPointY);
    dummyButton.center = midPoint;
}

一个比较典型的用手指来长时间的点击屏幕就是地图的这个应用,当你看不同地方的时候,通过你的手指按住屏幕,然后不要松手,长久按下去,那么不过一会就会有一个锚点标记在地图上

5.tap手势

轻击手势:检测捕获用户点击了屏幕的事件

- (void)viewTap{
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTaps:)];
    tapGR.numberOfTouchesRequired = 2;//默认也为1
    tapGR.numberOfTapsRequired = 3;//需要点3次才能触发事件
    [self.view addGestureRecognizer:tapGR];
}
- (void)handleTaps:(UITapGestureRecognizer *)paramSender{
    NSUInteger touchCounter = 0;
    for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {
        CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];
        NSLog(@"Touch #%lu:%@",(unsigned long)touchCounter + 1,NSStringFromCGPoint(touchPoint));
    }
}

在这段代码中,我们通过两个手指来点击屏幕的手势动作,获取每一次点击的位置,因 此当我们运行这段代码,并且手势动作完成之后,我们会得到我们点击的屏幕位置。

Touch #1:{221.5, 448}

Touch #2:{153.5, 219}

6.pinch手势

  通过捏合手势动作可以很轻松的来改变视图元素的一个比例,例如,我们在通过 Safari 浏览器打开网页的时候,有时间为了能够更清晰的看到其中的一些文字,那么我们就需要放 大我们的屏幕了,有时候我们又需要缩放页面。

  手势的动作状态有如下三种,一般是按照顺序来进行转换的。

    1). UIGestureRecognizerStateBegan

    2). UIGestureRecognizerStateChanged

    3). UIGestureRecognizerStateEnded

  一旦捏合手势动作产生了之后,我们就需要在捕获的事件中进行一个页面调整。其中有两个比较重要的变量 scale 和 velocity,前者是一个比例范围,后者是一个变化速率的,也就是说每次变化的一个像素点。

  一般设置代码如下 :

- (void)viewPinch{
    CGRect labelRect = CGRectMake(0.0f, 250.0f, 200.0f, 200.0f);
    myBlackLabel = [[UILabel alloc]init];
    myBlackLabel.backgroundColor = [UIColor blackColor];
    myBlackLabel.frame = labelRect;
    myBlackLabel.userInteractionEnabled = YES;
    [self.view addSubview:myBlackLabel];

    UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinches:)];
    [myBlackLabel addGestureRecognizer:pinchGR];
}
- (void)handlePinches:(UIPinchGestureRecognizer*)paramSender{

    if (paramSender.state == UIGestureRecognizerStateEnded) {
        currentScale = paramSender.scale;
    }else if (paramSender.state == UIGestureRecognizerStateBegan && currentScale != 0.0f){
        paramSender.scale = currentScale;
    }
    //NAN Not A Number就是代表不是一个数据
    if (paramSender.scale != NAN && paramSender.scale != 0.0) {
        //Return a transform which scales by `(sx, sy)‘: 捏合时x/y方向的该变量相同,所以都填的paramSender.scale
        paramSender.view.transform = CGAffineTransformMakeScale(paramSender.scale, paramSender.scale);
    }
}

由于 scale 这个属性的值是每次都在变的,所以我们需要用另外一个变量来保存当前的 一个 scale 的值,这个变量叫做 currentScale,这样我们就行进行一个缩小,变大的视图效果了

时间: 2024-08-23 10:44:02

swipe Rotation Pan的相关文章

Pan在WEB开发中的含义

对于软件开发而言,Pan其实是一个行话,而不是平底锅. Pan的含义 Pan是从panorama这个词发展而来的,意思就是全景图.当然,这个全景图你只能看到一部分,然后你可以通过按住这个全景图的一边,拖动,然后查看它的其他部分.这种拖拽移动全景图查看其他部分的过程,就叫做panning. 和swipe的区别 从定义上看,swipe和pan是非常相似的--这两者都是touch points发起的平移操作. 区别的地方应该是语义上的.pan会持续反馈,你拖动的时候,元素会一直在允许的方向上持续移动:

iOS 手势及触摸

转自:http://justsee.iteye.com/blog/1885538 一.响应链 在IOS开发中会遇到各种操作事件,通过程序可以对这些事件做出响应. 首先,当发生事件响应时,必须知道由谁来响应事件.在IOS中,由响应者链来对事件进行响应,所有事件响应的类都是UIResponder的子类, 响应者链是一个由不同对象组成的层次结构,其中的每个对象将依次获得响应事件消息的机会.当发生事件时,事件首先被发送给第一响应者,第一响应者往往是事 件发生的视图,也就是用户触摸屏幕的地方.事件将沿着响

iOS 手势UIGestureRecognizer

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

IOS中的手势识别

最近做项目需要用到手势识别,所以花了点时间去学习了一下,其实手势识别这部分的内容不多,还是很容易上手的. IOS中的手势一共有六种 :tap(点击),longPress(长按),swipe(挥动),pan(拖动),pich(缩放),rotation(旋转).这六个手势类都是继承自UIGestureRecongnizer,所以,先来看看UIGestureRecongnizer这个类. UIGestureRecongizer继承自NSObject,是以上六种手势的父类,定义了一些所有手势公用的属性和

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之UIKit_Day17

回顾: 1.手势:           基本手势类:UIGestureRecongnizer类型           六种手势:                    一次性手势:tap Swipe点击一下 划一下                    连续性的手势:longPress  Pinch rotation Pan                    多手势共存:设置需要共存的手势的Delegate,然后让代理对象遵守协议,实现方法二:return Yes   2. 变形      

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

UIGestureRecognizer

UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势.UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势: 1.拍击UITapGestureRecognizer (任意次数的拍击)      2.向里或向外捏UIPinchGestureRecognizer (用于缩放)      3.摇动或者拖拽UIPanGestureRecognizer      4.擦碰UISwipeGestureRecogni

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 *)