ios多手势事件

开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollView增加自己的手指事件。给imageView增加点击、放大、旋转等都可以通过多手势来完成。
下面就介绍下各种的手势的使用方法,及注意事项。
- (void)viewDidLoad

{
    [super viewDidLoad];
    //为了能响应多手势事件,imageView的userInteractionEnabled属性要设为YES.
    self.imageView.userInteractionEnabled=YES;

    //1、手指点击事件
    //单指单击
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                      action:@selector(fingerIncident:)];
//手指数
    singleFingerOne.numberOfTouchesRequired = 1;
//点击次数
    singleFingerOne.numberOfTapsRequired = 1;
//设置代理方法
    singleFingerOne.delegate= self;
//增加事件者响应者,
    [self.imageView addGestureRecognizer:singleFingerOne]; 

    //单指双击
    UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                      action:@selector(fingerIncident:)];
    singleFingerTwo.numberOfTouchesRequired = 1;
    singleFingerTwo.numberOfTapsRequired = 2;
    singleFingerTwo.delegate= self;
    [self.imageView addGestureRecognizer:singleFingerTwo];

    //双指单击
    UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                      action:@selector(fingerIncident:)];
    doubleFingerOne.numberOfTouchesRequired = 2;
    doubleFingerOne.numberOfTapsRequired = 1;
    doubleFingerOne.delegate= self;
    [self.imageView addGestureRecognizer:doubleFingerOne];

    //双指双击
    UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                      action:@selector(fingerIncident:)];
    doubleFingerTwo.numberOfTouchesRequired = 2;
    doubleFingerTwo.numberOfTapsRequired = 2;
    doubleFingerTwo.delegate= self;
    [self.imageView addGestureRecognizer:doubleFingerTwo];

    //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
    [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
    //同理双指也是如此
    [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];

    //2、手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上
    UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
    [self.imageView addGestureRecognizer:pinchGesture];//imageView添加手势识别
    [pinchGesture release];

    //3、旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上
    UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotateGesture:)];
    [self.imageView addGestureRecognizer:rotateGesture];
    [rotateGesture release];

    //4、拖手势
    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
    [self.imageView addGestureRecognizer:panGesture];
    [panGesture release];

    //当实现上面拖手势(UIPanGestureRecognizer)后将不能响应下面的左右划(UISwipeGestureRecognizer)事件,事件会被拖手势栏截,所以左右划和拖手势只能选其一
    //右划
    UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
    [self.imageView addGestureRecognizer:swipeGesture];
    [swipeGesture release];
    //左划
    UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不设置黑夜是右
    [self.imageView addGestureRecognizer:swipeLeftGesture];
    [swipeLeftGesture release];

    UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self
                                                                                                action:@selector(handleLongpressGesture:)];
    //长按时间为1秒
    longpressGesutre.minimumPressDuration=0.5;
    //允许15秒中运动
    longpressGesutre.allowableMovement=3;
    //所需触摸1次
    longpressGesutre.numberOfTouchesRequired=1;
    [self.imageView addGestureRecognizer:longpressGesutre];
    [longpressGesutre release];
}

//手指点击事件
- (void)fingerIncident:(UITapGestureRecognizer *)sender
{
    if (sender.numberOfTouchesRequired==1) {
        //单指点击事件
        if(sender.numberOfTapsRequired == 1) {
            //单指单击
            NSLog(@"单指单击");
            [sender.view.layer removeAllAnimations];
        }
        else if(sender.numberOfTapsRequired ==2 ){
            //单指双击
            NSLog(@"单指双击");
        }
    }
    else if (sender.numberOfTouchesRequired==2) {
        //双指点击事件
        if(sender.numberOfTapsRequired == 1) {
            //双指单击
            NSLog(@"双指单击");
        }
        else if(sender.numberOfTapsRequired ==2 ){
            //双指双击
            NSLog(@"双指双击");
        }
    }
}

//手指左右滑动事件
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender{
    if (sender.direction==UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"左滑");
    }
    else{
        NSLog(@"右滑");
    }
}

//手指捏事件
-(void)handlePinchGesture:(UIPinchGestureRecognizer *)sender{
    NSLog(@"捏");

    if([sender state] == UIGestureRecognizerStateEnded) {
        lastScale = 1.0;
        return;
    }

    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    CGAffineTransform newTransform = CGAffineTransformScale(sender.view.transform, scale, scale);

    [sender.view setTransform:newTransform];
    lastScale = [sender scale];
}

//手指旋转事件
-(void)handleRotateGesture:(UIRotationGestureRecognizer *)sender{
    NSLog(@"旋转");

    NSLog(@"sender.scale=%f",sender.rotation);
    NSLog(@"sender.velocity=%f",sender.velocity);

    sender.view.transform=CGAffineTransformRotate(sender.view.transform, sender.rotation);
    sender.rotation=0;
}

//手指拖事件
-(void)handlePanGesture:(UIPanGestureRecognizer *)sender{
    NSLog(@"拖");
    NSLog(@"sender=%@",sender);
    CGPoint translation=[(UIPanGestureRecognizer*)sender translationInView:self.view];

    //开始
    if (sender.state==UIGestureRecognizerStateBegan) {
        x=sender.view.center.x;
        y=sender.view.center.y;
    }
    //进行中
    if (sender.state==UIGestureRecognizerStateChanged) {
        sender.view.center=CGPointMake(x+translation.x, y+translation.y);
    }
    //结束
    if (sender.state==UIGestureRecognizerStateEnded) {
        sender.view.center=CGPointMake(x+translation.x, y+translation.y);
    }
}

