UI第四天:事件處理

⼀、事件的基本概念
UIEvent:事件,是由硬件捕捉的⼀個表⽰⽤戶操作設備的對象。
分三類:觸摸事件、晃動事件、遠程控制事件
觸摸事件:⽤戶通過觸摸設備屏幕操作對象、輸⼊數據。⽀持多點 觸摸,包含1個到多個觸摸點

⼆、觸摸的基本概念
UIView⽀持觸摸事件(因爲繼承於UIResponder),⽽且⽀持多 點觸摸。
需要定義UIView⼦類,實現觸摸相關的⽅法。
touches..began、touches..moved、touches...ended、 touches..canceled。
⼿勢:有規律的觸摸。
UITouch代表觸摸在屏幕上的⼀根⼿指。
可以獲取觸摸時間和觸摸 位置。
如何獲取touch對象。
touches集合中包含了視圖上的所有⼿勢
// 需要讓touchview響應事件
   
// 實現響應者類中的方法 來捕捉出摸事件
   
TouchView *touch = [[TouchView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    touch.
backgroundColor = [UIColor redColor];
    [
self.view addSubview:touch];
    [touch release];

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
實現一個可拖動可變色的TouchView
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // touch 保留手指信息(觸摸的點)
    UITouch *touch = [touches anyObject];
   // NSLog(@"%@",touch);
    //取出當前觸摸的點
    //返回一個當前觸摸的點相對於傳進去的參數view
    CGPoint p = [touch locationInView:self.window];
    NSLog(@"%@",NSStringFromCGPoint(p));
    CGPoint p1 = [touch previousLocationInView:self.window] ;
    NSLog(@"%@",NSStringFromCGPoint(p1));
    CGPoint p2;
    p2.x = p.x - p1.x ;
    p2.y = p.y - p1.y ;
    NSLog(@"%@",NSStringFromCGPoint(self.center));
    self.center = CGPointMake(self.center.x + p2.x, self.center.y +p2.y);
   
    self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0  blue:arc4random()%256/255.0  alpha:1];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"觸摸中斷比如小退");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"觸摸結束");
}
// UIActionSheet的使用方法是一個從下往上的彈框用於刪除時
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%@",[actionSheet buttonTitleAtIndex:buttonIndex]);
   
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"確認刪除" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles:@"額外", nil];
    [sheet showInView:self.window];
    [sheet release];

三、響應者鏈
由多個響應者對象組成的鏈。
什麼是響應者
UIResponder。響應者類。
iOS中所有能響應事件(觸摸、晃動、遠程事件)的對象都是響應 者。
系統定義了⼀個抽象的⽗類UIResponder來表⽰響應者。其⼦類都 是響應者。
檢測觸碰視圖
硬件檢測到觸摸操作,會將信息交給UIApplication,開始檢測。
UIApplication -> window -> viewController -> view -> 檢測所有⼦ 視圖 最終確認觸碰位置,完成響應者鏈的查詢過程。
處理觸碰事件
檢測到響應者後,實現touchesBegan:withEvent:等⽅法,即處理事 件。
如果響應者沒有處理事件,事件會向下傳遞。如果沒有響應者處理, 則丟棄觸摸事件。
事件處理的順序與觸摸檢測查詢相反。
觸摸的⼦視圖 -> view -> viewController -> window -> UIApplication
阻斷響應者鏈
響應者鏈可以被打斷。⽆法完成檢測查詢過程。
視圖類的屬性 : userInteractionEnabled。關閉後能阻斷查詢過 程。
總結
今天我們學習了事件和觸摸的基本概念
學習如何響應和處理觸摸事件
學習了響應者和響應者鏈

 /*
    
響應者鏈 分爲兩個過程
     1.
查詢過程
    
當你點擊屏幕
    
先定位到 應用程序->window->控制器->self.view->view子視圖一一查找 直到定位被點擊的子視圖 查詢過程結束
     2.
響應過程
     首先 看本視圖能不能處理事件(實現了touchBange等方法 就叫做可以處理事件)->父視圖->一層一層往下看能不能處理 直到window 如果都不能處理該次點擊事件 被遺棄(無效點擊)
    //    button.userInteractionEnabled = NO;阻斷button查詢來阻斷響應者鏈
     注意:UILabel UIImageView的交互 默認是關閉的
     */
// 晃動事件
//開始晃動
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"開始晃動");
   
}
//中止晃動
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"中止晃動");
}
//結束晃動
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"結束晃動");
    //跳轉界面
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    [self presentViewController:secondVC animated:YES completion:nil];
}

自定義UIview來自己寫一個button實現button方法
//爲了方便下面調用聲明成屬性
@property(nonatomic,retain)id target;
@property(nonatomic,assign)SEL action;//方法類型
-(instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action;

// 重寫初始化方法
-(instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
    self = [super initWithFrame:frame];
    if (self) {
        self.target =target;
        self.action =action;
    }
    return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //NSLog(@"點擊");
    //使用self.target 對象調用action方法
    //讓一個對象去調用這個對象類裏的方法
    //object 可攜帶的參數
    [self.target performSelector:self.action withObject:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent
*)event
{
   
}
發佈了33 篇原創文章 · 獲贊 0 · 訪問量 7618
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章