1

I've been busy trying to develop a game and I've at one of the parts that I find difficult. I may be entirely on the wrong foot swell of how to do this.

What I have is a couple of imageviews on my board. They are the holders of objects I drag into them (tiles). (again I don't know if this is the best or the right way to do it.)

For example I've made a image to display what I mean:

In this image I have a image (A) that I want to drag onto 1 of the top tiles my code will have to auto detect above what imageview the (A) is most located and place it into that view.

enter image description here

I got really stuck here and I'd like some information on how to this or another way of doing something like this.

The A is already dragable.

Kevin
  • 291
  • 4
  • 15

2 Answers2

2

You can use bool CGRectIntersectsRect (CGRect rectA,CGRect rect1); (rect1 or rect2 or 3 or 4) to check for intersection of the frame of 'A' with any of the four top tiles.

After intersection you can check for the smallest distance between the center of each tile to that of 'A' to check which tile contains the most of A. (All UIView has center property)

This is assuming all the tiles and A are separate views.

Or on second thought you can just check for the smallest distance between the centres keeping a minimum distance before checking, and avoid the first step completely.

CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));
Community
  • 1
  • 1
Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • Thank you for this. this is what sounded most logicaly to me aswell, as I tought about at work today. I dont want to set the imageview to that image of A so will I be able to match the center of the A object with the center of one of those views? Thanks. – Kevin Feb 28 '13 at 02:06
  • Yes. you can assign the center of A as one of center of one of those tiles. – Rakesh Feb 28 '13 at 02:30
1

Make property for your gesture:

@property (nonatomic, strong) UILongPressGestureRecognizer *longPress;

initialize it:

self.longPress = [UILongPressGestureRecognizer initWithTarget:self action:@selector(gestureRecognizedStateDidChanged:)];
self.longPress.minimumPressDuration = 0.03;

Your view controller class should implement <UIGestureRecognizerDelegate> protocol:

self.longPress.delegate = self;

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)sender {
return CGRectContainsPoint(self.targetImage.frame, [sender locationInView:self.view]);
}

- (void)gestureRecognizedStateDidChanged:(UILongPressGestureRecognizer *)sender {
switch (recognizer.state) {
        case UIGestureRecognizerStatePossible:
        case UIGestureRecognizerStateBegan: {
        // did started, you can pick up your view
        self.targerImageView.center = [sender locationInView:self.view];
        break;
    }
    case UIGestureRecognizerStateFailed:
    case UIGestureRecognizerStateCancelled:
    case UIGestureRecognizerStateEnded: {
        // drop callback, bring you view home or to target
        if (CGRectContainsPoint(self.toDropImageViews.frame, [sender locationInView:self.view]) {
            // do you drop code here
        }
        break;
    }
    case UIGestureRecognizerStateChanged: {
        self.targerImageView.center = [sender locationInView:self.view];
        // moving callback
        break;
        }
    }
iiFreeman
  • 5,165
  • 2
  • 29
  • 42
  • thank you for this find it hard to understand but i'll keep this code while im making mine looks promising thanks – Kevin Feb 28 '13 at 02:06