尝试拖放对象时出现旋转UIView iOS错误

尝试拖放对象时出现旋转UIView iOS错误,ios,objective-c,uiview,rotation,drag,Ios,Objective C,Uiview,Rotation,Drag,我有一个用于拖放对象的UIView子类,它的工作非常完美,但在我应用这个转换之后 CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2); self.transform = transform; 当转到“拖放”时,对象将开始一个无限螺旋:-/ 接触开始了 if (!moveObject){ CGAffineTransform transform = CGAffineTransformMak

我有一个用于拖放对象的UIView子类,它的工作非常完美,但在我应用这个转换之后

    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
    self.transform = transform;
当转到“拖放”时,对象将开始一个无限螺旋:-/

接触开始了

if (!moveObject){

    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
    self.transform = transform;

}else{

    // When a touch starts, get the current location in the view
    currentPoint = [[touches anyObject] locationInView:self];
}
触摸代码在这里

if (moveObject){

    //Current point is locationInView get in touchBegin
    // Get active location upon move
    CGPoint activePoint = [[touches anyObject] locationInView:self];

    // Determine new point based on where the touch is now located
    CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
                                   self.center.y + (activePoint.y - currentPoint.y));
    // Set new center location
    self.center = newPoint;
}
我如何解决这个问题

p.S.moveObject在别处声明,并正确设置为是/否


我不想同时移动和旋转对象,每次只移动一件东西。 当我旋转UIView时,我想保持一个新的方向,并且我必须能够以新的方向移动对象

已添加 这里是我的xcode项目示例。

我对此进行了测试,效果很好:

- (void)viewDragged:(UIPanGestureRecognizer *)gesture
{

    CGPoint translation = [gesture translationInView:self.view];

    gesture.view.center = CGPointMake(gesture.view.center.x + translation.x, gesture.view.center.y + translation.y);


    [gesture setTranslation:CGPointZero inView:gesture.view];

}

编辑现在当我们了解更多信息时

如果您希望使用某种切换器执行两种不同的操作,如
moveObject
,则需要将所有代码移动到else块中:

if (!moveObject){
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
    self.transform = transform;
} 
else {
    // When a touch starts, get the current location in the view
    currentPoint = [[touches anyObject] locationInView:self];
    //Current point is locationInView get in touchBegin
    // Get active location upon move
    CGPoint activePoint = [[touches anyObject] locationInView:self];

    // Determine new point based on where the touch is now located
    CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
                                   self.center.y + (activePoint.y - currentPoint.y));
    // Set new center location
    self.center = newPoint;
}
或者更好的方法是,如果每次触摸都要将视图旋转90度,则将转换代码更改为:

if (!moveObject){
    self.transform = CGAffineTransformRotate(self.transform, M_PI_2);
}
else {
    ….
} 

您可以设置较小的值,而不是M_PI_2,以实现缓慢旋转,同时用手指平移=)

在何处应用此转换?@Ossir in touchsbegind如果布尔标志为真,则开始进行转换,否则移动对象发布的代码正常。在应用转换之前,您应该向我们显示带有此检查的代码,以及更改此布尔值的代码。错误就在那里,我猜=)@Ossir检查代码,我已经添加了我的触摸开始,您将
moveObject
更改为
YES
?试着在
self.transform=transform之后马上做但现在我使用触摸方法拖放我的对象而不是手势识别器i insert currentPoint=[[Touchs anyObject]locationInView:self];外部但不工作,同时moveObject已正确设置为是或否,我没有忘记。另外,请查看@Unheilig的答案,这肯定是您的下一个问题=)