CoreAnimation 细说动画(二)

本文深入探讨了iOS开发中UIView过渡效果的实现方法及CoreAnimation动画原理,包括四种视图翻转效果、层过渡动画以及如何通过代理方法控制动画。详细介绍了动画效果的API使用、过渡类型选择、动画曲线、持续时间设置等关键点。

UIView 过渡

使用的API:setAnimationTransition:forView:cache: 参数为:一个过渡类型,要进行过渡操作的组件的父视图,一个Boolean值用于描述是否在动画之前和之后需要Core Animation进行缓存,缓存可以提升Core Animation的性能。

4种效果:

UIViewAnimationTransitionFlipFromRight 视图内容从右向左翻转,展现屏幕背面的另外一个视图。
UIViewAnimationTransitionFlipFromLeft 视图内容从左向右翻转,展现屏幕背面的另外一个视图。
UIViewAnimationTransitionCurlUp 视图翻卷起来。
UIViewAnimationTransitionCurlDown 显示和覆盖当前视图。

看下面的例子和图示:

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIView *animationView = [[UIView alloc] init];
    animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationView];
    animationView.tag = 100;
    animationView.transform = CGAffineTransformIdentity;
    animationView.frame = self.view.frame;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 50);
    [btn setTitle:@"点击看动画" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(theAnimationShow) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
 
    // Do any additional setup after loading the view, typically from a nib.
 
 
}
 
- (void)theAnimationShow
{
    UIView *animationView = [self.view viewWithTag:100];
 
    [UIView beginAnimations:@"ViewAnimationOne" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(theAnimationStop:finish:context:)];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:animationView cache:YES];
    [UIView commitAnimations];
}

效果图:

其它的效果只需要替换上面代码中
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:animationView cache:YES];的UIViewAnimationTransitionFlipFromRight 过渡样式即可。

按照上面介绍的顺序效果依次为:

www.helmsmansoft.comwww.helmsmansoft.comwww.helmsmansoft.com

以上图是我用模拟器截的 有的可能存在一些问题,大概是个意思….

连续变化的Core Animation层

动画有隐式和显示之分。隐式动画指的是,无须创建动画对象,只需改变动画层的属性,让核心动画自己去完成动画效果。显示动画指的是,需要自己创建和管理动画对象,并且将它们应用到动画层,才能显示动画效果。

额,关于隐式动画,由于其持续的时间比较短,几乎是看不出来的,如果要达到明显的动画效果,就需要显式的调用动画。

这里介绍下显示动画:层过渡动画

kCATransitionFade 淡入或淡出层内容,使得内容变的可见或隐藏。
kCATransitionMoveIn 要显示的层滑过底下的层
kCATransitionPush 要显示的层滑入屏幕的时候将底下的层推出屏幕
kCATransitionReveal 要显示的层渐渐显示出来,覆盖底下的层

由于要对层(layer)进行动画操作,所以需要添加QuartzCode.framework框架,并导入头文件#import

具体的操作效果看下面代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    self.view.backgroundColor = [UIColor redColor];
 
    UIView *layerView = [[UIView alloc] initWithFrame:self.view.frame]; 
    [layerView setBackgroundColor:[UIColor orangeColor]];
    layerView.tag = 100;
    [self.view addSubview:layerView];
    [layerView release];
 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 120, 50);
    [btn setTitle:@"点击查看动画" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(theAnimtaionBegin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
 
	// Do any additional setup after loading the view, typically from a nib.
}
 
- (void)theAnimtaionBegin
{
    UIView *layerView = [self.view viewWithTag:100];
    CATransition *transition= [CATransition animation];
    transition.duration = 1.0;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transition.delegate = self;
    transition.type = kCATransitionFade;  //过渡类型,淡入或淡出
    [layerView.layer addAnimation:transition forKey:@"transtionone"];
    layerView.alpha = 0.0;
}
 
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"....stop.....");
}

从上面的代码能正常运行,就可以发现,在Core Animation动画中,代理方法不需要设置选择器

移入、推出和显示

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    self.view.backgroundColor = [UIColor redColor];
 
    UIView *layerView = [[UIView alloc] initWithFrame:self.view.frame]; 
    [layerView setBackgroundColor:[UIColor orangeColor]];
    layerView.tag = 100;
    [self.view addSubview:layerView];
    [layerView release];
 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 120, 50);
    [btn setTitle:@"点击查看动画" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(theAnimtaionBegin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
 
	// Do any additional setup after loading the view, typically from a nib.
}
 
- (void)theAnimtaionBegin
{
    UIView *layerView = [self.view viewWithTag:100];
    CATransition *transition= [CATransition animation];
    transition.duration = 1.0;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    transition.delegate = self;
    transition.type = kCATransitionReveal;
    transition.subtype = kCATransitionFromTop;  //过渡方向
    [layerView.layer addAnimation:transition forKey:@"transtionone"];
    layerView.hidden = YES;
}

主要
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromTop; //过渡方向

改变这两个的属性即可。


转载自:舵手网络--http://www.helmsmansoft.com

   本文链接地址: http://www.helmsmansoft.com/index.php/archives/1499


基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值