//手指长按事件
-(void)handleLongpressGesture:(UILongPressGestureRecognizer *)sender{
    NSLog(@"长按");
    if (sender.state==UIGestureRecognizerStateBegan) {
        CABasicAnimation* rotationAnimation_ = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        //角度转弧度
        rotationAnimation_.fromValue = [NSNumber numberWithFloat:(-0.02)];
        rotationAnimation_.toValue = [NSNumber numberWithFloat:(0.02)];
        //动画时间
        rotationAnimation_.duration = 0.1f;
        //动画次数
        rotationAnimation_.repeatCount=FLT_MAX;
        //回退动画
        rotationAnimation_.autoreverses = YES;
        //动画开始结束的快慢,设置为加速
        rotationAnimation_.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [[sender.view layer] addAnimation:rotationAnimation_ forKey:@"revItUpAnimation"];
    }
}
时间: 2024-10-20 08:34:38

ios多手势事件的相关文章

IOS 手势事件的冲突

关于手操作需要强调几点: UIImageView默认是不支持交互的,也就是userInteractionEnabled=NO ,因此要接收触摸事件(手势识别),必须设置userInteractionEnabled=YES(在iOS中UILabel.UIImageView的userInteractionEnabled默认都是NO,UIButton.UITextField.UIScrollView.UITableView等默认都是YES). 轻扫手势虽然是连续手势但是它的操作事件只会在识别结束时调用

IOS开发中长按的手势事件编程

长按手势事件: 长按按钮1S后改变按钮颜色: 1 // 长按事件 2 #import "ViewController.h" 3 @interface ViewController (){ 4 UIButton *myBtn; 5 } 6 @end 7 @implementation ViewController 8 - (void)viewDidLoad { 9 [super viewDidLoad]; 10 myBtn = [[UIButton alloc]initWithFrame

iOS中touches事件,addtarget ...action和GestureRecognizer详解

刚学完uiview,uicontrol类,许多人知道 touchesBegain,touchesMoved,touchesEnd,GestureRecognizer的用途,但仔细考虑这些事件之间的关系,却令人头疼. 现在以一个例子来分析它们的内部实现: - (void)viewDidLoad { UIButton * btn=[[UIButton alloc]initWithFrame:CGRectMake(20, 40, 50, 50)]; [self.view addSubview:btn]

iOS开发——项目总结OC篇&iOS 长按事件菜单的实现(剪切版)

iOS 长按事件菜单的实现(剪切版) 一:简单实现菜单 添加长按手势 [self.label addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)]]; 实现手势方法 1 - (void)labelClick 2 { 3 // 获得菜单 4 UIMenuController *menu = [UIMenuController share

windows phone和android,ios的touch事件兼容

1.开发背景 最近用html5写了个小游戏,中间踩过无数坑,有很多甚至百度都百度不到答案,可见html5还真是不成熟,兼容性的复杂度比ie6有过之而无不及,性能那个渣简直无力吐槽.. 好了,吐槽结束,虽然有这么多的缺点,但是由于其良好的跨平台前景以及极低的学习成本,再加上优秀的框架,我最终还是选择了用html5来开发这个小游戏,而且是小游戏,所以就没有用什么游戏开发框架了,只是自己简单的封装了一个,因此所有的bug都被我走了一遍..正当我调试完所有的android上的bug之后,心想自己的努力不

【iOS开发-手势】iOS中各种手势总结

iOS中支持的手势事件 UITapGestureRecognizer(敲击) UIPinchGestureRecognizer(捏合,用于缩放) UIPanGestureRecognizer(拖拽) UISwipeGestureRecognizer(轻扫) UIRotationGestureRecognizer(旋转) UILongPressGestureRecognizer(长按) UITapGestureRecognizer(敲击) //创建UITapGestureRecognizer对象,

iOS中手势的使用

在iOS中添加手势比较简单,可以归纳为以下几个步骤: 创建对应的手势对象: 设置手势识别属性[可选]: 附加手势到指定的对象: 编写手势操作方法: 为了帮助大家理解,下面以一个图片查看程序演示一下上面几种手势,在这个程序中我们完成以下功能: 如果点按图片会在导航栏显示图片名称: 如果长按图片会显示删除按钮,提示用户是否删除: 如果捏合会放大.缩小图片: 如果轻扫会切换到下一张或上一张图片: 如果旋转会旋转图片: 如果拖动会移动图片: 具体布局草图如下: 为了显示导航条,我们首先将主视图控制器KC

UITouch/UIResponder:iOS上触摸事件的视图检测和事件传递

iPhone上有非常流畅的用户触摸交互体验,能检测各种手势:点击,滑动,放大缩小,旋转.大多数情况都是用UI*GestureRecognizer这样的手势对象来关联手势事件和手势处理函数.也有时候,会看到第三方代码里会在如下函数中进行处理: -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event: 那么问题就来了,手势和touch到底有什么区别和联系?这一切还得从头iOS触摸事件检测,以及UIResponder(响应者)开始说起

手势事件

1.基本的手势事件主要有如下三个方法: dispatchTouchEvent : 判断该事件是否需要下发.返回true表示需要下发给下级视图,返回false表示不需要下发(交给自身的onTouchEvent处理).但是否最终下发,还需根据onInterceptTouchEvent的拦截结果. onInterceptTouchEvent : 判断当前容器是否需要拦截该事件.返回true表示予以拦截(交给自身的onTouchEvent处理).不放给下级视图,返回false表示不拦截该事件. onTo