欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS觸摸事件UITouch應(yīng)用詳解

 更新時間:2017年08月12日 11:14:28   作者:杰瑞教育  
這篇文章主要為大家詳細介紹了iOS觸摸事件UITouch的應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下

因為UIView或者UIViewController都是繼承與UIResponder ,所以都有UITouch這個事件。當(dāng)用戶點擊屏幕的時候,會產(chǎn)生觸摸事件。

通過UITouch事件,可以監(jiān)聽到開始觸摸、觸摸移動過程、觸摸結(jié)束以及觸摸打斷四個不同階段的狀態(tài),在這些方法中,我們能夠獲取到很多有用的信息,比如觸摸點的坐標(biāo)、觸摸的手指數(shù)、觸摸的次數(shù)等等,下面通過一個小例子來說明一下。

詳細代碼如下:

/*
  定義屬性
 */
@interface ViewController ()
{
  CGPoint _startPoint; //開始點擊的點
  CGPoint _endPoint; //結(jié)束點擊的點
  
  UILabel *_label1; //顯示當(dāng)前觸摸的狀態(tài)的標(biāo)簽
  UILabel *_label2;
  UILabel *_label3;
  UILabel *_label4;
  UIImageView *_imageView; //笑臉圖片
}

/*
  觸摸事件UITouch的系列方法如下所示 <一>到<四>
 */

#pragma mark <一> 當(dāng)一個或多個手指觸碰屏幕時,發(fā)送touchesBegan:withEvent:消息
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  _label1.text = @"觸摸 開始 ";
  
  //1. 首先獲取觸摸屏幕的手指
  UITouch * touch = [touches anyObject];
  
  //2. 點擊的當(dāng)前點的坐標(biāo)
  CGPoint point = [touch locationInView:self.view];
  _label2.text = [NSString stringWithFormat:@"當(dāng)前點得坐標(biāo):x=%.1f, y=%.1f",point.x,point.y];
  
  //4. 獲取觸摸屏幕的次數(shù)
  int tapCount = touch.tapCount;
  //5. 獲取觸摸屏幕的手指根數(shù)
  int fingerCount = touches.count;
  
  _label3.text = [NSString stringWithFormat:@"觸摸屏幕次數(shù)為%i, 觸摸的手指數(shù)為%i",tapCount,fingerCount];
  
  //6. 當(dāng)前視圖默認只支持單點觸摸 如果想添加多點觸摸 必須開啟多點觸摸模式
  self.view.multipleTouchEnabled = YES;
  
  //7.1. 得到開始點擊的點,得到最后點擊的點,計算一下,看看做了什么操作
  _startPoint = [touch locationInView:self.view];
  _label4.text = @"";
}

#pragma mark <二> 當(dāng)一個或多個手指在屏幕上移動時,發(fā)送touchesMoved:withEvent:消息
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  _label1.text = @"觸摸 move...";
  CGPoint point = [[touches anyObject] locationInView:self.view];
  _label2.text = [NSString stringWithFormat:@"當(dāng)前點得坐標(biāo):x=%.1f, y=%.1f",point.x,point.y];
}

#pragma mark <三> 當(dāng)一個或多個手指離開屏幕時,發(fā)送touchesEnded:withEvent:消息
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  _label1.text = @"觸摸 結(jié)束";
  CGPoint point = [[touches anyObject] locationInView:self.view];
  
  //3. 判斷是否進入了圖片范圍內(nèi)
  if (CGRectContainsPoint(_imageView.frame, point)) {
    _label2.text = @"停留在笑臉圖片范圍內(nèi)";
  }
  else
  {
    _label2.text = @"停留在笑臉圖片外面";
  }
  
  //7.2 計算開始到結(jié)束偏移量
  float distanceX = fabsf(point.x - _startPoint.x);
  //獲取手指縱向移動的偏移量
  float distanceY = fabsf(point.y - _startPoint.y);
  
  _label4.text = [NSString stringWithFormat:@"x偏移了%.1f,y方向偏移了%.1f",distanceX,distanceY];
  
  _startPoint = CGPointZero;
}

#pragma mark <四> 當(dāng)觸摸序列被諸如電話呼入這樣的系統(tǒng)事件打斷所意外取消時,發(fā)送touchesCancelled:withEvent:消息-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
  _label1.text = @"觸摸 取消";
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論