简体   繁体   English

仅在您第一次触摸时才会移动的可拖动图像

[英]Draggable image that only moves if you first touch it

I am making a game in which you must drag an image across the view. 我正在制作一个游戏,其中您必须在视图上拖动图像。 I am currently using this code to do so: 我目前正在使用此代码执行此操作:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *mytouch = [[event allTouches] anyObject];
    circle.center = [mytouch locationInView:self.view];
    [self collision];
}

The problem with the code above is that if the image is on the right side of the screen and I press down on the left, the image will move to the location I just touched on the left. 上面代码的问题是,如果图像位于屏幕的右侧,而我按下左侧,则图像将移动到我刚刚触摸左侧的位置。 I want it to be draggable, but only move if you first press down on the image, then drag it around. 我希望它是可拖动的,但是只有在您首先按下图像然后在周围拖动时才移动。 So you would first have to press down on the image on the right side of the screen and (keeping my finger on the screen) drag to the left (or wherever I drag my finger). 因此,您首先必须按下屏幕右侧的图像,然后(将手指保持在屏幕上)向左拖动(或在我拖动手指的任何位置)。

The most concise way to do this I've found is to subclass UIImageView , then all you have to do is this: 我发现最简单的方法是将UIImageView子类化,然后您要做的就是:

#import "MyImageView.h"

@implementation MyImageView

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

@end

An example ViewController for this is as follows: 一个示例ViewController如下所示:

#import "ViewController.h"
#import "MyImageView.h"
#import <QuartzCore/QuartzCore.h>
#define NUM_IMAGES 50

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    const float radius = 15.0f;
    const float diameter = radius * 2;
    for (int i = 0; i < NUM_IMAGES; ++i)
    {
        float x = radius + drand48() * (self.view.frame.size.width - diameter - radius);
        float y = radius + drand48() * (self.view.frame.size.height - diameter - radius);
        MyImageView *imageView = [[MyImageView alloc] initWithFrame:CGRectMake(x, y, diameter, diameter)];
        imageView.backgroundColor = [UIColor colorWithRed:drand48()
                                                    green:drand48()
                                                     blue:drand48()
                                                    alpha:1.0f];
        imageView.userInteractionEnabled = YES;
        imageView.layer.cornerRadius = radius;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, diameter, diameter)];
        label.text = [NSString stringWithFormat:@"%i", i + 1];
        label.textAlignment = NSTextAlignmentCenter;
        [imageView addSubview:label];
        [self.view addSubview:imageView];
    }
}

@end

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

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