你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS觸摸手勢——UITouch

iOS觸摸手勢——UITouch

編輯:IOS開發綜合

UITouch,主要是重寫四個方法(觸摸開始、觸摸移動、觸摸結束、觸摸退出)以實現觸摸響應方法

1、觸摸開始- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { }

2、觸摸移動- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { }

3、觸摸結束- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { }

4、觸摸退出(注意:通常是受到設備其他方法的影響:如來電、電量不足關機等)- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }

使用注意事項:

1、觸摸開始方法中,一般實現的屬性有:

1-1、獲取觸摸對象,如:

UITouch*touch=touches.anyObject;

1-2、觸摸次數,以便判斷實現對應方法,如:

NSIntegerclickNumber=touch.tapCount;

NSLog(@"clicknumber=%@",@(clickNumber));

1-3、觸摸位置是否用戶想要處理的子視圖,如:

CGPointcurrentPoint=[touchlocationInView:self.view];

if(CGRectContainsPoint(self.label.frame,currentPoint))

{

//改變標題

self.label.text=@"觸摸位置在當前label";

}

2、觸摸移動方法中,一般實現的屬性有:

2-1、獲取觸摸對象,如:

UITouch *touch = touches.anyObject;

2-2、獲取當前點擊位置,以及上一個點擊位置,如:

CGPointcurrentPoint=[touchlocationInView:self.view];

CGPointpreviousPoint=[touchpreviousLocationInView:self.view];

注:可通過觸摸移動改變子視圖的位置,如:

floatmoveX=currentPoint.x-previousPoint.x;

floatmoveY=currentPoint.y-previousPoint.y;

CGFloatcenterX=self.label.center.x+moveX;

CGFloatcenterY=self.label.center.y+moveY;

self.label.center=CGPointMake(centerX,centerY);

//觸摸開始

-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event

{

//self.view.backgroundColor=[UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1.0];

NSLog(@"觸摸手勢開始");

//獲取觸摸對象

UITouch*touch=touches.anyObject;

//獲取觸摸次數

NSIntegerclickNumber=touch.tapCount;

NSLog(@"clicknumber=%@",@(clickNumber));

if(2==clickNumber)

{

self.label.backgroundColor=[UIColorredColor];

}

elseif(3==clickNumber)

{

self.label.backgroundColor=[UIColorgreenColor];

}

//獲取觸摸狀態

UITouchPhasestate=touch.phase;

NSLog(@"state=%ld",state);

//獲取響應視圖對象

UIView*view=touch.view;

NSLog(@"view=%@",view);

//獲取當前觸摸位置

CGPointcurrentPoint=[touchlocationInView:self.view];

NSLog(@"touch.locationInView={%2.3f,%2.3f}",currentPoint.x,currentPoint.y);

//獲取當前觸摸的前一個位置

CGPointpreviousPoint=[touchpreviousLocationInView:self.view];

NSLog(@"touch.previousLocationInView={%2.3f,%2.3f}",previousPoint.x,previousPoint.y);

//獲取觸摸區域的子視圖

//方法1

//for(UIView*oneViewinself.view.subviews)

//{

//if([oneViewisKindOfClass:[UILabelclass]])

//{

////是否選中圖標(當前坐標是否包含所選圖標)

//if(CGRectContainsPoint(oneView.frame,currentPoint))

//{

////獲取當前被選擇圖標

//UILabel*selectedView=(UILabel*)oneView;

////改變標題

//selectedView.text=@"觸摸位置在當前label";

//}

//}

//}

//方法2

if(CGRectContainsPoint(self.label.frame,currentPoint))

{

//改變標題

self.label.text=@"觸摸位置在當前label";

}

}

//觸摸移動

