你好。我是 cocos2d 的初学者。我想平移图像(不旋转)
.
请任何人建议我。
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];
}
}
我建议UIPanGestureRecognizer
。您可以在此处的 RayWenderlich 的文章中找到有关如何执行此操作的精美教程。希望这可以帮助。