Olex
Olex

Reputation: 1656

Dragging UIImages to the selected area

Newbie obj-c question.

My task is to make visually represented test for iPad. In view I have three UIImages (with images of answers to test question) what will dragged to area for answer. If selected right image, then it needs to stay in this area, if not - it goes back into the start position. If user stop dragging not in area for answer it also needs to goes back to the start position. I tried to implement this: http://www.cocoacontrols.com/platforms/ios/controls/tkdragview but it's too hard for me because I am a super newbie.

I implemented simple dragging of images by PanRecognizer, so every from three images dragging

 -(IBAction)controlPan:(UIPanGestureRecognizer *)recognizer {
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];
}

If I think in right way, I need to set CGPoint's with start and destination coordinates and then to customize Pan Gestures method? If it's not right, in what way can I do this?

Upvotes: 0

Views: 323

Answers (1)

sunkehappy
sunkehappy

Reputation: 9101

You can consider this three method, maybe these are easier for you:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:

Detailed demo for – touchesBegan:withEvent: is:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    UITouch *touched = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touched.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    UITouch *touched = [[event allTouches] anyObject];
    // Here I suppose self.imageView contains the image user is dragging.
    CGPoint location = [touch locationInView:touched.view];
    // Here the check condition is the image user dragging is absolutely in the answer area.
    if (CGRectContainsRect(self.answerArea.frame, self.imageView.frame)) {
        // set transition to another UIViewController
    } else {
        self.imageView.center = location;    // Here I suppose you just move the image with user's finger.
    }
}

Here is the Apple's doc for this UIResponder Class Reference

Upvotes: 2

Related Questions