-(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event

{

//self.view.backgroundColor=[UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1.0];

NSLog(@"觸摸手勢移動");

//移動UI視圖控件

//方法1獲得觸摸點的集合,可以判斷多點觸摸事件

for(UITouch*touchinevent.allTouches)

{

//獲取當前觸摸坐標

CGPointcurrentPoint=[touchlocationInView:self.view];

NSLog(@"touch.locationInView={%2.3f,%2.3f}",currentPoint.x,currentPoint.y);

//獲取當前觸摸前一個坐標

CGPointpreviousPoint=[touchpreviousLocationInView:self.view];

NSLog(@"touch.previousLocationInView={%2.3f,%2.3f}",previousPoint.x,previousPoint.y);

//坐標偏移量(效果異常??)

floatmoveX=currentPoint.x-previousPoint.x;

floatmoveY=currentPoint.y-previousPoint.y;

CGFloatcenterX=self.label.center.x+moveX;

CGFloatcenterY=self.label.center.y+moveY;

self.label.center=CGPointMake(centerX,centerY);

}

//方法2

//UITouch*touch=touches.anyObject;

////獲取當前觸摸坐標

//CGPointcurrentPoint=[touchlocationInView:self.view];

//NSLog(@"touch.locationInView={%2.3f,%2.3f}",currentPoint.x,currentPoint.y);

////獲取當前觸摸前一個坐標

//CGPointpreviousPoint=[touchpreviousLocationInView:self.view];

//NSLog(@"touch.previousLocationInView={%2.3f,%2.3f}",previousPoint.x,previousPoint.y);

////坐標偏移量

//floatmoveX=currentPoint.x-previousPoint.x;

//floatmoveY=currentPoint.y-previousPoint.y;

//CGFloatcenterX=self.label.center.x+moveX;

//CGFloatcenterY=self.label.center.y+moveY;

//self.label.center=CGPointMake(centerX,centerY);

}

//觸摸結束

-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event

{

//self.view.backgroundColor=[UIColorwhiteColor];

NSLog(@"觸摸手勢結束");

self.label.text=@"touch觸摸手勢";

}

//觸摸退出(注意:通常是受到設備其他方法的影響:如來電、電量不足關機等)

-(void)touchesCancelled:(NSSet*)toucheswithEvent:(UIEvent*)event

{

NSLog(@"觸摸手勢退出");

}

效果圖

 

\
\
\ITouch,主要是重寫四個方法(觸摸開始、觸摸移動、觸摸結束、觸摸退出)以實現觸摸響應方法

1、觸摸開始- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { }

2、觸摸移動- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { }

3、觸摸結束- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { }

4、觸摸退出(注意:通常是受到設備其他方法的影響:如來電、電量不足關機等)- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }

使用注意事項:

1、觸摸開始方法中,一般實現的屬性有:

1-1、獲取觸摸對象,如:

UITouch*touch=touches.anyObject;

1-2、觸摸次數,以便判斷實現對應方法,如:

NSIntegerclickNumber=touch.tapCount;

NSLog(@"clicknumber=%@",@(clickNumber));

1-3、觸摸位置是否用戶想要處理的子視圖,如:

CGPointcurrentPoint=[touchlocationInView:self.view];

if(CGRectContainsPoint(self.label.frame,currentPoint))

{

//改變標題

self.label.text=@"觸摸位置在當前label";

}

2、觸摸移動方法中,一般實現的屬性有:

2-1、獲取觸摸對象,如:

UITouch *touch = touches.anyObject;

2-2、獲取當前點擊位置,以及上一個點擊位置,如:

CGPointcurrentPoint=[touchlocationInView:self.view];

CGPointpreviousPoint=[touchpreviousLocationInView:self.view];

注:可通過觸摸移動改變子視圖的位置,如:

floatmoveX=currentPoint.x-previousPoint.x;

floatmoveY=currentPoint.y-previousPoint.y;

CGFloatcenterX=self.label.center.x+moveX;

CGFloatcenterY=self.label.center.y+moveY;

self.label.center=CGPointMake(centerX,centerY);

//觸摸開始

-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event

