やあ。私は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 の Article hereで、これを行う方法に関する美しいチュートリアルを見つけることができます。お役に立てれば。