idumban
idumban

Reputation: 69

How to pan (move) an image in cocos2d for ipad?

Hi. I am beginner in cocos2d. I want to pan an image (not rotate) like

rotating globe .

Please any one suggest me.

Upvotes: 0

Views: 345

Answers (2)

Deepjyoti Roy
Deepjyoti Roy

Reputation: 482

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; // calling a method **move**
        [panRecognizer setMinimumNumberOfTouches:1]; // set yourself
        [panRecognizer setMaximumNumberOfTouches:2]; // 
        [panRecognizer setDelegate:self];
        [yourImage addGestureRecognizer:panRecognizer];


/*
    CGFloat firstX;
    CGFloat firstY;
*/

   //---method move---//
 -(IBAction)move:(id)sender
        {
            yourImage.clipsToBounds = YES;
            [yourImage bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
            CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

            if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
            {

                firstX = [[sender view] center].x;
                firstY = [[sender view] center].y;
            }

            translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

            [[sender view] setCenter:translatedPoint];

            if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
            {

                CGFloat finalX = translatedPoint.x + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
                CGFloat finalY = translatedPoint.y + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);

                if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
                {

                    if(finalX < 0)
                    {

                        finalX = 0;
                    }

                    else if(finalX > 260)
                    {

                        finalX = 260;
                    }

                    if(finalY < 0)
                    {

                        finalY = 0;
                    }

                    else if(finalY > 416)
                    {

                        finalY = 416;
                    }
                }

                else
                {

                    if(finalX < 0)
                    {

                        finalX = 0;
                    }

                    else if(finalX > 416)
                    {

                        finalX = 260;
                    }

                    if(finalY < 0)
                    {

                        finalY = 0;
                    }

                    else if(finalY > 260)
                    {

                        finalY = 416;
                    }
                }

                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationDuration:.5];

                [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                [[sender view] setCenter:CGPointMake(finalX, finalY)];
                [UIView commitAnimations];
            }
        }

Upvotes: 1

Mathew Varghese
Mathew Varghese

Reputation: 4527

I suggest UIPanGestureRecognizer. You can find such a beautiful tutorial on how to do this at RayWenderlich's Article here. Hope this helps.

Upvotes: 0

Related Questions