简体   繁体   English

触摸并在屏幕上拖动图像

[英]Touch and drag an image around the screen

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). 使屏幕上的三个图像具有单独的触摸控件(拖动)的最佳方法是什么。

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 DragView.h

@interface DragView : UIImageView {
}
@end

DragView.m DragView.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. 现在,用户将能够在其超级视图中拖动DragView的任何单个实例。 If you are creating your image views manually (eg imageView = [[UIImageView alloc] initWithImage:anImage]), replace UIImageView with DragView . 如果要手动创建图像视图(例如,imageView = [[UIImageView alloc] initWithImage:anImage]), DragView UIImageView替换为DragView You will also need to import DragView.h in your view controller's header. 您还需要在视图控制器的标题中导入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 UIImageView to DragView . 如果要通过界面构建​​器初始化图像视图,则可以使用属性检查器将图像视图的类从UIImageView更改为DragView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM