触摸并拖动屏幕周围的图像 [英] Touch and drag an image around the screen

查看:106
本文介绍了触摸并拖动屏幕周围的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

推荐答案

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

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

DragView.h

@interface DragView : UIImageView {
}
@end

DragView.m

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

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

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 UIImageView with DragView. You will also need to import DragView.h in your view controller's header.

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

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 UIImageView to DragView.

这篇关于触摸并拖动屏幕周围的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