zoukankan      html  css  js  c++  java
  • ios-触摸手势

    //
    //  TouchView.m
    //  touchnumber
    //
    //  Created by  liyang on 14-4-26.
    //  Copyright (c) 2014年 liyang. All rights reserved.
    //
    
    #import "TouchView.h"
    
    
    @implementation TouchView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.userInteractionEnabled=YES;//这个作用是设置此视图是否能捕获事件,当上面的视图的都不能捕获事件的时候下面的视图自然就能捕获了。当UIImageView为什么要设置 self.userInteractionEnabled=YES;因为那个视图默认为no不能捕获事件,上面的按钮自己也不能捕获,所以在上面加上按钮的时候必须将这个属性设置为yes
            self.multipleTouchEnabled=YES;//这个作用是设置此视图是否支持多点触摸
        }
        return self;
    }
    //触摸开始
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
       
      UITouch *touch=  [touches anyObject];
        NSInteger touchnumber=touch.tapCount;
        if (touchnumber==1) {
            //[self singleTab];//这样调用会造成,先执行单击,再双击,所以我们还一种当时来调用
            [self performSelector:@selector(singleTab) withObject:nil afterDelay:0.5];//延迟0.5秒调用这个方法
            
        }else if(touchnumber==2){
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTab) object:nil];//取消之间延迟调用的方法
            [self doubleTab];
        }
       
      }
    //触摸移动
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch *touch=  [touches anyObject];
        CGPoint point=[touch locationInView:self];//拿到点击的坐标
       UIView *view= [[self superview] viewWithTag:100];
        CGRect frame=  view.frame;
        frame.origin=point;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        view.frame=frame;
        [UIView commitAnimations];
        NSLog(@"%@",NSStringFromCGPoint(point));
    
    }
    //触摸结束
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    
    }
    //触摸取消,比如打电话进来了
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    
    }
    -(void)singleTab{
     NSLog(@"单击");
    
    }
    
    -(void)doubleTab{
        
          NSLog(@"双击");
    }
    
    @end

     捏合手势,2点距离

    //
    //  TouchView.m
    //  touchnumber
    //
    //  Created by  liyang on 14-4-26.
    //  Copyright (c) 2014年 liyang. All rights reserved.
    //
    
    #import "TouchView.h"
    
    
    @implementation TouchView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.userInteractionEnabled=YES;//这个作用是设置此视图是否能捕获事件,当上面的视图的都不能捕获事件的时候下面的视图自然就能捕获了。当UIImageView为什么要设置 self.userInteractionEnabled=YES;因为那个视图默认为no不能捕获事件,上面的按钮自己也不能捕获,所以在上面加上按钮的时候必须将这个属性设置为yes
            self.multipleTouchEnabled=YES;//这个作用是设置此视图是否支持多点触摸
        }
        return self;
    }
    double distince=0;
    //触摸开始
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        if ([touches count]==2) {//捏合手势
            NSArray *toucharray= [touches allObjects];
            UITouch * firsttouch=[toucharray objectAtIndex:0];
            UITouch * secondtouch=[toucharray objectAtIndex:1];
            
            
            CGPoint point1=[firsttouch locationInView:self];
            CGPoint point2=[secondtouch locationInView:self];
            //计算2点的距离
            distince=[self distince:point1 point:point2];
            NSLog(@"%f",distince);
        }
        
      }
    
    //计算2点的距离
    -(double)distince:(CGPoint)p1 point:(CGPoint)p2{
       return  sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
    
    }
    
    //触摸移动
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        if ([touches count]==2) {//捏合手势
            NSArray *toucharray= [touches allObjects];
            UITouch * firsttouch=[toucharray objectAtIndex:0];
            UITouch * secondtouch=[toucharray objectAtIndex:1];
            
            
            CGPoint point1=[firsttouch locationInView:self];
            CGPoint point2=[secondtouch locationInView:self];
            //计算2点的距离
           double distince_new=[self distince:point1 point:point2];
            NSLog(@"%f",distince_new/distince);//这个就可以作为放大的倍数了
        }
    
    }
    //触摸结束
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    
    }
    //触摸取消,比如打电话进来了
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    
    }
    
    @end

      //手势

    //
    //  RootViewController.m
    //  uigesturerecognize
    //
    //  Created by  liyang on 14-4-26.
    //  Copyright (c) 2014年 liyang. All rights reserved.
    //
    
    #import "RootViewController.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //单击
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [self.view addGestureRecognizer:tap];
        //双击
        UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap1:)];
        tap1.numberOfTapsRequired=2;
        [self.view addGestureRecognizer:tap1];
        [tap requireGestureRecognizerToFail:tap1];//区别双击和单机,估计实现和刚才一样的,因为现在单机明显慢半拍了
        
        
        //清扫
       UISwipeGestureRecognizer *swipe= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
        swipe.numberOfTouchesRequired=2;//清扫用的手指数
        swipe.direction=UISwipeGestureRecognizerDirectionRight;//清扫的方向
        [self.view addGestureRecognizer:swipe];
        
        
        
        //滑动
        UIPanGestureRecognizer *pan= [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
        
        [self.view addGestureRecognizer:pan];
        
        //长按手势
        UILongPressGestureRecognizer *longpress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpress:)];
        longpress.minimumPressDuration=2;//长按的时间
         [self.view addGestureRecognizer:longpress];
        
        
        //旋转手势
       UIRotationGestureRecognizer *rotation= [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationaction:)];
        [self.view addGestureRecognizer:rotation];
        
        
        //捏合手势
        UIPinchGestureRecognizer*pinch= [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchaction:)];
        [self.view addGestureRecognizer:pinch];
    
    }
    
    -(void)tap:(UITapGestureRecognizer *)pangesturerecognizer{
        NSLog(@"单击");
    }
    -(void)tap1:(UITapGestureRecognizer *)pangesturerecognizer{
        CGPoint p=  [pangesturerecognizer locationInView:self.view];//取得划过的点
        
        NSLog(@"%@", NSStringFromCGPoint(p));
        NSLog(@"双击");
    }
    -(void)swipe:(UISwipeGestureRecognizer *)swipeGesture{
        NSLog(@"清扫");
    }
    -(void)pan:(UIPanGestureRecognizer *)pan{
      CGPoint p=  [pan locationInView:self.view];//取得划过的点
        
        NSLog(@"%@", NSStringFromCGPoint(p));
    }
    -(void)longpress:(UILongPressGestureRecognizer *)pan{
        
        if (pan.state==UIGestureRecognizerStateEnded) {
            return;
        }
        CGPoint p=  [pan locationInView:self.view];//长按也可以获取点取得划过的点,得出结论所有手势都可以获得点
        
        NSLog(@"长按%@", NSStringFromCGPoint(p));
    }
    
    
    -(void)rotationaction:(UIRotationGestureRecognizer *)rotation{
    
    
        NSLog(@"%f",rotation.rotation*(180/M_PI));
    }
    -(void)pinchaction:(UIPinchGestureRecognizer *)pinchgesturerecognizer{
        if (pinchgesturerecognizer.state==UIGestureRecognizerStateEnded) {
            return;
        }
        if ( pinchgesturerecognizer.scale>1) {
            NSLog(@"放大,%f",pinchgesturerecognizer.scale);
        }else{
            NSLog(@"缩小%f",pinchgesturerecognizer.scale);
        }
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    1.这里只记录一些学习笔记 2.这里只记录一些学习心得,如果心得方向有错,请留言 2.这里只记录一些日记(只为提升英语,暂时有点忙,等转行了开始写)
  • 相关阅读:
    了解 NoSQL 的必读资料
    关于什么时候用assert(断言)的思考
    这次见到了一些大侠
    NetBeans 时事通讯(刊号 # 87 Jan 12, 2010)
    动态链接库dll,静态链接库lib, 导入库lib
    新女性十得 写得了代码,查得出异常
    记录系统乱谈
    新女性十得 写得了代码,查得出异常
    fullpage.js禁止滚动
    RunningMapReduceExampleTFIDF hadoopclusternet This document describes how to run the TFIDF MapReduce example against ascii books. This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (110 nodes) Google Pro
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/3691670.html
Copyright © 2011-2022 走看看