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

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

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

因?yàn)閁IView或者UIViewController都是繼承與UIResponder ,所以都有UITouch這個(gè)事件。當(dāng)用戶點(diǎn)擊屏幕的時(shí)候,會(huì)產(chǎn)生觸摸事件。

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

詳細(xì)代碼如下:

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

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

#pragma mark <一> 當(dāng)一個(gè)或多個(gè)手指觸碰屏幕時(shí),發(fā)送touchesBegan:withEvent:消息
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  _label1.text = @"觸摸 開始 ";
  
  //1. 首先獲取觸摸屏幕的手指
  UITouch * touch = [touches anyObject];
  
  //2. 點(diǎn)擊的當(dāng)前點(diǎn)的坐標(biāo)
  CGPoint point = [touch locationInView:self.view];
  _label2.text = [NSString stringWithFormat:@"當(dāng)前點(diǎn)得坐標(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)前視圖默認(rèn)只支持單點(diǎn)觸摸 如果想添加多點(diǎn)觸摸 必須開啟多點(diǎn)觸摸模式
  self.view.multipleTouchEnabled = YES;
  
  //7.1. 得到開始點(diǎn)擊的點(diǎn),得到最后點(diǎn)擊的點(diǎn),計(jì)算一下,看看做了什么操作
  _startPoint = [touch locationInView:self.view];
  _label4.text = @"";
}

#pragma mark <二> 當(dāng)一個(gè)或多個(gè)手指在屏幕上移動(dòng)時(shí),發(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)前點(diǎn)得坐標(biāo):x=%.1f, y=%.1f",point.x,point.y];
}

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

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

相關(guān)文章

最新評(píng)論