iphone - 如何测量 iPhone 中滑动的速度,然后计算图像的旋转?

标签 iphone objective-c ios xcode uitouch

我使用了下面的代码,我可以旋转我的图像。但是在动画之后我不知道如何获得图像的最终角度。

我想在“触摸结束”事件后计算图像的旋转角度

- (void)rotateAccordingToAngle:(float)angle
{
    [NumbersImage setTransform:CGAffineTransformRotate(NumbersImage.transform, angle)];  
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    [NumbersImage.layer removeAllAnimations];
    previousTimestamp = event.timestamp;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if ([touch view] == NumbersImage) {

    CGPoint center = CGPointMake(CGRectGetMidX([NumbersImage bounds]), CGRectGetMidY([NumbersImage bounds]));
    CGPoint currentTouchPoint = [touch locationInView:NumbersImage];
    CGPoint previousTouchPoint = [touch previousLocationInView:NumbersImage];
    CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

    [self rotateAccordingToAngle:angleInRadians];

    CGFloat angleInDegree = RadiansToDegrees(angleInRadians);
    FinalDegree +=angleInDegree;        
    revolutions+= (angleInDegree/360.0f);
}

}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

if ([touch view] == NumbersImage) {

    NSTimeInterval timeSincePrevious = event.timestamp - previousTimestamp;
    CGFloat revolutionsPerSecond = revolutions/timeSincePrevious;

    [self startAnimationWithRevolutions:revolutionsPerSecond forTime:5.0f];
    NSLog(@"Revolution per second = %f",revolutionsPerSecond);

    revolutions = 0;
}
}

CGFloat RadiansToDegrees(CGFloat radians)
{
    //NSLog(@"Radians %f",radians);
    return radians * 180 / M_PI;
};


- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NumbersImage.userInteractionEnabled = TRUE;
    if (timerUpdate) {
        [timerUpdate invalidate];
        timerUpdate = nil;
    }
}
-(void)updateTransform{
    NumbersImage.transform = [[NumbersImage.layer presentationLayer] affineTransform];
}
-(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time
{
    NumbersImage.userInteractionEnabled = FALSE;
    float totalRevolutions = revPerSecond * time;

    timerUpdate = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateTransform) userInfo:nil repeats:YES];

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:time]forKey:kCATransactionAnimationDuration];

    CABasicAnimation* spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
CGAffineTransform transform = NumbersImage.transform;

float fromAngle = atan2(transform.b, transform.a);
float toAngle = fromAngle + (totalRevolutions*4*M_PI);

NSLog(@"To Angle = %f",toAngle);
NSLog(@"From Angle = %f",fromAngle);
spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle];
spinAnimation.toValue = [NSNumber numberWithFloat:toAngle];
spinAnimation.repeatCount = 0;
spinAnimation.removedOnCompletion = NO;
spinAnimation.delegate = self;
spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[NumbersImage.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
[CATransaction commit];


//NSLog(@"Total Rotation = %f",totalRevolutions);

//NSLog(@"Degree Rotation %f", temp);

//[self Rotate];
CGFloat temp = RadiansToDegrees(fromAngle);
FinalDegree += temp;
//NSLog(@"FinalDegree %f",FinalDegree);


//NSLog(@"Final degree %f",FinalDegree);
//NSLog(@"Total %f",total);

}

CGFloat DegreesToRadians(CGFloat degrees)
{
    return degrees * M_PI / 180;
}

此代码用于测量速度并根据用户触摸旋转图像。

但我无法获得图像的最终角度。

请有人帮我写这段代码。

谢谢,

河芒。

最佳答案

你做错了方法。看这里:

  1. 声明一个实例 float x; 并在 viewDidLoad 中将其设置为零。
  2. 在 touchesMoved 中将 x 递增 1(或递减,具体取决于检测到的方向)。
  3. 请记住:x 是以度为单位的角度,因此如果递减导致低于零,则执行 x = 360.0;,反之亦然:如果递增导致超过 360.0,则执行 x = 0.0;
  4. 还有在touchesMoved里说:

myImageView.transform = CGAffineTransformMakeRotation(x * M_PI/180);

顺便说一下,您将永远知道角度 - 它是 x。

关于iphone - 如何测量 iPhone 中滑动的速度,然后计算图像的旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11573524/

相关文章:

ios - 为什么 UIViewController.UIView 对子 UIViewLabel 的约束抛出错误 "Invalid pairing of layout attributes"?

ios - iAds 会导致自动布局出现问题吗?

iphone - 列表项导航在 iPhone/iPad 上的 iOS 中的 Safari/Chrome 上不起作用

iphone - 按下主页按钮时 MPMoviez PlayerViewController 消失

ios - 表格 View 中的嵌套 Collection View 更好或多个 Collection View

iOS 键盘动画冲突

ios - Swift:谷歌登录后转到其他 View Controller

iOS - 内存警告卸载 View Controller 并使应用程序无响应

iphone - iPhone 的 Objective-C 中的面向方面编程

iphone - 我应该使用什么 OpenGL ES 纹理格式才能在 iPad 上获得更好的动态范围?