Ios 平移手势在默认旋转和缩放后表现怪异
我目前正在设计一个小游戏作为一个学习项目,基本上如下所示,一个图像在Ios 平移手势在默认旋转和缩放后表现怪异,ios,rotation,scale,pan,Ios,Rotation,Scale,Pan,我目前正在设计一个小游戏作为一个学习项目,基本上如下所示,一个图像在viewDidLoad上旋转和缩放,另一个图像是原始图像的直接副本 所以基本上有一个图像与另一个图像有点不同,目标是缩小它,旋转它,然后在另一个图像上移动它,5像素,5度旋转,5%缩放 我遇到了一个问题。我使用以下代码“扭曲”图像 CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2.5); image.transform = CGAffineTra
viewDidLoad
上旋转和缩放,另一个图像是原始图像的直接副本
所以基本上有一个图像与另一个图像有点不同,目标是缩小它,旋转它,然后在另一个图像上移动它,5像素,5度旋转,5%缩放
我遇到了一个问题。我使用以下代码“扭曲”图像
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2.5);
image.transform = CGAffineTransformScale(transform, 1.25, 1.25);
旋转图像并将其缩放125%后,平移手势无法正确执行
有人知道这里会发生什么吗?我的意思是它不能用我的手指移动。。它似乎在滑行或向相反的方向移动
我的平移手势方法如下
if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [gesture translationInView:image];
//if within game field
if((image.center.x + translation.x) > 50.0 && (image.center.x + translation.x) < 255.0 && (image.center.y + translation.y) > 50.0 && (image.center.y + translation.y) < 302) {
[image setCenter:CGPointMake([image center].x + translation.x, [image center].y + translation.y)]; //move it
}
}
[gesture setTranslation:CGPointZero inView:[image superview]];
if(gesture.state == UIGestureRecognizerStateEnded) [self didWin]; // not relevant to question
if(signature.state==UIGestureRecognitizerStateStarted | | signature.state==UIGestureRecognitizerStateChanged){
CGPoint translation=[手势翻译视图:图像];
//如果在游戏场内
如果((image.center.x+translation.x)>50.0&&(image.center.x+translation.x)<255.0&&(image.center.y+translation.y)>50.0&&(image.center.y+translation.y)<302){
[image setCenter:CGPointMake([image center].x+translation.x[image center].y+translation.y)];//移动它
}
}
[手势设置翻译:CGPointZero-inView:[图像超级视图]];
如果(signature.state==UIGestureRecognitizerStateEnded)[self didWin];//与问题无关
有人知道为什么在我旋转和缩放我的图像后,平移操作不正确吗?当我注释掉前两行代码时,pan将正确执行并用用户的手指移动
提前感谢您的任何建议或帮助 解决方案是稍微更改pan代码
if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [gesture translationInView:self.view]; //CHANGED
//if within game field
if((image.center.x + translation.x) > 50.0 && (image.center.x + translation.x) < 255.0 && (image.center.y + translation.y) > 50.0 && (image.center.y + translation.y) < 302) {
[image setCenter:CGPointMake([image center].x + translation.x, [image center].y + translation.y)]; //move it
}
}
[gesture setTranslation:CGPointZero inView:self.view];
if(signature.state==UIGestureRecognitizerStateStarted | | signature.state==UIGestureRecognitizerStateChanged){
CGPoint translation=[手势翻译视图:self.view];//已更改
//如果在游戏场内
如果((image.center.x+translation.x)>50.0&&(image.center.x+translation.x)<255.0&&(image.center.y+translation.y)>50.0&&(image.center.y+translation.y)<302){
[image setCenter:CGPointMake([image center].x+translation.x[image center].y+translation.y)];//移动它
}
}
[手势集翻译:CGPointZero-inView:self.view];
我在视图中更改了
:self.view
和translationview:self.view]代码>旋转可能已更改帧。我使用了cosf和sinf函数来处理它
handlePan和handleRotate是回调函数,我可以在其中控制self.view的子视图。在这里,您应该用自己的视图替换图像
static CGFloat _rotation = 0;
- (void)handlePan:(UIPanGestureRecognizer*)recognizer
{
UIImageView *image = nil;
for (UIImageView *tmp in recognizer.view.subviews) { // pick the subview
if (tmp.tag == AXIS_TAG) {
image = tmp;
}
}
CGPoint translation = [recognizer translationInView:image];
// I have found any related documents yet, but these equations do work!//////
CGFloat dx = translation.x * cosf(_rotation) - translation.y*sinf(_rotation);
CGFloat dy = translation.x * sinf(_rotation) + translation.y*cosf(_rotation);
/////////////////////////////////////////////////////////////////////////////
image.center = CGPointMake(image.center.x+dx, dy+image.center.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:image];
}
- (void)handleRotate:(UIRotationGestureRecognizer*)recognizer
{
UIImageView *image = nil;
for (UIImageView *tmp in recognizer.view.subviews) { // pick the subview
if (tmp.tag == AXIS_TAG) {
image = tmp;
}
}
CGFloat r = [recognizer rotation];
if ((recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
&& recognizer.numberOfTouches == 2) {
image.transform = CGAffineTransformMakeRotation(_rotation+r);
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
_rotation+=r; // Record the final rotation
}
}