how to add on touch and detect UIImageView on view so i could move it around and not adding one on top of it?

我怕爱的太早我们不能终老 提交于 2019-12-04 18:47:28

This should let you going…

Create two ivars in your header file:

UIImageView *myImageView1;
UIImageView *myImageView2;

The next two methods will be in your implementation file:

-(void)initImagesAndPanGesture
{
    UIImage *img = [UIImage imageNamed:@"image1.png"];
    myImageView1 = [[UIImageView alloc]initWithImage:img];
    [myImageView1 setFrame:CGRectMake(300, 800, 100, 100)];
    [myImageView1 setUserInteractionEnabled:YES];
    UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan1:)];
    [myImageView1 addGestureRecognizer:recognizer1];
    [self.view addSubview:myImageView1];

    img = [UIImage imageNamed:@"image2.png"];
    myImageView2 = [[UIImageView alloc]initWithImage:img];
    [myImageView2 setFrame:CGRectMake(500, 800, 100, 100)];
    [myImageView2 setUserInteractionEnabled:YES];
    recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan1:)];
    [myImageView2 addGestureRecognizer:recognizer1];
    [self.view addSubview:myImageView2];
}

-(void)handlePan1:(UIPanGestureRecognizer *)recognizer
{
    if (!(CGRectIntersectsRect(myImageView1.frame, myImageView2.frame)))
    {
        CGPoint translation = [recognizer translationInView:self.view];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                         recognizer.view.center.y + translation.y);

        [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    }
    else 
    {
        NSLog(@"intersect!");
    }
}
skram

You can add a UIPanGestureRecognizer to the UIImageView to handle the dragging, as seen below.

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageView addGestureRecognizer:panRecognizer];

Implement the following method:

    -(void)move:(id)sender {

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
      firstX = [[sender view] center].x;
      firstY = [[sender view] center].y;
    }
  translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
      [[sender view] setCenter:translatedPoint];

    }

In combination to the above, you can use CGRectIntersectsRect to find if it's intersecting another cup.

BOOL isIntersecting = CGRectIntersectsRect ([cup1 frame],[cup2 frame]);

You would have to wrap the functionality to take into account all your cups but this gives you an idea a to how to accomplish what you're trying to do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!