iOS的动画切换

转载的一片关于动画的文章,很全

CALayer下的CATransition

一.封装动画方法

1.用CATransition实现动画的封装方法如下,每句代码是何意思,请看注释之。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma CATransition动画实现
- (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIView *) view
{
     //创建CATransition对象
     CATransition *animation = [CATransition animation];
     
     //设置运动时间
     animation.duration = DURATION;
     
     //设置运动type
     animation.type = type;
     if  (subtype != nil) {
         
         //设置子类
         animation.subtype = subtype;
     }
     
     //设置运动速度
     animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
     
     [view.layer addAnimation:animation forKey:@ "animation" ];
}

代码说明:

CATransition常用的属性如下:

  • duration:设置动画时间

  • type:稍后下面会详细的介绍运动类型

  • subtype:和type匹配使用,指定运动的方向,下面也会详细介绍

  • timingFunction :动画的运动轨迹,用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是均匀变化(相同时间变化量相同)还是先快后慢,先慢后快还是先慢再快再慢。  

动画的开始与结束的快慢,有五个预置分别为(下同):

  •  kCAMediaTimingFunctionLinear            线性,即匀速

  •  kCAMediaTimingFunctionEaseIn            先慢后快

  • kCAMediaTimingFunctionEaseOut           先快后慢

  • kCAMediaTimingFunctionEaseInEaseOut     先慢后快再慢

  •  kCAMediaTimingFunctionDefault           实际效果是动画中间比较快.

2.用UIView的block回调实现动画的代码封装 

1
2
3
4
5
6
7
8
  #pragma UIView实现动画
  - (void) animationWithView : (UIView *)view WithAnimationTransition : (UIViewAnimationTransition) transition
  {
      [UIView animateWithDuration:DURATION animations:^{
          [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
          [UIView setAnimationTransition:transition forView:view cache:YES];
      }];
  }

3.改变View的背景图,便于切换时观察

1
2
3
4
5
  #pragma 给View添加背景图
  -(void)addBgImageWithImageName:(NSString *) imageName
  {
      self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageName]];
  }

 二.调用上面的方法实现我们想要的动画

1.我们在View上添加多个Button,给不同的Button设置不同的Tag值,然后再ViewController中绑定同一个方法,点击不同的button实现不同的页面切换效果。storyBoard上的控件效果如下图所示:

121710078375833.png

2.下面我们就开始编写点击button要回调的方法

(1).定义枚举来标示按钮所对应的动画类型,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef enum : NSUInteger {
     Fade = 1,                    //淡入淡出
     Push,                        //推挤
     Reveal,                      //揭开
     MoveIn,                      //覆盖
     Cube,                        //立方体
     SuckEffect,                  //吮吸
     OglFlip,                     //翻转
     RippleEffect,                //波纹
     PageCurl,                    //翻页
     PageUnCurl,                  //反翻页
     CameraIrisHollowOpen,        //开镜头
     CameraIrisHollowClose,       //关镜头
     CurlDown,                    //下翻页
     CurlUp,                      //上翻页
     FlipFromLeft,                //左翻转
     FlipFromRight,               //右翻转
     
} AnimationType;

(2),获取Button的Tag值:

1
2
UIButton *button = sender;
AnimationType animationType = button.tag;

(3).每次点击button都改变subtype的值,包括上,左,下,右

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
NSString *subtypeString;
     
     switch  (_subtype) {
         case  0:
             subtypeString = kCATransitionFromLeft;
             break ;
         case  1:
             subtypeString = kCATransitionFromBottom;
             break ;
         case  2:
             subtypeString = kCATransitionFromRight;
             break ;
         case  3:
             subtypeString = kCATransitionFromTop;
             break ;
         default :
             break ;
     }
     _subtype += 1;
     if  (_subtype > 3) {
         _subtype = 0;
     }

(4)通过switch结合上边的枚举来判断是那个按钮点击的

1
2
3
4
switch  (animationType)
{
      //各种Case,此处代码下面会给出  
  }

3.调用我们封装的运动方法,来实现动画效果

(1)淡化效果

1
2
3
case  Fade:
[self transitionWithType:kCATransitionFade WithSubtype:subtypeString ForView:self.view];
break ;

