iOS 事件處理之UIResponder簡介,iosuiresponder
- 在使用者使用app過程中,會產生各種各樣的事件
- iOS中的事件可以分為3大類型:觸摸事件、加速計事件、遠端控制事件
- 在iOS中不是任何對象都能處理事件,只有繼承了UIResponder的對象才能接收並處理事件。我們稱之為“響應者對象”
- UIApplication、UIViewController、UIView都繼承自UIResponder,因此它們都是響應者對象,都能夠接收並處理事件
// 當手指開始觸摸view// NSArray,字典,NSSet(無序)- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"%ld", touches.count); NSLog(@"%s",__func__);}// 當手指在view上移動的時候- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"%s",__func__); // 擷取UITouch對象 UITouch *touch = [touches anyObject]; // 擷取當前點 CGPoint curP = [touch locationInView:self]; // 擷取上一個點 CGPoint preP = [touch previousLocationInView:self]; // 擷取x軸位移量 CGFloat offsetX = curP.x - preP.x; // 擷取y軸位移量 CGFloat offsetY = curP.y - preP.y; // 修改view的位置(frame,center,transform) self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY); // self.transform = CGAffineTransformMakeTranslation(offsetX, 0); }// 當手指離開這個view的時候- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"%s",__func__);}// 當觸摸事件被打斷的時候調用(電話打入)- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"%s",__func__);}