自定义过渡动画

简介: 自定义过渡动画

上一篇中说到过渡动画,使用CATransition,但是CATransition提供的动画类型太少了,而且苹果通过 UIView

+transitionFromView:toView:duration:options:completion: +transitionWithView:duration:opt ions:animations:方法提供了Core Animation的过渡效果,除了UIViewAnimationOptionTransitionCrossDissolve外,其他常量和CATransition的type提供的常量完全无关也不同。


**


UIView特性提供的动画


**

UIView特性中提供的几个常量如下:

 UIViewAnimationOptionTransitionNone               = 0 << 20, // default
    UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
    UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
    UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
    UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
    UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,
    UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,
    UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,

使用方法请看下面代码:

 [UIView transitionWithView:imageView duration:1.0
                       options:UIViewAnimationOptionTransitionCurlDown
                    animations:^{
                        //cycle to next image
                        UIImage *currentImage = imageView.image;
                        NSUInteger index = [images indexOfObject:currentImage];
                        index = (index + 1) % [images count];
                        imageView.image = images[index];
                    }
                    completion:NULL];

这个方法想必大家都很熟悉,博主也是在看到这里时才恍然大悟,原来这个方法是这样的,它不属于Core Animation,但却提供了Core Animation的过渡效果。


以上所说为UIView的特性提供的Core Animation过渡效果,在开发中属于比较常见的。


**


自定义动画


**


系统提供的动画毕竟是有限的,当一个动画用系统的属性解决不了怎么办?这事后就要自定义动画了,怎么自定义呢?我们都知道旋转,缩放,平移,把这些过程通过UIView的特性展现出来就组合成了一系列的动画,先看使用方法:

//截图 
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, YES, 0.0);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *coverImage = UIGraphicsGetImageFromCurrentImageContext();
//拿到截图的图片,放置在原图位置坐操作
UIView *coverView = [[UIImageView alloc] initWithImage:coverImage];
coverView.frame = CGRectMake(80, 80, 150, 150);
[self.view addSubview:coverView];
//update the view color(we'll simply randomize the layer background color)
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; //perform animation (anything you like)
[UIView animateWithDuration:1.0 animations:^{
        //scale, rotate and fade the view
        CGAffineTransform transform = CGAffineTransformMakeScale(0.01, 0.01);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        coverView.transform = transform;
        coverView.alpha = 0.0;
        UIImage *currentImage = imageView.image;
        NSUInteger index = [images indexOfObject:currentImage];
        index = (index + 1) % [images count];
        imageView.image = images[index];
    } completion:^(BOOL finished) {
        //remove the cover view now we're finished with it
        [coverView removeFromSuperview];
    }];

下面看下上面两种动画的效果:

image.png

这样看来,动画就没那么复杂了吧?类似的效果还有很多,方法也不限定,可以自己组合,还有一个就是利用前面说过的动画组和基础动画,关键帧动画来组合不同的动画效果。不知道的去翻前面的博客吧。


关于动画博主会不间断更新内容,欢迎关注。

最后不要把正事忘了,Demo下载地址:https://github.com/codeliu6572/Custom_Animation

目录
相关文章
|
3天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8097 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10645 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全
|
11天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1605 5
|
10天前
|
云安全 数据采集 人工智能
|
17天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2757 8
|
10天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2727 0