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 子句。所以图像开始从那个垂直位置旋转,而不是从最后旋转的位置开始。

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

谢谢!

4

2 回答 2

0

我找到了解决这个问题的方法:

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 是从北到这个地方的度数方向!和方向是从罗盘或真航向度数的当前方向

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;

我希望我有所帮助:)

于 2011-12-07T12:41:06.483 回答
0

尝试使用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"];
于 2011-11-30T19:44:41.177 回答