UITouch类

    1、UITouch类继承自NSObject类,代表用户触摸事件

     属性 :

         tapCount:用户手指接触屏幕的次数。

         timestamp:一个触摸时间发生的时间或者持续的时间。以秒为单位

         phase:用户触摸屏幕动作的类型:             

                   UITouchPhaseBegin,

                   UITouchPhaseMoved,

                   UITouchPhaseStationary,

                   UITouchPhaseEnded,

                   UITouchPhaseCancelled.

        view:触摸事件最初发生时所的视图

        window:触摸事件最初发生时所在的窗口。


    方法:

         - (CGPoint)locationInView:(UIView *)view:返回对象在视图中的坐标。

         - (CGPoint)previousLocationInView:(UIView *)view:返回对象之前在视图中的坐标。  


        代码:

        首先创建一个空的工程,然后建一个新类,注意:该类继承自UIView,

          在AppDelegate.h中包含该类的头文件,并声明一个该类的属性,名为pView;

     AppDelegate.m文件中- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法添加如下代码:
      

    boUIview *pNew = [[UIviewalloc]initWithFrame:CGRectMake(0,0,320,500)];

    

   self.pView = pNew;

    

    [pNewrelease];

    

    self.pView.backgroundColor = [UIColorgreenColor];

    

    [self.windowaddSubview:self.pView];




    新建的类的头文件

    

#import <UIKit/UIKit.h>


@interface boUIview :UIView


{

   float _lastDis;

}



@end


    

    在实现文件中

    

#import "boUIview.h"


@implementation boUIview


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self)

    {

       //开启多点触摸

        self.multipleTouchEnabled =YES;

        

        self.userInteractionEnabled =YES;

    }

    return self;

}


//触摸开始时调用该方法(UIResponder中的方法)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"touchBegin");

    

    //获取任意一个UITouch对象

   UITouch *pTouch = [touchesanyObject];

    

    NSLog(@"pTouc.timmestamp=%f",pTouch.timestamp);

    

    //获取该对象在坐标系中的位置

   CGPoint point = [pTouchlocationInView:self];

    //输出位置坐标

    NSLog(@"%@",NSStringFromCGPoint(point));

    //获取点击次数

   NSInteger tapCount = [pTouchtapCount];

    

   if(tapCount ==1)

    {

        //如果点击次数为1时调用的方法

        [selfperformSelector:@selector(singleTouch:)withObject:nilafterDelay:0.2];

    }

    

   elseif (tapCount ==2)

    {

        //如果点击次数为2,就取消对点击数为1时关联的方法的调用,并调用另一个方法。

        [NSObjectcancelPreviousPerformRequestsWithTarget:selfselector:@selector(singleTouch:)object:nil];

        

        [selfdoubleTouch:nil];

    }

}


//点击次数为1时调用的方法

- (void)singleTouch:(id)sender

{

   NSLog(@"单击");

}


//点击次数为2时关联的方法

- (void)doubleTouch:(id)sender

{

   NSLog(@"双击");

}


//自定义的方法,用来计算2个点之间的距离

- (double)distanceOfPoint:(CGPoint)point1 withPoint:(CGPoint)point2

{

    //两个点x坐标差的平方

   double numX =pow(point1.x - point2.x,2);

    //两个点y坐标差的平方

   double numY =pow(point1.y - point2.y,2);

    //2个点的距离

   double distance =sqrt(numX + numY);

    

   return distance;

}

//(UIResponder中的方法)

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"touchMoved");

    //定义一个数组来存放所有触摸对象

   NSArray *pArr = [touchesallObjects];

    //分别取出2touch对象

   UITouch *pTouch1 = [pArrobjectAtIndex:0];

    

   UITouch *pTouch2 = [pArrobjectAtIndex:1];

    //获取两个对象的坐标位置

   CGPoint point1 = [pTouch1locationInView:self];

    

   CGPoint point2 = [pTouch2locationInView:self];

    //调用自己定义的方法寄宿堂这2个点之间的距离

   double distance = [selfdistanceOfPoint:point1withPoint:point2];

    

   if((distance -_lastDis) >0)

    {

        //如果移动后的两点距离大于移动前的两点之间距离。说明是放大操作

       NSLog(@"放大");

    }

    

   else

    {

       NSLog(@"缩小");

    }

    //_lastDis表示之前两点之间的距离

   _lastDis = distance;

}

//(UIResponder中的方法)

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"tuchCancel");

}

//(UIResponder中的方法)

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"touchEnd");

}



/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/


@end




       


       



      
 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值