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-21 23:11:31

问题


i wonder if you guys have a sample code adding and move UIImageView on touch and also detect if there is an UIImageView there so i can't add UIImageView on top of it.

EDIT: clearer question 1. i wanna add an cups(UIImageView) when i touch the view but do not wan the cup(UIImageView) to stack.

  1. i wanna move the UIImageView but will bounce back to the original position if there is an UIImageView there so it will not stack the UIImageView already there.

thanks for reading my question and appreciated your helps

cheers

des


回答1:


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!");
    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/10842831/how-to-add-on-touch-and-detect-uiimageview-on-view-so-i-could-move-it-around-and

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