像核心动画老虎机一样在z轴上旋转

Baz*_*nga 5 iphone xcode core-animation ios

我可以通过此代码在此处移动或动画我的UIView:

- (void) makeAnim1{

    //downward animation 
    [UIView animateWithDuration:1.5
                          delay:0.15
                        options: UIViewAnimationCurveLinear
                     animations:^{
                         carousel.frame = CGRectOffset(carousel.frame, 0, 650);
                     }
                     completion:^(BOOL finished){ //task after an animation ends
                         [self performSelector:@selector(makeAnim1_1) withObject:nil afterDelay:2.0];
                         NSLog(@"Done!");
                     }];    
}

- (void) makeAnim1_1{

    //upward animation
    [UIView animateWithDuration:1.5
                          delay:0.1
                        options: UIViewAnimationCurveLinear
                     animations:^{
                         carousel.frame = CGRectOffset(carousel.frame, 0, -650);
                     } 
                     completion:^(BOOL finished){
                        NSLog(@"Done!");
                     }];    


} 
Run Code Online (Sandbox Code Playgroud)

但它只是上下移动UIView.如何让它像一个旋转,Slot machine但只包含一个图像或视图.像在z轴上旋转一样.但要让它看起来像包含多个图像.

谢谢您的帮助.

Dav*_*ist 1

您无需更改frame动画块内部,而是更改transform. 变换可用于缩放、旋转和平移(移动)视图。您只能绕 z 轴旋转,但这正是您所要求的。transform视图上的属性采用,CGAffineTransform如下所示:

// rotate pi/2 degrees clockwise
carousel.transform = CGAffineTransformMakeRotation(M_PI_2);
Run Code Online (Sandbox Code Playgroud)

如果您需要进行更高级的变换,例如绕另一个轴旋转,那么您需要使用一点核心动画并设置视图层的变换属性(需要 a 而CATransform3D不是 a CGAffineTransform)。

与所有核心动画代码一样,您需要导入 QuartzCore.framework 并在代码中包含 QuartzCore/QuartzCore.h。

您正在执行的上述动画是 UIView 动画,仅用于对视图进行动画处理,但您要求的动画需要视图层的更高级动画。我建议您查看 CABasicAnimation 的文档,并查看 iOS 的核心动画编程指南以了解更多信息。

您可以像这样设置视图图层的 x 旋转动画:

CABasicAnimation *slotAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
[slotAnimation setToValue:[NSNumber numberWithFloat:M_PI_2]];
// animation customizations like duration and timing 
// that you can read about in the documentation 
[[carousel layer] addAnimation:slotAnimation forKey:@"mySlotAnimation"];
Run Code Online (Sandbox Code Playgroud)

上面的代码确实会绕 x 轴旋转视图,但如果没有透视,看起来会很愚蠢(搜索“perspective Core Animation”,之前已经有人问过)。可能需要进行很多调整才能获得正确的外观,但这应该足以让您开始。