事件的基本概念

用戶在使用App的過程當中會產生各類事件:code

  • 觸摸事件(手勢與屏幕交互)
  • 加速計事件(搖一搖)
  • 遠程控制事件(耳麥)

只有繼承自UIResponder的對象才能接受並處理事件,這類對象稱爲響應者對象orm

  • UIApplication
  • UIViewController
  • UIView
  • ...對象

產生相應交互時,系統會自動調用相關事件繼承

  • 觸摸:touchesBegan:
  • 移動:touchesMoved:
  • 離開:touchesEnded:
  • 系統事件打斷觸摸:touchesCancelled:

UITouch對象事件

  • 做用:保存了觸摸的相關信息
  • 經過相應屬性與方法獲取

UIEvent對象it

  • 事件對象,記錄了事件產生的時刻和類型
  • 還提供了相應方法能夠得到在某個View上面的觸摸對象UITouch

實現View隨手指拖拽而移動io

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //0.獲取UITouch對象
    UITouch *touch = [touches anyObject];//當前只有一個UITouch對象,可使用anyObject
    //1.獲取手指當前觸摸點
    CGPoint curP = [touch locationInView:self];
    //2.獲取手指上一個觸摸點
    CGPoint preP = [touch previousLocationInView:self];
    //3.計算偏移量
    CGFloat offsetX = curP.x - preP.x;
    CGFloat offsetY = curP.y - preP.y;
    //4.修改View的位置
    //MakeTranslation:會清空上一次的形變
    //self.transform = CGAffineTransformMakeTranslation(offsetX, 0);
    self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
}
相關文章
相關標籤/搜索