#import "VCRoot.h" @interface VCRoot () @end @implementation VCRoot - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _iView = [[UIImageView alloc] init] ; _iView.frame = CGRectMake(40, 60, 160, 240); _iView.image = [UIImage imageNamed:@"8.png"] ; [self.view addSubview:_iView] ; } //觸屏事件函數: //是UIRespoder的成員方法 //UIRespoder是UIViewController的父親類 //UIRespoder是能夠進行事件(屏幕)處理的類 //當開始點擊屏幕時調用 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"開始點擊屏幕!"); //得到觸屏手勢的數據對象 //touches爲NSSet集合類型 //anyObject從集合中選出一個對象 //從集合中選出"任意"對象 //若是集合中只有一個對象 //anyObject會返回這個集合中惟一的對象 // UITouch* touch = [touches anyObject] ; //得到手指點擊屏幕的次數,兩次點擊之間的時間間隔小於0.5秒 //單次點擊時:tapCount == 1 //雙次點擊:tapCount == 2 //三次點擊: tapCount == 3 NSUInteger tapCount = touch.tapCount ; if (touch.phase == UITouchPhaseBegan) { NSLog(@"開始!!!"); } else if (touch.phase == UITouchPhaseEnded ) { NSLog(@"end!"); } // [self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)] //touch.view == self.view; //得到當前點擊手指相對於當前視圖self.view的位置 //點的相對座標原點能夠經過參數一的視圖設定 //返回一個點的結構體對象 CGPoint pt = [touch locationInView:self.view]; NSLog(@"tapCount = %ld",tapCount) ; NSLog(@"pt.x = %f",pt.x) ; NSLog(@"pt.y = %f",pt.y) ; } //當點擊結束時,手指離開屏幕的時刻(瞬間)調用 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"手指離開屏幕!"); UITouch* touch = [touches anyObject] ; if (touch.tapCount == 1) { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1]; _iView.frame = CGRectMake(0, 0, 320, 480); [UIView commitAnimations] ; } else if (touch.tapCount == 2) { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1] ; _iView.frame = CGRectMake(40, 60, 160, 240); [UIView commitAnimations] ; } } //當手指在屏幕上移動時調用 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"手指移動!"); UITouch* touch = [touches anyObject] ; //相對於當前視圖座標 CGPoint pt = [touch locationInView:self.view] ; //改變圖像視圖的位置 //_iView.frame = CGRectMake(pt.x, pt.y, _iView.frame.size.width, _iView.frame.size.height) ; //方法二 _iView.center = pt ; } //當觸屏操做被取消(中斷)時調用 //電話打入,或緊急事件調用時,手勢被取消時調用 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"中斷!"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end