嗨我有一个UIImageView,它是部分关闭,部分关闭屏幕。
我想绕屏幕点(视图中心)旋转视图。
我该怎么做?
fullRotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotationAnimation.fromValue= [NSNumber numberWithFloat:0.0f];
fullRotationAnimation.toValue = [NSNumber numberWithFloat:2 * M_PI];
fullRotationAnimation.duration = 4;
fullRotationAnimation.repeatCount = 1;
[self->greyRings.layer addAnimation:fullRotationAnimation forKey:@"360"];
我现在正在使用该代码,但它只是围绕屏幕中心旋转。
有什么想法吗?
答案 0 :(得分:2)
使用块动画:
- (void)rotateImageView:(UIImageView *)imageView aroundPoint:(CGPoint)point byAngle:(CGFloat)angle {
CGFloat sinA = sin(angle);
CGFloat cosA = cos(angle);
CGFloat x = point.x;
CGFloat y = point.y;
CGAffineTransform transform = CGAffineTransformMake(cosA,sinA,-sinA,cosA,x-x*cosA+y*sinA,y-x*sinA-y*cosA);
[UIView animateWithDuration:4.0 animations:^{
imageView.transform = transform;
}];
}
请注意,角度为弧度,因此完整旋转为2.0 * M_PI。
答案 1 :(得分:1)
也许您必须设置CALayer的anchorPoint:有关更多详细信息,请参阅Specifying a Layer’s Geometry。
//(0,0) is the left-bottom of your layer bounds
self->greyRings.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
我认为负面的anchorPoint也应该有效。所以你最好给出你的界限,我们可以为你计算。