PDA

View Full Version : UIPanGestureRecognizer problem in Ipad




ashwinr87
Apr 26, 2011, 04:35 PM
Hi,

in my app, I am trying to move a subview which I popup. This is am doing using UIPanGestureRecognizer and gesture recognizing functions provided by apple here (http://developer.apple.com/library/ios/#samplecode/Touches/Introduction/Intro.html)

So the problem I get is that when I click on the button image and try to move the view, it does not move the view. The functionality works only when I click the button, and then click it and move it. only then does it move the view.

I would like to know what I am doing wrong.

Here is the button code to which I add this functionality

UIButton *moveButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 1, 30, 30)];
[moveButton addTarget:self action:@selector(moveButtonClick:)forControlEvents:UIControlEventTouchDown];
[moveButton setBackgroundImage:[UIImage imageNamed: @"moveButton.png"] forState:UIControlStateNormal];
[customView addSubview:moveButton];
[moveButton release];

and here is the code I use for the app to recognize the pan gesture

-(void) moveButtonClick: (id) sender
{
[self addGestureRecognizersToPiece:self.view];
}

// shift the piece's center by the pan amount
// reset the gesture recognizer's translation to {0, 0} after applying so the next callback is a delta from the current position
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];

[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];

[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}

// adds a set of gesture recognizers to one of our piece subviews
- (void)addGestureRecognizersToPiece:(UIView *)piece
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
[panGesture setDelegate:self];
[panGesture setMaximumNumberOfTouches:1];
[piece addGestureRecognizer:panGesture];
[panGesture release];

}


// scale and rotation transforms are applied relative to the layer's anchor point
// this method moves a gesture recognizer's view's anchor point between the user's fingers
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}

// UIMenuController requires that we can become first responder or it won't display
- (BOOL)canBecomeFirstResponder
{
return YES;

It would be really great if someone could help me out in this.



ashwinr87
Apr 28, 2011, 10:23 AM
I solved the problem... apparently using the button to perform the action I needed, will indeed require me to click on it twice and then only perform the pan action. So I removed it and added an Image and added gesture recognizer to it.

Here is the updated code...

The code for the Image -

// Image which allows subview to be moved around
CGRect myImageRect = CGRectMake(5, 1, 30, 30);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"moveButton.png"]];
myImage.opaque = YES;
myImage.userInteractionEnabled = YES;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
[panGesture setDelegate:self];
[panGesture setMaximumNumberOfTouches:1];
UIView *piece = self.view;
[piece addGestureRecognizer:panGesture];
[panGesture release];
[customView addSubview:myImage];
[myImage release];

After this I removed the button code and removed the action recognizer for the button.. other than that, the rest of the code is the same and it works fine...