0

嗨,我有一个 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"];

我目前正在使用该代码,但它只是围绕屏幕中心旋转。

请问有什么想法吗?

4

2 回答 2

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。

于 2012-08-28T15:29:41.143 回答
1

也许您必须设置 CALayer 的 anchorPoint:有关详细信息,请参阅指定图层的几何图形。

//(0,0) is the left-bottom of your layer bounds
self->greyRings.layer.anchorPoint = CGPointMake(0.0f, 0.0f);

我认为负锚点也应该起作用。所以你最好给出你的界限,我们可以为你计算。

于 2012-08-28T15:45:57.157 回答