Question

I think the title is pretty self explaining. I need an icon that is dragged by users touch. If this icon is droped on a certain area I would like to call a function. How´s that possible?

Thanks a lot,

Tyler

Was it helpful?

Solution

  • Add UIPanGesture to the View.
- (void)viewDidLoad
{

    [super viewDidLoad];
    UIPanGestureRecognizer *pangesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imageIsMoved:)];
    pangesture.minimumNumberOfTouches = 1;

    [self.myView addGestureRecognizer:pangesture];

}
  • Write a gesture method when drag is over
-(void)imageIsMoved:(UIPanGestureRecognizer *)gesture{

    CGRect frameToBeCompared;
    if (gesture.state == UIGestureRecognizerStateEnded) {

        UIView *v = [gesture view];
        CGRect viewFrame  = v.frame;
        if (CGRectEqualToRect(frameToBeCompared, viewFrame)) {
            [self callMyMethod];
        }
    }
}

OTHER TIPS

Check out this thread. Once you have the basics down, you can use an if statement to check the bounds of your icon and if they hit a specific spot call your next function.

Here's another example from one of my favorite iOS tutorial sites, www.raywenderlich.com

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top