在触摸屏幕的时候,触摸点的位置是十分重要的信息。得到的位置坐标是和它所处的试图或窗口相关的坐标。调用触摸对象的locationInView:方法即可,查找到触摸点在指定视图上的位置,就把视图对象传递进去。对自定义视图对象来说,这里调用[touch locationInView:self.view]来获得触摸在本视图上的位置。如果指定为nil,则返回触摸点在窗口的位置。

 
  
  1. CGPoint originalLocation;   //全局变量,用于存储起始位置 
  2. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
  3.     UITouch *touch = [touches   anyObject]; 
  4.     originalLocation = [touch locationInView:self.view]; 
  5.      
  6. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
  7.     UITouch *touch = [touches anyObject]; 
  8.     CGPoint currentLocation = [touch locationInView:self.view]; 
  9.     CGRect frame = self.view.frame; 
  10.     frame.origin.x += currentLocation.x - originalLocation.x; 
  11.     frame.origin.y += currentLocation.y - originalLocation.y; 
  12.      
  13.     self.view.frame = frame;