在触摸屏幕的时候,触摸点的位置是十分重要的信息。得到的位置坐标是和它所处的试图或窗口相关的坐标。调用触摸对象的locationInView:方法即可,查找到触摸点在指定视图上的位置,就把视图对象传递进去。对自定义视图对象来说,这里调用[touch locationInView:self.view]来获得触摸在本视图上的位置。如果指定为nil,则返回触摸点在窗口的位置。
CGPoint originalLocation; //全局变量 用于存储起始位置-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; originalLocation = [touch locationInView:self.view]; }-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint currentLocation = [touch locationInView:self.view]; CGRect frame = self.view.frame; frame.origin.x += currentLocation.x - originalLocation.x; frame.origin.y += currentLocation.y - originalLocation.y; self.view.frame = frame; }