码迷,mamicode.com
首页 > 其他好文 > 详细

手势,手势应用小程序

时间:2014-05-19 15:08:25      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

 第一,

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

     注意:

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

第二,

     手势识别器:UIGestureRecognizer

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

     UIGestureRecognizer的子类:

     UITapGestureRecognizer;//轻拍识别器

     UILongPressGestureRecognizer;//长按识别器

     UISwipGestureRecognizer;//轻扫识别器

     UIRotationGestureRecognizer;//旋转识别器

     UIPinchGestureRecognizer;//捏合识别器

     UIPanGestureRecognizer;//拖动识别器

 

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

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

  RootViewController.m文件中

  -(void)viewDidLoad

bubuko.com,布布扣
[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];
    }
bubuko.com,布布扣

//选择事件关联方法

 -(void)segmentAction:(id)sender

bubuko.com,布布扣
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;
    }
bubuko.com,布布扣

//轻拍方法

-(void)tapAction:(id)sender

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

//长按方法

-(void)longPressAction:(id)sender

bubuko.com,布布扣
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"]];
bubuko.com,布布扣

//定义轻扫方法

-(void)swipeAction:(id)sender

bubuko.com,布布扣
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"]];
bubuko.com,布布扣

//定义旋转方法

-(void)rotationAction:(id)sender

bubuko.com,布布扣
NSLog(@"旋转");
    UIRotationGestureRecognizer *rotation = (UIRotationGestureRecognizer*)sender;
    imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);
bubuko.com,布布扣

//定义捏合方法

-(void)pinchAction:(id)sender

bubuko.com,布布扣
UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer*)sender;
    imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
bubuko.com,布布扣

//定义拖拽方法

-(void)panAction:(id)sender

bubuko.com,布布扣
 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;
bubuko.com,布布扣

//动画方法

-(void)startAction:(id)sender

 

bubuko.com,布布扣
 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];
    }
bubuko.com,布布扣
bubuko.com,布布扣
-(void)sliderAction:(id)sender
{
    UISlider *slider = (UISlider*)sender;
    NSLog(@"%f",slider.value);
    if (imageView.isAnimating) {
       imageView.animationDuration = slider.value;
        [imageView startAnimating];
    }
   
}
bubuko.com,布布扣

 结果:

      bubuko.com,布布扣

 

 

         

 

 

手势,手势应用小程序,布布扣,bubuko.com

手势,手势应用小程序

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/taopengcun/p/3735169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!