#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