用戶在使用App的過程當中會產生各類事件:code
只有繼承自UIResponder的對象才能接受並處理事件,這類對象稱爲響應者對象orm
...對象
產生相應交互時,系統會自動調用相關事件繼承
touchesBegan:
touchesMoved:
touchesEnded:
touchesCancelled:
UITouch對象事件
UIEvent對象it
實現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); }