iOS基礎:UITouch

一、UITouch事件

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    NSLog(@"觸摸");
    
    //1、拿到觸摸對象
    UITouch * touch = [touches anyObject];
    
    //2、拿到觸摸的視圖
    UIView * view = touch.view;
    
    //3、拿到觸摸時間
    NSTimeInterval time = touch.timestamp;
    
    //4、觸摸的狀態  這是一個枚舉類型
    UITouchPhase phase = touch.phase;
    
    //5、拿到上一次觸摸的座標
    CGPoint point = [touch previousLocationInView:self.view];
    
    //6、拿到當前觸摸的座標
    CGPoint point1 = [touch locationInView:self.view];
    
    
}


//2.觸摸移動的時候會調用
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"觸摸移動的時候會調用");
    UITouch * touch = [touches anyObject];
    //拿到上一次觸摸的座標
    CGPoint point = [touch previousLocationInView:self.view];
    //拿到當前觸摸的座標
    CGPoint point1 = [touch locationInView:self.view];
    //算出距離  上一次和當前座標的距離
    CGSize size = CGSizeMake(point1.x-point.x, point1.y-point.y);
    //把偏移量加到 x y 值上。
    self.myView.center = CGPointMake(self.myView.center.x+size.width, self.myView.center.y+size.height);
}


//3.觸摸結束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    NSLog(@"觸摸結束");
}

//4.觸摸取消(比如來電的時候會取消觸摸)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"觸摸取消");
    
}

參考文章:

1、iOS_UITouch 事件 - 簡書

2、UITouch 觸摸事件處理(實例) - Just Code - ITeye技術網站

3、locationInView - zhangkongzhongyun的專欄 - 博客頻道 - CSDN.NET

發佈了126 篇原創文章 · 獲贊 8 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章