0

I know this has been sort of answered before ( iPhone "touchesBegan" and "touchesMoved" message ... do not move to centre of touch ), but I'm very new to iPhone development so don't understand the answer.

Essentially, what I want to do is have a UIImageView which responds to touches, to move it around the screen. The sample from Apple has the view that when it has touchesBegan, moves its centre to the touch, then moves the view.

What I'd like is for the image to pan like when you use the UIImagePicker. The selected image does not snap to the touch, but instead will move from the touch.

Any help would be most appreciated.

4

1 回答 1

3

首先,您需要获取第一次触摸屏幕的点。然后对于每个“触摸移动”,获取新点的 delta x 和 y 坐标并将图像移动该数量。

// This is untested code and I can't even be sure if it compiles
// Hopefully it is verbose enough to help you with that you are
// Trying to do. If not I can update once I get back infront of 
// a Mac.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view];
    CGPoint delta = currentPoint - lastPoint;

    currentViewLoc = imageView.center;

    imageView.center.x = currentViewLoc.x - delta.x;
    imageView.center.y = currentViewLoc.y - delta.y;

    currentPoint = lastPoint;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject]; 
    currentPoint = [touch locationInView:self.view];
}

反正是这样的。我希望你明白我想说什么。

于 2010-11-21T00:21:58.563 回答