我已經寫了一些類似於我的程序一點,但我的情況,我認爲是一個比較複雜的,因爲我在3D旋轉:https://itunes.apple.com/ua/app/bit/id366236469?mt=8
基本上我做的是我設置了一個NSTimer調用定期一些方法。我只是想方向和速度來創建一個旋轉矩陣(正如我所說,3D有點難看:P),並且將速度乘以小於1的某個數字,從而使其下降。乘法而不是減法的原因是,如果來自用戶的旋轉是兩倍的難度,您不希望對象旋轉兩次,因爲這變得煩人,等待我找到。
至於確定車輪正在旋轉的方向,只需將它存儲在touchesEnded:withEvent:方法中,您可獲取所有信息。既然你說你已經有了跟蹤工作,只要用戶把手指放下,這應該是顯而易見的。
我有3D是一樣的東西:
// MyView.h
@interface MyView : UIView {
NSTimer *animationTimer;
}
- (void) startAnimation;
@end
// MyAppDelegate.h
@implementation MyAppDelegate
- (void) applicationDidFinishLaunching:(UIApplication *)application {
[myView startAnimation];
}
@end
// MyView.m
GLfloat rotationMomentum = 0;
GLfloat rotationDeltaX = 0.0f;
GLfloat rotationDeltaY = 0.0f;
@implementation MyView
- (void)startAnimation {
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0/60.0) * animationFrameInterval) target:self selector:@selector(drawView:) userInfo:nil repeats:TRUE];
}
- (void) drawView:(id)sender {
addRotationByDegree(rotationMomentum);
rotationMomentum /= 1.05;
if (rotationMomentum < 0.1)
rotationMomentum = 0.1; // never stop rotating completely
[renderer render];
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *aTouch = [touches anyObject];
CGPoint loc = [aTouch locationInView:self];
CGPoint prevloc = [aTouch previousLocationInView:self];
rotationDeltaX = loc.x - prevloc.x;
rotationDeltaY = loc.y - prevloc.y;
GLfloat distance = sqrt(rotationDeltaX*rotationDeltaX+rotationDeltaY*rotationDeltaY)/4;
rotationMomentum = distance;
addRotationByDegree(distance);
self->moved = TRUE;
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
}
我已經離開了addRotationByDegree功能,但它的作用是,它使用全局變量rotationDeltaX和rotationDeltaY和旋轉矩陣適用於已存儲矩陣,然後保存結果。在您的例子,你可能想要的東西要簡單得多,就像(我現在假設,只有在X方向運動旋轉的車輪):
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *aTouch = [touches anyObject];
CGPoint loc = [aTouch locationInView:self];
CGPoint prevloc = [aTouch previousLocationInView:self];
GLfloat distance = loc.x - prevloc.x;
rotationMomentum = distance;
addRotationByDegree(distance);
self->moved = TRUE;
}
void addRotationByDegree(distance) {
angleOfWheel += distance; // probably need to divide the number with something reasonable here to have the spin be nicer
}
這是一個有趣的小應用程序!我一定會諮詢它nect時間我決定我是否想要一個浴缸或淋浴,或者其他東西:) 這確實看起來像我所需要的(儘管在3D中)。對不起,我很痛苦 - 你能給我一些更具體的細節嗎? 我不太確定旋轉矩陣是什麼,我不是任何方式的代數專家。 – Luke 2013-02-15 16:52:18
我很努力去理解我首先應用於對象的旋轉方式。例如,我不想讓它旋轉特定數量的弧度/度數,我希望它可以無限旋轉,然後我可以按照您的建議進行操作,並嘗試在每次後續旋轉時減慢它的速度。我如何開始這個過程,初始旋轉?我所擁有的是表示速度的單個浮點值。 – Luke 2013-02-15 16:53:10
「我想讓它無限旋轉」你呢? – Vinzzz 2013-02-18 09:26:26