CGGraphics图像上下文动画在iOS 5.1中的旋转

时间:2012-07-14 19:11:18

标签: ios image animation rotation

我正在iOS中创建旋转旋钮按钮。图形设计师给了我png图层,所以我使用图像上下文来混合图层。

在旋转时,只有部分图层会旋转。我的目标是为这些轮换制作动画。

目前,我正在使用

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
- (BOOL)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event

在这些方法中,我计算所需的图像旋转并绘制它。

这很好用。我遇到的障碍是

- (BOOL)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event

我想做一个“捕捉到”动画。我怎样才能做到这一点? 由于混合,我不确定在这种情况下如何使用仿射变换。以下方法是我如何构建图像:

- (UIImage *)knobImageWithRadians:(float)radians
{
UIImage *i05 = [UIImage imageNamed:@"05 - Metal Over Empty Space - ROTATE ME.png"];
UIImage *i04 = [UIImage imageNamed:@"04 - Inner Knob.png"];
UIImage *i03 = [UIImage imageNamed:@"03 - Over Knob Metal - (OVERLAY BLEND)  - ROTATE ME.png"];
UIImage *i02 = [UIImage imageNamed:@"02 - Numbers and Dashes - ROTATE ME.png"];
UIImage *i01 = [UIImage imageNamed:@"01 - Reflective Overlay - (COLOR DODGE BLEND).png"];

CGSize size = i11.size;     

UIGraphicsBeginImageContext(size);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, size.width/2, size.height/2);


[i04 drawInRect:CGRectMake(-i04.size.width/2, -i04.size.height/2, i04.size.width, i04.size.height) blendMode:kCGBlendModeNormal alpha:1.0];

CGContextRotateCTM(bitmap, radians);
[i03 drawInRect:CGRectMake(-i03.size.width/2, -i03.size.height/2, i03.size.width, i03.size.height) blendMode:kCGBlendModeOverlay alpha:1.0];

[i02 drawInRect:CGRectMake(-i02.size.width/2, -i02.size.height/2, i02.size.width, i02.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRotateCTM(bitmap, -radians);

[i01 drawInRect:CGRectMake(-i01.size.width/2, -i01.size.height/2, i01.size.width, i01.size.height) blendMode:kCGBlendModeColorDodge alpha:1.0];

UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return blendedImage;
}

更新:

现在,我只是让它直接捕捉而没有任何动画。

1 个答案:

答案 0 :(得分:2)

您必须通过定期以新角度重新绘制图像来自行设置动画。或者提出仅使用源而不是合成的视觉效果,这样您就可以将每个图像放在一个单独的图层中。