Objective C UIImageView 旋转动画效果不佳

发布于 2024-12-19 00:16:59 字数 759 浏览 2 评论 0原文

我正在表格单元格中创建指南针,因此图像视图的旋转出现问题,它从资源中图像的位置重新开始。

这是我的代码的一部分:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];

if(cellValue.direction == 0){
   cell.compass.transform = CGAffineTransformRotate(cell.compass.transform,DegreesToRadians([cellValue.fixedDirection floatValue]));
} else {
   cell.compass.transform = CGAffineTransformRotate(cell.compass.transform,DegreesToRadians([cellValue.direction floatValue]));   
}
[UIView commitAnimations];

cell.compass 是 UIimageView ,其中有垂直箭头的图像。如果方向为 0,那么它应该达到该度数,在从罗盘获得角度后,它会进入 else 子句。因此图像从该垂直位置开始旋转,而不是从最后旋转的位置开始旋转。

如何获得最后的旋转位置并从那里继续

谢谢!

I am creating compass in a table cell so I have problem with the rotation of the image view it start all over again from the position which is the image in the resources.

this is part of my code:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];

if(cellValue.direction == 0){
   cell.compass.transform = CGAffineTransformRotate(cell.compass.transform,DegreesToRadians([cellValue.fixedDirection floatValue]));
} else {
   cell.compass.transform = CGAffineTransformRotate(cell.compass.transform,DegreesToRadians([cellValue.direction floatValue]));   
}
[UIView commitAnimations];

cell.compass is UIimageView and I have image in it that is arrow vertical. if the direction is 0 then it should get to that degree and after it get angle from compass it goes to the else clause. So the image starst to rotate from that vertical position not from the last rotated position.

how to get the last rotated position and continue from there

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪装你 2024-12-26 00:17:00

尝试使用 removedOnCompletion = NO 旋转视图的图层,而不是旋转 UIView

CALayer *layer = [cell.compass layer];
NSNumber *initRotation = [layer valueForKeyPath:@"transform.rotation"];
//CATransform3D rotationTransform = CATransform3DRotate(layer.transform, angle, 0.0, 0.0, 1.0);
//layer.transform = rotationTransform;

CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration = 0.5;
animation.fromValue = initRotation ;
animation.toValue = [NSNumber numberWithFloat:([initRotation floatValue] + angle)];
animation.removedOnCompletion = NO;
[layer addAnimation:animation forKey:@"transform.rotation"];

Try to rotate view's layer with removedOnCompletion = NO instead of rotate UIView

CALayer *layer = [cell.compass layer];
NSNumber *initRotation = [layer valueForKeyPath:@"transform.rotation"];
//CATransform3D rotationTransform = CATransform3DRotate(layer.transform, angle, 0.0, 0.0, 1.0);
//layer.transform = rotationTransform;

CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration = 0.5;
animation.fromValue = initRotation ;
animation.toValue = [NSNumber numberWithFloat:([initRotation floatValue] + angle)];
animation.removedOnCompletion = NO;
[layer addAnimation:animation forKey:@"transform.rotation"];
べ映画 2024-12-26 00:17:00

我找到了这样的问题解决方案:

NSLog(@"old dir %d: new dir: %d", [cellValue.oldDirection intValue], [cellValue.direction intValue]);
CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration = 0.5;

int oldDir = [cellValue.oldDirection intValue];
int newDir = [cellValue.direction intValue];


/// in didUpdateHeading
/// int result = 0;

/// result = fixedDir - orientation; 

/// place.oldDirection = place.direction;
/// place.direction = [NSNumber numberWithInt:result];

固定方向是从北到该地方的方向!方向是指南针或 trueHeading 的当前方向,

if (oldDir*newDir < 0 && abs(abs(oldDir) - abs(newDir)) < 180) {
    oldDir = (360 + oldDir) % 360;
    newDir = (360 + newDir) % 360;
}

animation.fromValue = [NSNumber numberWithFloat:DegreesToRadians(oldDir)];
animation.toValue = [NSNumber numberWithFloat:DegreesToRadians(newDir)];


animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[cell.compass.layer addAnimation:animation forKey:@"transform.rotation"];
float differenceAngle = [animation.toValue floatValue] - [animation.fromValue floatValue];
CATransform3D rotationTransform = CATransform3DMakeAffineTransform(CGAffineTransformRotate(cell.compass.transform,differenceAngle));

cell.compass.layer.transform = rotationTransform;

我希望我有所帮助:)

I found a solution to the problem like this:

NSLog(@"old dir %d: new dir: %d", [cellValue.oldDirection intValue], [cellValue.direction intValue]);
CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration = 0.5;

int oldDir = [cellValue.oldDirection intValue];
int newDir = [cellValue.direction intValue];


/// in didUpdateHeading
/// int result = 0;

/// result = fixedDir - orientation; 

/// place.oldDirection = place.direction;
/// place.direction = [NSNumber numberWithInt:result];

fixed dir is the direction in degree from north to the place! and orientation is the current orientation in degree from the compass or trueHeading

if (oldDir*newDir < 0 && abs(abs(oldDir) - abs(newDir)) < 180) {
    oldDir = (360 + oldDir) % 360;
    newDir = (360 + newDir) % 360;
}

animation.fromValue = [NSNumber numberWithFloat:DegreesToRadians(oldDir)];
animation.toValue = [NSNumber numberWithFloat:DegreesToRadians(newDir)];


animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[cell.compass.layer addAnimation:animation forKey:@"transform.rotation"];
float differenceAngle = [animation.toValue floatValue] - [animation.fromValue floatValue];
CATransform3D rotationTransform = CATransform3DMakeAffineTransform(CGAffineTransformRotate(cell.compass.transform,differenceAngle));

cell.compass.layer.transform = rotationTransform;

I hope I helped :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文