• CATransition(过渡)


    属性动画只对图层的可动画属性起作用,所以如果要改变一个不能动画的属性(比如图片),或者从层级关系中添加或者移除图层,属性动画将不起作用。

    于是就有了过渡的概念。过渡并不像属性动画那样平滑地在两个值之间做动画,而是影响到整个图层的变化。过渡动画首先展示之前的图层外观,然后通过一个交换过渡到新的外观。

    为了创建一个过渡动画,我们将使用CATransition,同样是另一个CAAnimation的子类,和别的子类不同,CAAnimation有一个type和subtype来标识变换效果;

    type:

    kCATransitionFade 
    kCATransitionMoveIn 
    kCATransitionPush 
    kCATransitionReveal
     
    kCATransitionMoveIn从顶部滑动进入,但不像推送动画那样把老土层推走,然而kCATransitionReveal把原始的图层滑动出去来显示新的外观,而不是把新的图层滑动进入。
     
    subtype(可以通过subtype来控制它们的方向):
    kCATransitionFromRight 
    kCATransitionFromLeft 
    kCATransitionFromTop 
    kCATransitionFromBottom
     
    清单8.11 使用CATransition来对UIImageView做动画
    @interface ViewController ()
    @property (nonatomic, weak) IBOutlet UIImageView *imageView;
    @property (nonatomic, copy) NSArray *images;
    @end
    @implementation ViewController
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //set up images
        self.images = @[[UIImage imageNamed:@"Anchor.png"],
                        [UIImage imageNamed:@"Cone.png"],
                        [UIImage imageNamed:@"Igloo.png"],
                        [UIImage imageNamed:@"Spaceship.png"]];
    }
    - (IBAction)switchImage
    {
        //set up crossfade transition
        CATransition *transition = [CATransition animation];
        transition.type = kCATransitionFade;
        //apply transition to imageview backing layer
        [self.imageView.layer addAnimation:transition forKey:nil];
        //cycle to next image
        UIImage *currentImage = self.imageView.image;
        NSUInteger index = [self.images indexOfObject:currentImage];
        index = (index + 1) % [self.images count];
        self.imageView.image = self.images[index];
    }
    @end
     

    你可以从代码中看出,过渡动画和之前的属性动画或者动画组添加到图层上的方式一致,都是通过-addAnimation:forKey:方法。但是 和属性动画不同的是,对指定的图层一次只能使用一次CATransition,因此,无论你对动画的键设置什么值,过渡动画都会对它的键设置成 “transition”,也就是常量kCATransition。

    8.4.jpeg

    隐式过渡

    CATransision 可以对图层任何变化平滑过渡的事实使得它成为那些不好做动画的属性图层行为的理想候选。苹果当然意识到了这点,并且当设置了CALayer的 content属性的时候,CATransition的确是默认的行为。但是对于视图关联的图层,或者是其他隐式动画的行为,这个特性依然是被禁用的,但 是对于你自己创建的图层,这意味着对图层contents图片做的改动都会自动附上淡入淡出的动画。

    我们在第七章使用CATransition作为一个图层行为来改变图层的背景色,当然backgroundColor属性可以通过正常的CAPropertyAnimation来实现,但这不是说不可以用CATransition来实行。

    对图层树的动画

    CATransition 并不作用于指定的图层属性,这就是说你可以在即使不能准确得知改变了什么的情况下对图层做动画,例如,在不知道UITableView哪一行被添加或者删 除的情况下,直接就可以平滑地刷新它,或者在不知道UIViewController内部的视图层级的情况下对两个不同的实例做过渡动画。

    这些例子和我们之前所讨论的情况完全不同,因为它们不仅涉及到图层的属性,而且是整个图层树的改变--我们在这种动画的过程中手动在层级关系中添加或者移除图层。

    这里用到了一个小诡计,要确保CATransition添加到的图层在过渡动画发生时不会在树状结构中被移除,否则CATransition将会和图层一起被移除。一般来说,你只需要将动画添加到被影响图层的superlayer。

    在 清单8.12中,我们展示了如何在UITabBarController切换标签的时候添加淡入淡出的动画。这里我们建立了默认的标签应用程序模板,然后用 UITabBarControllerDelegate的-tabBarController:didSelectViewController:方法来 应用过渡动画。我们把动画添加到UITabBarController的视图图层上,于是在标签被替换的时候动画不会被移除。

    清单8.12 对UITabBarController做动画

    #import "AppDelegate.h"
    #import "FirstViewController.h" 
    #import "SecondViewController.h"
    #import @implementation AppDelegate
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
        UIViewController *viewController1 = [[FirstViewController alloc] init];
        UIViewController *viewController2 = [[SecondViewController alloc] init];
        self.tabBarController = [[UITabBarController alloc] init];
        self.tabBarController.viewControllers = @[viewController1, viewController2];
        self.tabBarController.delegate = self;
        self.window.rootViewController = self.tabBarController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {
        //set up crossfade transition
        CATransition *transition = [CATransition animation];
        transition.type = kCATransitionFade;
        //apply transition to tab bar controller's view
        [self.tabBarController.view.layer addAnimation:transition forKey:nil];
    }
    @end
  • 相关阅读:
    H5,JS国际化网站中英文切换
    DIV右上角标签的CSS3实现技巧
    .NET Core也是国产化信息系统开发的重要选项
    微软自家的.Net下的JavaScript引擎--- ClearScript
    Java 生态碎片化 和 .NET生态的一致性
    使用 .NET 进行游戏开发
    .NET 是信息技术应用创新产业重要参与者
    Chrome 80+ 跨域Samesite 导致的cookie not found 解决方法
    在腾讯云云函数计算上部署.NET Core 3.1
    腾讯云 云开发 部署 Blazor网站
  • 原文地址:https://www.cnblogs.com/jingdizhiwa/p/5490321.html
Copyright © 2020-2023  润新知