帮助我理解iPhone的touchesBegan实现和拖动UIView

发布于 2024-10-03 22:14:23 字数 347 浏览 1 评论 0原文

我知道这个问题之前已经得到了回答(iPhone“touchesBegan”和“touchesMoved”消息......不要移动到触摸中心 ),但我对 iPhone 开发非常陌生,所以不明白答案。

本质上,我想做的是有一个 UIImageView 响应触摸,在屏幕上移动它。来自Apple的示例具有这样的视图:当它有touchesBegan时,将其中心移动到触摸处,然后移动视图。

我想要的是图像像使用 UIImagePicker 时那样平移。所选图像不会随着触摸而对齐,而是会随着触摸而移动。

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-10-10 22:14:23

首先,您需要找到第一次触摸屏幕的点。然后,对于每个“触摸移动”,获取新点的增量 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];
}

无论如何。我希望你能明白我想说的。

First you need to get the point where you first touched the screen. Then for each 'touch moved' get the delta x and y coordinates for the new point and move the image by that amount.

// 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];
}

something like that anyway. I hope you get the idea of what I am trying to say.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文