touches,motion觸摸事件響應,touchesmotion
//觸摸事件響應需要重寫方法
1 // 觸摸時觸發該方法(訊息發送) 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 3 UITouch *touch = [touches anyObject]; 4 CGPoint point = [touch locationInView:self.rootView.touchView];//locationInView:得到當前點擊下在指定視圖中位置的座標 5 NSLog(@"觸摸開始:%.2f,%.2f",point.x,point.y); 6 } 7 //但觸摸移動時觸發該事件 8 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 9 10 UITouch *touch = [touches anyObject];11 CGPoint point1 = [touch locationInView:self.rootView.touchView];12 CGPoint point2 = [touch previousLocationInView:self.rootView.touchView];//previouslocationInView:得到當前點的前一個點在指定視圖中位置的座標13 }14 // 觸摸結束時觸發該方法(訊息發送)15 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{16 NSLog(@"觸摸結束");17 }18 //發生其他情況,如來電話或意外退出時,執行該方法19 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{20 21 }
//手機搖一搖實現要重寫方法
1 //開始時調用 2 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ 3 NSLog(@"搖一搖開始"); 4 } 5 //結束時調用 6 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ 7 NSLog(@"搖一搖結束"); 8 } 9 //意外情況調用10 - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{11 NSLog(@"意外退出");12 }