(2).Push效果

1
2
3
case  Push:
[self transitionWithType:kCATransitionPush WithSubtype:subtypeString ForView:self.view];
break ;

效果如下:

121723047906597.png


(3).揭开效果:

1
2
3
case  Reveal:
[self transitionWithType:kCATransitionReveal WithSubtype:subtypeString ForView:self.view];
break ;

效果图如下:

04.png

(4).覆盖效果

1
2
3
case  MoveIn:
[self transitionWithType:kCATransitionMoveIn WithSubtype:subtypeString ForView:self.view];
break ;

效果图如下:

05.png

(5).立方体效果

1
2
3
case  Cube:
[self transitionWithType:@ "cube"  WithSubtype:subtypeString ForView:self.view];
break ;

效果如下:

06.png

(6).吮吸效果

1
2
3
case  SuckEffect:
[self transitionWithType:@ "suckEffect"  WithSubtype:subtypeString ForView:self.view];
break ;

效果如下:

07.png

(7).翻转效果

1
2
3
case  OglFlip:
[self transitionWithType:@ "oglFlip"  WithSubtype:subtypeString ForView:self.view];
break ;

图如下:

007.png

(8).波纹效果

1
2
3
case  RippleEffect:
[self transitionWithType:@ "rippleEffect"  WithSubtype:subtypeString ForView:self.view];
break ;

008.png

(9).翻页和反翻页效果

1
2
3
4
5
6
7
case  PageCurl:
             [self transitionWithType:@ "pageCurl"  WithSubtype:subtypeString ForView:self.view];
             break ;
             
         case  PageUnCurl:
             [self transitionWithType:@ "pageUnCurl"  WithSubtype:subtypeString ForView:self.view];
             break ;

009.jpg

(10).相机打开效果

1
2
3
4
5
6
7
case  CameraIrisHollowOpen:
             [self transitionWithType:@ "cameraIrisHollowOpen"  WithSubtype:subtypeString ForView:self.view];
             break ;
             
         case  CameraIrisHollowClose:
             [self transitionWithType:@ "cameraIrisHollowClose"  WithSubtype:subtypeString ForView:self.view];
             break ;

10.png

(11).调用上面封装的第二个动画方法


在 Flutter 中,你可以使用 `CupertinoTabBar` 和 `CupertinoTabScaffold` 来实现带有动画的 TabBar 切换效果。下面是一个简单的示例代码: ```dart import 'package:flutter/cupertino.dart'; class MyTabScreen extends StatefulWidget { @override _MyTabScreenState createState() => _MyTabScreenState(); } class _MyTabScreenState extends State<MyTabScreen> { int _currentIndex = 0; @override Widget build(BuildContext context) { return CupertinoTabScaffold( tabBar: CupertinoTabBar( items: [ BottomNavigationBarItem( icon: Icon(CupertinoIcons.home), label: 'Home', ), BottomNavigationBarItem( icon: Icon(CupertinoIcons.search), label: 'Search', ), BottomNavigationBarItem( icon: Icon(CupertinoIcons.person), label: 'Profile', ), ], currentIndex: _currentIndex, onTap: (index) { setState(() { _currentIndex = index; }); }, ), tabBuilder: (BuildContext context, int index) { return CupertinoTabView( builder: (BuildContext context) { switch (index) { case 0: return HomeScreen(); case 1: return SearchScreen(); case 2: return ProfileScreen(); default: return Container(); } }, ); }, ); } } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text('Home'), ), child: Center( child: Text('Home Screen'), ), ); } } class SearchScreen extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text('Search'), ), child: Center( child: Text('Search Screen'), ), ); } } class ProfileScreen extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text('Profile'), ), child: Center( child: Text('Profile Screen'), ), ); } } ``` 在这个示例中,我们使用 `CupertinoTabScaffold` 和 `CupertinoTabBar` 来创建底部的 TabBar,然后在 `tabBuilder` 方法中根据索引值切换显示不同的页面。在 `onTap` 回调中,我们可以更新 `_currentIndex` 的值来实现切换 Tab 时的动画效果。 以上是一个基本的示例,你可以根据自己的需求进行定制和修改。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值