1.功能:
手指在屏幕上每一次觸摸都會(huì)產(chǎn)生一個(gè)觸摸事件,這些事件會(huì)存儲(chǔ)在NSSet類型的touches里面探入,我們可以通過touch方法獲取這些觸摸事件
注:
(1)touch對(duì)象是不需要我們單獨(dú)創(chuàng)建的震檩,在我們觸摸屏幕時(shí)就已經(jīng)產(chǎn)生琢蛤!所以當(dāng)我們觸摸屏幕,如果寫了以下四種方法抛虏,就會(huì)直接觸發(fā)事件博其!
(2)UIImageView 的userInteractionEnabled參數(shù)設(shè)置為YES,才能觸發(fā)UIImageView的點(diǎn)擊事件
imageView.userInteractionEnabled = YES;
2.四個(gè)代理方法:
(1)touchesBegan方法:
//這個(gè)方法是在觸摸開始時(shí),調(diào)用 這個(gè)方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];//獲取touch對(duì)象
[UIView animateWithDuration:0.3 animations:^{
imageView.center = [touch locationInView:self.window];
}];
}
(2)touchesEnded方法:
//這個(gè)方法是在,手指離開屏幕時(shí),觸發(fā)這個(gè)方法
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//可以在這里不使用任何一個(gè)事件,只去寫我們自己的方法
[UIView animateWithDuration:0.3 animations:^{
imageView.center = CGPointMake(160, 300);
}];
}
(3)touchesMoved方法:
//這個(gè)方法是說,手指在屏幕上移動(dòng)時(shí),觸發(fā)這個(gè)方法
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//那么,手指只要在屏幕有偏移那么就調(diào)用一次這個(gè)方法
UITouch * touch = [touches anyObject];
//判斷touch的view是否是imageView
if (touch.view == imageView && touch.phase == UITouchPhaseMoved) {
imageView.center = [touch locationInView:self.window];}
}
(4)touchesCancelled方法:
//這個(gè)方法是觸摸被打斷時(shí)調(diào)用的方法,這個(gè)方法在模擬器上測(cè)試不了
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//程序運(yùn)行過程中,進(jìn)來電話,短信,推送等優(yōu)先級(jí)比較高的事件
}