UIEvent 事件是有設備捕獲到用戶對硬件的操作, 每一個事件都是一個UIEvent對象, iOS中的事件分三種: 觸摸事件, 搖晃事件, 遠程控制事件
觸摸事件: 是有用戶對屏幕通過觸摸產生的事件, 對於UIView 或UIView的子類, 都是能夠接收到觸摸事件的, 只是沒有對觸摸事件作出響應, iOS支持多點觸摸, 若一個視圖相對觸摸事件作出響應, 只需在該類中實現, touchesBegan:, touchesEnded:, touchesMoved:等觸摸方法即可
搖晃事件
遠程控制事件
//當手指觸摸屏幕時觸發(剛開始接觸)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s", __FUNCTION__);
//隨機色
//touches 存儲觸摸屏幕的手指對象
//每一個手指對象都是一個UITouch類型的對象
NSLog(@"%lu", (unsigned long)touches.count);
//單擊 改變自身顏色
// 1 獲取手指對象
UITouch *touch1 = [touches anyObject];
//2 獲取手指點擊的次數
if ([touch1 tapCount] == 1) {
// [self changeSelfColor];
[self performSelector:@selector(changeSelfColor) withObject:nil afterDelay:0.5];
}else if ([touch1 tapCount] == 2) {
//取消之前的任務
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeSelfColor) object:nil];
[self changeSuperviewColor];
}
}
//當觸摸被取消時, 前提是手指必須觸摸屏幕(例如, 電話進入時)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s", __FUNCTION__);
}
//當手指離開屏幕時觸發(接觸結束)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s", __FUNCTION__);
// [self changeSelfColor];
// [self changeSuperviewColor];
// [self changeSelfLocation];
}
//當手指觸摸屏幕, 並且在視圖內移動時觸發(此時手指不離開屏幕)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self changeSelfColor];
// [self changeSuperviewColor];
// self.center = CGPointMake(arc4random() % (270 - 50 + 1) + 50, arc4random() % (518 - 50 + 1) + 50 );
}
//修改自身視圖顏色
- (void)changeSelfColor
{
self.backgroundColor = [UIColor randomColor];
}
//修該父視圖顏色
- (void)changeSuperviewColor
{
self.superview.backgroundColor = [UIColor randomColor];
}
- (void)changeSelfLocation
{
self.center = CGPointMake(arc4random() % (270 - 50 + 1) + 50, arc4random() % (518 - 50 + 1) + 50 );
}
//改變自身視圖位置
UIEvent,觸摸事件,touchesBegan:, touchesEnded:, touchesMoved:等觸摸方法,單擊改變自身顏色, 雙擊改變俯視圖顏色
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.