我有一个用于拖放对象的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在别处声明,并且正确设置为YES / NO。
我不想同时移动和旋转物体,每次只能做一件事。 当我旋转UIView时,我想保持一个新的方向,我必须能够以新的方向移动对象。
ADDED 这是我的xcode项目示例。 https://www.dropbox.com/s/lza1n9kjnxa4zun/xcode.zip
答案 0 :(得分:1)
我对此进行了测试,它就像魅力一样:
- (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];
}
答案 1 :(得分:1)
已编辑现在,当我们了解更多信息时......
如果您希望将某种切换器与moveObject
进行两种不同的操作,则需要将所有代码移动到其他块中:
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,以便在用手指平移时实现慢速旋转=)