• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

手势,手势应用小程序

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 第一,

     手势是指从用一个或多个手指接触屏幕开始,直到手指离开屏幕为止的所有事件。

     注意:

     在iPhone中最多同时可以支持5点触摸,iPad中最多同时可以支持11点触摸。模拟器中按下option键,可以模拟两点触摸。

第二,

     手势识别器:UIGestureRecognizer

     关系:UIGestureRecognizer是在Touch的基础上封装出来的;

     UIGestureRecognizer的子类:

     UITapGestureRecognizer;//轻拍识别器

     UILongPressGestureRecognizer;//长按识别器

     UISwipGestureRecognizer;//轻扫识别器

     UIRotationGestureRecognizer;//旋转识别器

     UIPinchGestureRecognizer;//捏合识别器

     UIPanGestureRecognizer;//拖动识别器

 

下面是手势识别器的简单应用

 在我们创建的根视图文件中:

  RootViewController.m文件中

  -(void)viewDidLoad

[super viewDidLoad];
    imageNumber =1;
    // Do any additional setup after loading the view from its nib.
    
    NSArray *titles = @[@"轻拍",@"长按",@"轻扫",@"旋转",@"捏合",@"拖拽"];
    //定义选择控件
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:titles];
    segment.frame = CGRectMake(0, 460-30, 320, 30);
                                                                    //根据值的变化控制不同的事件
    [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segment];
    
    //生成图片视图
    //全局变量
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50,80 , 220,280)];
    imageView.image = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"h1" ofType:@"jpeg"]];
    imageView.userInteractionEnabled = YES;//允许用户交互
    [self.view addSubview:imageView];
    
    
    //设置动画
    //滑动控件
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(50, 30, 220, 10)];
    slider.maximumValue = 7;//设置最大值
    slider.minimumValue = 0.5;//设置最小值
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    
    //定义按钮
    NSArray *titleArr = [[NSArray alloc]initWithObjects:@"动画开始",@"动画结束", nil];
    for (int i=0; i<[titleArr count]; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(30+130*i, 380, 120, 30);
        [btn setTag:101+i];
        [btn setTitle:[titleArr objectAtIndex:i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }

//选择事件关联方法

 -(void)segmentAction:(id)sender

UISegmentedControl *segmentedControl = (UISegmentedControl*)sender;//拿到segment的每个控件
    //移除上一次手势  保证只有一个手势在视图上  UIGestureRecognizer手势识别器的父类
    for (UIGestureRecognizer*recognizer in imageView.gestureRecognizers) {
        [imageView removeGestureRecognizer:recognizer];
    }
    
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
        {
            NSLog(@"点击轻拍");
            //定义轻怕手势类
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            
           // tap.numberOfTapsRequired = 1;//得到轻拍次数
            [imageView addGestureRecognizer:tap];
            
        }
            break;
        case 1:
        {
            NSLog(@"点击长按");
            //定义长按手势类
            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
            [imageView addGestureRecognizer:longPress];
        }
            break;
        case 2:
        {
            NSLog(@"点击轻扫");
            //定义轻扫手势类
            UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
           // swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
            [imageView addGestureRecognizer:swipe];
            
        }
            break;
        case 3:
        {
            NSLog(@"点击旋转");
            //定义旋转手势类
            UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
            [imageView addGestureRecognizer:rotation];
            
        }
            break;
        case 4:
        {
            NSLog(@"点击捏合");
            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
            [imageView addGestureRecognizer:pinch];
        }
            break;
        case 5:
        {
            NSLog(@"点击拖拽");
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
            [imageView addGestureRecognizer:pan];
        }
            break;
        default:
            break;
    }

//轻拍方法

-(void)tapAction:(id)sender

NSLog(@"轻拍成功");
    imageNumber=arc4random()%7+1;//获取随机数1-8
    imageView.image = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"h%d",imageNumber] ofType:@"jpeg"]];//生成8个随机图片

//长按方法

-(void)longPressAction:(id)sender

NSLog(@"长按方法");
    if (imageNumber>6) {
        imageNumber = -1;
    }
   // NSLog(@"%d",imageNumber);
    imageNumber +=2;
    imageView.image = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"h%d",imageNumber] ofType:@"jpeg"]];

//定义轻扫方法

-(void)swipeAction:(id)sender

UISwipeGestureRecognizer *swipe = (UISwipeGestureRecognizer*)sender;
  if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
      {
        if (imageNumber>1) {
             imageNumber-=1;//向右滑-1
        }else if (imageNumber ==0)
        {
            imageNumber =1;
        }
        else if (imageNumber ==1)
        {
            imageNumber =8;
        }
       
    }
    imageView.image = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"h%d",imageNumber] ofType:@"jpeg"]];

//定义旋转方法

-(void)rotationAction:(id)sender

NSLog(@"旋转");
    UIRotationGestureRecognizer *rotation = (UIRotationGestureRecognizer*)sender;
    imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);

//定义捏合方法

-(void)pinchAction:(id)sender

UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer*)sender;
    imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);

//定义拖拽方法

-(void)panAction:(id)sender

 UIPanGestureRecognizer *pan = (UIPanGestureRecognizer*)sender;
    NSLog(@"拖拽");
    //判断 一次 相当于touchBegin
    if (pan.state == UIGestureRecognizerStateBegan) {
         startPoint = [pan locationInView:self.view];//得到第一状态的点
    }
    CGPoint newPoint = [pan locationInView:self.view];//得到现在状态的点
    CGFloat contextOffX = newPoint.x - startPoint.x;//得到移动后的x 偏移量
    CGFloat contextOffY = newPoint.y - startPoint.y;//得到移动后的y 偏移量
    imageView.center = CGPointMake(imageView.center.x+contextOffX, imageView.center.y+contextOffY);
    startPoint = newPoint;

//动画方法

-(void)startAction:(id)sender

 

 UIButton *btn = (UIButton*)sender;
    if (btn.tag ==101) {
        NSLog(@"动画开始");
        NSMutableArray *imageArr = [[NSMutableArray alloc]init];
        for (int i=1; i<7; i++) {
            UIImage *currentImage = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"run%d",i] ofType:@"tiff"]];
            [imageArr addObject:currentImage];
        }
        imageView.animationImages = (NSArray*)imageArr;
        [imageView startAnimating];
    }else if (btn.tag ==102)
    {
        NSLog(@"动画结束");
        [imageView stopAnimating];
    }
-(void)sliderAction:(id)sender
{
    UISlider *slider = (UISlider*)sender;
    NSLog(@"%f",slider.value);
    if (imageView.isAnimating) {
       imageView.animationDuration = slider.value;
        [imageView startAnimating];
    }
   
}

 结果:

      

 

 

         

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
微信小程序:thiscodeisamockone发布时间:2022-07-18
下一篇:
微信小程序增加页面发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap