xcode 触摸并在屏幕周围拖动图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8640103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:50:17  来源:igfitidea点击:

Touch and drag an image around the screen

iphoneobjective-ciosxcodeipad

提问by user964627

I have three images that I want the user to move around the screen. For example you have a football, baseball, and a basketball. I would like the user to select one of the images and then move that image around on the screen without affecting the other two. I watched some tutorials on make a image move around on the screen but what I am having a hard time moving around several images.

我有三个图像,我希望用户在屏幕上移动。例如,您有橄榄球、棒球和篮球。我希望用户选择其中一个图像,然后在屏幕上移动该图像而不影响其他两个。我看了一些关于在屏幕上移动图像的教程,但是我很难在几个图像上移动。

What is the best way to enable three images on the screen to have separate touch controls (Drag Around).

使屏幕上的三个图像具有单独的触摸控件(拖动)的最佳方法是什么。

回答by Alex Nichol

Simply create a UIImageView subclass, for this example I'll call it DragView, and use this class for each of the image views:

只需创建一个 UIImageView 子类,在本例中我将其称为 DragView,并为每个图像视图使用此类:

DragView.h

拖动视图.h

@interface DragView : UIImageView {
}
@end

DragView.m

拖动视图.m

- (void)touchesMoved:(NSSet *)set withEvent:(UIEvent *)event {
    CGPoint p = [[set anyObject] locationInView:self.superview];
    self.center = p;
}

Now, the user will be able to drag any individual instance of DragView around in its superview. If you are creating your image views manually (e.g. imageView = [[UIImageView alloc] initWithImage:anImage]), replace UIImageViewwith DragView. You will also need to import DragView.h in your view controller's header.

现在,用户将能够在其超级视图中拖动任何单独的 DragView 实例。如果您手动创建图像视图(例如 imageView = [[UIImageView alloc] initWithImage:anImage]),请替换UIImageViewDragView. 您还需要在视图控制器的标题中导入 DragView.h。

If you are initializing your image views through interface builder, you can use the attributes inspector to change the class of the image view from UIImageViewto DragView.

如果您通过界面构建​​器初始化图像视图,则可以使用属性检查器将图像视图的类从UIImageView更改为DragView