UITouch的方法

  • 在觸摸開始時(shí),調(diào)用 這個(gè)方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");
    
    //手指在屏幕上每一次觸摸都會(huì)產(chǎn)生一個(gè)觸摸事件
    //這些事件會(huì)保存這個(gè)NSSet 類型的 touches里
    //我們可以取出這個(gè)事件
    UITouch * touch = [touches anyObject];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.center = [touch locationInView:self.window];
    }];
    
    
    NSLog(@"window:%@",touch.window);
    NSLog(@"view:%@",touch.view);
    NSLog(@"phase:%id",touch.phase);
}

  • 手指離開屏幕時(shí),觸發(fā)這個(gè)方法
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
    //可以在這里不使用任何一個(gè)事件,只去寫我們自己的方法

    [UIView animateWithDuration:0.3 animations:^{
        imageView.center = CGPointMake(160, 300);
    }];
    
}
  • 手指在屏幕上移動(dòng)時(shí),觸發(fā)這個(gè)方法
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //那么,手指只要在屏幕有偏移那么就調(diào)用一次這個(gè)方法
    NSLog(@"touchesMove");
    UITouch * touch = [touches anyObject];
    NSLog(@"view:%@",touch.view);
    if (touch.view == imageView && touch.phase == UITouchPhaseMoved) {
        imageView.center = [touch locationInView:self.window];
 }
  • 觸摸被打斷時(shí)調(diào)用的方法
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    //程序運(yùn)行過程中,進(jìn)來電話,短信,推送等優(yōu)先級(jí)比較高的事件
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容