0

I'm developing an iOS app based on augmented reality. What it suppose to do is user will set his distance from wall through a UISlider. Then he'll select a picture from gallery and see how will it look on the wall. App should scale the UIImage accordingly to the distance of user from wall and user can drag it to see how it look on the wall.

I want to apply UILongPressGestureRecognizer on the added UIImage so that it can be deleted i.e. tap hold and delete.
This is the code I've applied to import the image which is already in my library I'll do the import from gallery later:

self.myImage = [UIImage imageNamed:@"myimage.png"];
self.myImageView = [[UIImageView alloc] initWithImage:self.myImage];
self.myImageView.userInteractionEnabled = YES;
CGRect cellRectangle;
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
cellRectangle = CGRectMake(0, 0, self.myImage.size.width/5, self.myImage.size.height/5);

And for UILongPressGestureRecognizer this is the code:

  self.lpgr = [[UILongPressGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(handleLongPressGestures:)];
self.lpgr.minimumPressDuration = 2.0; //seconds
self.lpgr.accessibilityFrame = cellRectangle;

[self.customCam addGestureRecognizer:self.lpgr];

Where customCam is the view on which the camera is shown for AR.

 - (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender
{
    if ([sender isEqual:self.lpgr]) {
        if (sender.state == UIGestureRecognizerStateBegan)
        {
            CGPoint p = [self.lpgr locationInView:self.myImageView];
            NSLog(@"TapLong Run on points %@",NSStringFromCGPoint(p));
        }
    }
}

The problem with this code is it applies the UILongPressGestureRecognizer on all of the customCam view.

How to bound it that it'll remain within the myImageView. I've also tried doing this:

[self.myImageView addGestureRecognizer:self.lpgr];

but this didn't work and I've also added this self.myImageView.userInteractionEnabled = YES;

  • [self.customCam addGestureRecognizer:self.lpgr]; you are added here,keep this one self.myImageView – Balu Nov 27 '14 at 09:09
  • @Balu I've tried this one but it didn't do anything then. It never got to the function 'handleLongPressGestures' if condition! But on customCam it does. –  Nov 27 '14 at 09:15
  • What I'm thinking of is to set another if condition that if the gesture coordinates are within the UIImageView coordinates then it'll perform the the action. So can you help me with that? –  Nov 27 '14 at 09:17

1 Answers1

0

where you have added [self.customCam addGestureRecognizer:self.myImageView]; first add image view in custom view then add self.lpgr in myImageview. replace this line [self.customCam addGestureRecognizer:self.lpgr]; by this

[myImageView addGestureRecognizer:self.lpgr]; and try

Sport
  • 8,570
  • 6
  • 46
  • 65
  • Tried it but it didn't work. There is no error but the code didn't run the functionality of 'handleLongPressGestures' method. –  Nov 27 '14 at 09:20
  • handleLongPressGestures is called ? – Sport Nov 27 '14 at 09:23
  • By adding this line ' [self.customCam addGestureRecognizer:self.myImageView];' and replacing the other line as you said. I got this warning 'unrecognized selector sent to instance' and it didn't show the image. Seems like customCam layer is above the myImageView Layer. –  Nov 27 '14 at 09:36
  • http://stackoverflow.com/questions/15870756/using-uilongpressgesturerecognizer-for-subviews-of-uiscrollview look at this – Sport Nov 27 '14 at 09:43