加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 19:06:50 所属栏目:百科 来源:网络整理
导读:我可以通过此代码在此处移动或动画我的UIView: - (void) makeAnim1{ //downward animation [UIView animateWithDuration:1.5 delay:0.15 options: UIViewAnimationCurveLinear animations:^{ carousel.frame = CGRectOffset(carousel.frame,650); } complet
我可以通过此代码在此处移动或动画我的UIView:

- (void) makeAnim1{

    //downward animation 
    [UIView animateWithDuration:1.5
                          delay:0.15
                        options: UIViewAnimationCurveLinear
                     animations:^{
                         carousel.frame = CGRectOffset(carousel.frame,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,-650);
                     } 
                     completion:^(BOOL finished){
                        NSLog(@"Done!");
                     }];    


}

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

谢谢您的帮助.

解决方法

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

// rotate pi/2 degrees clockwise
carousel.transform = CGAffineTransformMakeRotation(M_PI_2);

如果你需要进行更高级的变换,比如围绕另一个轴旋转,那么你需要使用一点Core Animation并设置views层的transform属性(它采用CATransform3D而不是CGAffineTransform).

与所有Core Animation代码一样,您需要导入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"];

上面的代码确实会围绕x轴旋转视图,但是在没有透视的情况下看起来会非常愚蠢(搜索SO的“透视核心动画”,之前已经被问过).可能需要进行大量调整以获得正确的外观,但这应该足以让您入门.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读