如何在cocos2d中为ipad平移(移动)图像?

时间:2012-10-19 07:28:55

标签: iphone cocos2d-iphone

您好。我是cocos2d的初学者。我想像

那样平移图像(不是旋转)

rotating globe

请任何人建议我。

2 个答案:

答案 0 :(得分:1)

    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];
            }
        }

答案 1 :(得分:0)

我建议UIPanGestureRecognizer。你可以在RayWenderlich's Article here找到关于如何做到这一点的精彩教程。希望这会有所帮助。