我想围绕其中心点旋转图像。我面临的问题是我需要获得角度来计算触摸移动事件(我不想使用多点触控)。我目前使用下面的代码< / p>
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *allTouches = [touches allObjects];
gestureStartPoint = gestureMovedPoint;//i am getting the gestureStartPoint on touch began event
gestureMovedPoint = [[allTouches objectAtIndex:0] locationInView:[self superview]];
NSLog(@"gestureMovedPoint = %@",NSStringFromCGPoint(gestureMovedPoint));
}
CGFloat previousAngle = [self angleBetweenPoints:gestureStartPoint second11:gestureMovedPoint]; // atan2(gestureMovedPoint.y - gestureStartPoint.y, gestureMovedPoint.x - gestureStartPoint.x) * 180 / M_PI;
CGFloat currentAngle =atan2(self.transform.b, self.transform.a);//atan2(gestureMovedPoint.y - gestureStartPoint.y,gestureMovedPoint.x - gestureStartPoint.x) * 180 / M_PI;
CGFloat angleToRotate = currentAngle - previousAngle;
float xpoint = (((atan2((gestureMovedPoint.x - gestureStartPoint.x) , (gestureMovedPoint.y - gestureStartPoint.y)))*180)/M_PI);
CGAffineTransform transform = CGAffineTransformMakeRotation(angleToRotate-100);
self.transform = transform;
请帮助我找到解决方案,因为我被困在这里,需要尽快完成此应用程序,因为有一个死线。 提前致谢
答案 0 :(得分:2)
很高兴我记得三角法
-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{
float dX = touchPoint.x-objPos.x; // distance along X
float dY = touchPoint.y-objPos.y; // distance along Y
float radians = atan2(dY, dX); // tan = opp / adj
//Now we have to convert radians to degrees:
float degrees = radians*M_PI/360;
return degrees;
}
一旦你有了不错的方法,只需在触摸事件方法中执行此操作即可。 (我忘记了它的名字......)
CGAffineTransform current = view.transform;
[view setTransform:CGAffineTransformRotate(current,
[self degreesTorotateObjectWithPosition:view.frame.origin
andTouchPoint:[touch locationInView:parentView]]
//Note: parentView = The view that your object to rotate is sitting in.
这几乎是你需要的所有代码。数学是正确的,但我不确定setTransform的东西。我在学校用浏览器写这个。你应该能够从这里弄明白。
祝你好运,Aurum Aquila
答案 1 :(得分:0)
必须考虑到这一点。但我更喜欢用两个触摸来旋转视图。它会简单得多。
答案 2 :(得分:0)
我在如何获得触摸驱动旋转方面确实有点挣扎,更是如此,因为我希望100%理解我正在使用的代码。经过多次尝试失败后,我最终得到了这个:
- (CGFloat) pointToAngleFromCenter: (CGPoint) point {
// transform point to a self.center'ed origin based coordinate system
point.x = point.x - self.center.x ;
// ditto for y, but compensate for y going downwards to y going upwards
point.y = self.center.y - point.y ;
return ::atan2(point.y, point.x) ;
}
如果有人对这种方法有更好的名称,我会全力以赴。
它的作用是在父视图坐标中取一个点,相对于视图中心(在父视图坐标中)重新映射它,并计算此重新映射点与轴[0X]之间的角度。为此,它将y标准化为正常的数学坐标(y值在其值增加而不是向下时上升),因此self.center.y - point.y
而不是相反。
最后,在touchesMoved:
- (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event {
UITouch * touch = [touches anyObject] ;
CGFloat currA = [self pointToAngleFromCenter:[touch locationInView:self.superview]] ;
CGFloat prevA = [self pointToAngleFromCenter:[touch previousLocationInView:self.superview]] ;
// the current value of the rotation angle
CGFloat tranA = ::atan2(self.transform.b, self.transform.a) ;
// the angle difference between last touch and the current touch
CGFloat diffA = currA - prevA ;
// the new angle resulting from applying the difference
CGFloat angle = tranA - diffA ;
CGAffineTransform t = ::CGAffineTransformMakeRotation(angle) ;
self.transform = t ;
[self setNeedsDisplay] ;
}