UI基礎:事件.響應鏈 分類: iOS學習-UI 2015-07-03 19:51 1人閱讀 評論(0) 收藏

UIEvent:事件,是由硬件捕捉的一個表明用戶操做操做設備的對象.
事件分三類:觸摸事件.晃動事件.遠程控制事件.數組

觸摸事件:用戶經過觸摸設備屏幕操做對象,.輸入數據.支持多點觸摸,包含1個到多個觸摸點.
UIView支持觸摸事件(繼承了UIResponder),並且支持多點觸摸
使用時,須要定義UIView子類,重寫觸摸相關的方法.
1.剛開始觸摸到屏幕的時候觸發markdown

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ }

2.觸摸事件被意外中斷的時候觸發(如:來電)dom

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ }

3.當手指離開屏幕的時候觸發ui

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ }

4.當手指在視圖上移動的時候觸發spa

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ }

UITouch 點擊類
touchs 存放手指對象
如:code

1.當單擊時
UITouch *touch=[touches anyObject]; 
//獲取手指對象的數組
2.但雙擊時
NSArray *allTouch=[touches allObjects];
//獲取手指對象
UITouch *touch1=[allTouch firstObject];
UITouch *touch2=[allTouch lastObject];

手勢:有規律的觸摸
UITouch表明觸摸在屏幕上的一根手指,能夠獲取觸摸時間和觸摸位置.
獲取touch對象:touches集合中包含了視圖上的全部手勢.regexp


響應者鏈
有多個響應者對象組成的鏈.
UIResponder 響應者類
iOS中全部能響應事件(觸摸.晃動.遠程事件)的對象都是響應者.
系統定義了一個抽象的父類UIResponder來表示響應者.其子類都是響應者.orm

檢測觸碰視圖
硬件檢測到觸摸操做,會將信息交給UIApplication,開始檢測.
檢測過程:
UIApplication -> window ->viewController ->view ->檢測全部子視圖
處理觸摸事件:
事件處理的順序與觸摸檢測查詢相反.(本身的用戶交互關閉,就交給父類處理)
觸摸的子視圖 -> view -> viewController ->window ->UIApplication對象

阻斷響應者鏈
響應者鏈能夠被打斷.沒法完成檢測查詢過程.
視圖類的屬性:userInteractionEnabled 關閉後能夠阻斷查詢過程繼承

__其中,下面是在事件裏面幾個簡單程序的主要代碼段
1.實現點擊屏幕一次換顏色.點擊兩次換父視圖的顏色

//當手指離開屏幕的時候觸發
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"%s",__FUNCTION__);
    // UITouch 點擊類
    // touchs 存放手指對象
    UITouch *touch=[touches anyObject];
    if (1==touch.tapCount) {
        // 當視圖識別爲單擊,延遲執行之下的方法,看是否會有再次點擊
        [self performSelector:@selector(changeMyselfBackGroundColor) withObject:nil afterDelay:0.3];
// self.backgroundColor=[UIColor randomColor];

    }else if(2==touch.tapCount){
// 當識別爲雙擊的時候,取消以前的操做
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeMyselfBackGroundColor) object:nil];
        self.superview.backgroundColor=[UIColor randomColor];
    }

}

-(void)changeMyselfBackGroundColor{
    self.backgroundColor=[UIColor randomColor];
}

其中,對UIcolor進行了類目,使其有個隨機顏色的功能

@implementation UIColor (Random)
+(UIColor *)randomColor{
    return [UIColor colorWithRed:COLORVALUE green:COLORVALUE blue:COLORVALUE alpha:1.0];

}
@end

2.實現縮放視圖的功能

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    if (1==touches.count) {
        return;
    }else{

        // 獲取手指對象的數組
        NSArray *allTouch=[touches allObjects];
        //獲取手指對象
        UITouch *touch1=[allTouch firstObject];
        UITouch *touch2=[allTouch lastObject];
        // 獲取兩個手指的當前位置
        CGPoint currenFirstPoint=[touch1 locationInView:self];
        CGPoint currenSecondPoint=[touch2 locationInView:self];
        // 得到兩個手指當前的距離
        CGFloat currentDistance=[self distanFromPoint:currenFirstPoint toPoint:currenSecondPoint];
        // 獲取以前兩手指的位置
        CGPoint previousFirstPoint=[touch1 previousLocationInView:self];
        CGPoint previousSecondPoint=[touch2 previousLocationInView:self];
        // 獲取以前兩手指之間的距離
        CGFloat currentsDistance=[self distanFromPoint:previousFirstPoint toPoint:previousSecondPoint];
        // 獲取比例
        CGFloat rate=currentDistance/currentsDistance;
        // 縮放視圖,中心點不變,修改bounds便可
        self.bounds=CGRectMake(0, 0, self.frame.size.width*rate, self.frame.size.height*rate);



    }



}
//封裝計算兩點之間的距離的方法
-(CGFloat)distanFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint{
    CGFloat dx=fromPoint.x-toPoint.x;
    CGFloat dy=fromPoint.y-toPoint.y;
    return sqrt(dx*dx+dy*dy);
}

3.實現視圖隨着手指移動

@implementation MoveView

//觸摸的方法不必徹底實現
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    //先獲取手指對象
    UITouch *touch=[touches anyObject];
// 獲取自身視圖手指當前的手指位置
    CGPoint currenPoint=[touch locationInView:self];
// 獲取手指以前的位置
    CGPoint perviousPoint=[touch previousLocationInView:self];
// 計算移動的增量
    CGFloat dx=currenPoint.x-perviousPoint.x;
    CGFloat dy=currenPoint.y-perviousPoint.y;

    CGPoint center=self.center;
    self.center=CGPointMake(center.x+dx, center.y+dy);

}
@end
相關文章
相關標籤/搜索