{

//self.view.backgroundColor=[UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1.0];

NSLog(@"觸摸手勢開始");

//獲取觸摸對象

UITouch*touch=touches.anyObject;

//獲取觸摸次數

NSIntegerclickNumber=touch.tapCount;

NSLog(@"clicknumber=%@",@(clickNumber));

if(2==clickNumber)

{

self.label.backgroundColor=[UIColorredColor];

}

elseif(3==clickNumber)

{

self.label.backgroundColor=[UIColorgreenColor];

}

//獲取觸摸狀態

UITouchPhasestate=touch.phase;

NSLog(@"state=%ld",state);

//獲取響應視圖對象

UIView*view=touch.view;

NSLog(@"view=%@",view);

//獲取當前觸摸位置

CGPointcurrentPoint=[touchlocationInView:self.view];

NSLog(@"touch.locationInView={%2.3f,%2.3f}",currentPoint.x,currentPoint.y);

//獲取當前觸摸的前一個位置

CGPointpreviousPoint=[touchpreviousLocationInView:self.view];

NSLog(@"touch.previousLocationInView={%2.3f,%2.3f}",previousPoint.x,previousPoint.y);

//獲取觸摸區域的子視圖

//方法1

//for(UIView*oneViewinself.view.subviews)

//{

//if([oneViewisKindOfClass:[UILabelclass]])

//{

////是否選中圖標(當前坐標是否包含所選圖標)

//if(CGRectContainsPoint(oneView.frame,currentPoint))

//{

////獲取當前被選擇圖標

//UILabel*selectedView=(UILabel*)oneView;

////改變標題

//selectedView.text=@"觸摸位置在當前label";

//}

//}

//}

//方法2

if(CGRectContainsPoint(self.label.frame,currentPoint))

{

//改變標題

self.label.text=@"觸摸位置在當前label";

}

}

//觸摸移動

-(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event

{

//self.view.backgroundColor=[UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1.0];

NSLog(@"觸摸手勢移動");

//移動UI視圖控件

//方法1獲得觸摸點的集合,可以判斷多點觸摸事件

for(UITouch*touchinevent.allTouches)

{

//獲取當前觸摸坐標

CGPointcurrentPoint=[touchlocationInView:self.view];

NSLog(@"touch.locationInView={%2.3f,%2.3f}",currentPoint.x,currentPoint.y);

//獲取當前觸摸前一個坐標

CGPointpreviousPoint=[touchpreviousLocationInView:self.view];

NSLog(@"touch.previousLocationInView={%2.3f,%2.3f}",previousPoint.x,previousPoint.y);

//坐標偏移量(效果異常??)

floatmoveX=currentPoint.x-previousPoint.x;

floatmoveY=currentPoint.y-previousPoint.y;

CGFloatcenterX=self.label.center.x+moveX;

CGFloatcenterY=self.label.center.y+moveY;

self.label.center=CGPointMake(centerX,centerY);

}

//方法2

//UITouch*touch=touches.anyObject;

////獲取當前觸摸坐標

//CGPointcurrentPoint=[touchlocationInView:self.view];

//NSLog(@"touch.locationInView={%2.3f,%2.3f}",currentPoint.x,currentPoint.y);

////獲取當前觸摸前一個坐標

//CGPointpreviousPoint=[touchpreviousLocationInView:self.view];

//NSLog(@"touch.previousLocationInView={%2.3f,%2.3f}",previousPoint.x,previousPoint.y);

////坐標偏移量

//floatmoveX=currentPoint.x-previousPoint.x;

//floatmoveY=currentPoint.y-previousPoint.y;

//CGFloatcenterX=self.label.center.x+moveX;

//CGFloatcenterY=self.label.center.y+moveY;

//self.label.center=CGPointMake(centerX,centerY);

}

//觸摸結束

-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event

{

//self.view.backgroundColor=[UIColorwhiteColor];

NSLog(@"觸摸手勢結束");

self.label.text=@"touch觸摸手勢";

}

//觸摸退出(注意:通常是受到設備其他方法的影響:如來電、電量不足關機等)

-(void)touchesCancelled:(NSSet*)toucheswithEvent:(UIEvent*)event

{

NSLog(@"觸摸手勢退出");

}

效果圖

 

\
\
\
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved