IOS 简单的动画自定义方法(旋转、移动、闪烁等)

#define kDegreesToRadian(x) (M_PI * (x) / 180.0)

#define kRadianToDegrees(radian) (radian*180.0)/(M_PI)

- (void)viewDidLoad

{

[superviewDidLoad];

self.title = @"测试动画";

self.view.backgroundColor = [UIColor lightGrayColor];

myTest1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 60, 40)];

myTest1.backgroundColor = [UIColor blueColor];

myTest1.textAlignment = NSTextAlignmentCenter;

myTest1.text = @"张明炜";

myTest1.textColor = [UIColor whiteColor];

[self.view addSubview:myTest1];

//闪烁效果。

//    [myTest1.layer addAnimation:[self opacityForever_Animation:0.5] forKey:nil];

///移动的动画。

//    [myTest1.layer addAnimation:[self moveX:1.0f X:[NSNumber numberWithFloat:200.0f]] forKey:nil];

//缩放效果。

//    [myTest1.layer addAnimation:[self scale:[NSNumber numberWithFloat:1.0f] orgin:[NSNumber numberWithFloat:3.0f] durTimes:2.0f Rep:MAXFLOAT] forKey:nil];

//组合动画。

//    NSArray *myArray = [NSArray arrayWithObjects:[self opacityForever_Animation:0.5],[self moveX:1.0f X:[NSNumber numberWithFloat:200.0f]],[self scale:[NSNumber numberWithFloat:1.0f] orgin:[NSNumber numberWithFloat:3.0f] durTimes:2.0f Rep:MAXFLOAT], nil];

//    [myTest1.layer addAnimation:[self groupAnimation:myArray durTimes:3.0f Rep:MAXFLOAT] forKey:nil];

//路径动画。

//    CGMutablePathRef myPah = CGPathCreateMutable();

//    CGPathMoveToPoint(myPah, nil,30, 77);

//    CGPathAddCurveToPoint(myPah, nil, 50, 50, 60, 200, 200, 200);//这里的是控制点。

//    [myTest1.layer addAnimation:[self keyframeAnimation:myPah durTimes:5 Rep:MAXFLOAT] forKey:nil];

//旋转动画。

[myTest1.layeraddAnimation:[self rotation:2 degree:kRadianToDegrees(90) direction:1 repeatCount:MAXFLOAT] forKey:nil];

}

#pragma mark === 永久闪烁的动画 ======

-(CABasicACnimation *)opacityForever_Animation:(float)time

{

CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"opacity"];//必须写opacity才行。

animation.fromValue = [NSNumbernumberWithFloat:1.0f];

animation.toValue = [NSNumbernumberWithFloat:0.0f];//这是透明度。

animation.autoreverses = YES;

animation.duration = time;

animation.repeatCount = MAXFLOAT;

animation.removedOnCompletion = NO;

animation.fillMode = kCAFillModeForwards;

animation.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。

return animation;

}

#pragma mark =====横向、纵向移动===========

-(CABasicAnimation *)moveX:(float)time X:(NSNumber *)x

{

CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"transform.translation.x"];///.y的话就向下移动。

animation.toValue = x;

animation.duration = time;

animation.removedOnCompletion = NO;//yes的话,又返回原位置了。

animation.repeatCount = MAXFLOAT;

animation.fillMode = kCAFillModeForwards;

return animation;

}

#pragma mark =====缩放-=============

-(CABasicAnimation *)scale:(NSNumber *)Multiple orgin:(NSNumber *)orginMultiple durTimes:(float)time Rep:(float)repertTimes

{

CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];

animation.fromValue = Multiple;

animation.toValue = orginMultiple;

animation.autoreverses = YES;

animation.repeatCount = repertTimes;

animation.duration = time;//不设置时候的话,有一个默认的缩放时间.

animation.removedOnCompletion = NO;

animation.fillMode = kCAFillModeForwards;

return  animation;

}

#pragma mark =====组合动画-=============

-(CAAnimationGroup *)groupAnimation:(NSArray *)animationAry durTimes:(float)time Rep:(float)repeatTimes

{

CAAnimationGroup *animation = [CAAnimationGroupanimation];

animation.animations = animationAry;

animation.duration = time;

animation.removedOnCompletion = NO;

animation.repeatCount = repeatTimes;

animation.fillMode = kCAFillModeForwards;

return animation;

}

#pragma mark =====路径动画-=============

-(CAKeyframeAnimation *)keyframeAnimation:(CGMutablePathRef)path durTimes:(float)time Rep:(float)repeatTimes

{

CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

animation.path = path;

animation.removedOnCompletion = NO;

animation.fillMode = kCAFillModeForwards;

animation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];

animation.autoreverses = NO;

animation.duration = time;

animation.repeatCount = repeatTimes;

return animation;

}

#pragma mark ====旋转动画======

-(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount

{

CATransform3D rotationTransform = CATransform3DMakeRotation(degree, 0, 0, direction);

CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"transform"];

animation.toValue = [NSValue valueWithCATransform3D:rotationTransform];

animation.duration  =  dur;

animation.autoreverses = NO;

animation.cumulative = NO;

animation.fillMode = kCAFillModeForwards;

animation.repeatCount = repeatCount;

animation.delegate = self;

return animation;

}

转自:http://zhangmingwei.iteye.com/blog/2101782

时间: 2024-08-06 16:13:25

IOS 简单的动画自定义方法(旋转、移动、闪烁等)的相关文章

cocos2d-x ios游戏开发初认识(七) 简单的动画

前面有一节说了帧动画,就是让精灵改变自己的位置.形状.大小来实现相应的动作,这讲主要是要通过一些方法来实现精灵的移动,产生各种炫丽的动画,也可能让你找到一点游戏场景. 下面具体根据代码分析: 为了清晰最好将前几节的代码注释掉. //根据前面的知识先创建一个菜单 CCMenuItemFont *item =CCMenuItemFont::create("开始游戏",this, menu_selector(MainScene::onMenuItem));//点击事件 //添加到菜单栏里面

iOS百思不得姐、ARKit、旋转动画、立体相册源码等

iOS精选源码 自定义视图弹出实现方案 仿写百思不得姐 ARKit,距离感应,AR尺子 iOS传感器集锦 AR太阳系,动画与光线处理,ARKit iOS启动页广告JYJAdViewController ThemeManager 是一个轻量级的主题管理库,使用简单方便无耦合 旋转跑马屏风动画Donut ARKit,AR尺子 3D立体可旋转相册 -- 原创应用:大杨子相册 iOS优质博客 iOS代码加固/混淆 众所周知的是大部分iOS代码一般不会做加密加固,因为iOS APP一般是通过AppStor

文顶顶 iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

iOS开发-三种简单的动画设置

[在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所有动画提交并生成动画 [UIView commit

iOS开发中三种简单的动画设置

iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所有动画提交并生成动

iOS简单动画

知识架构 CALayer 图层类 CABasicAnimation 基础动画 CAKeyFrameAnimation 帧动画 CATransition 转场动画 CAAnimationGroup 动画组 layer的基本概念 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层,在创建UIView对象时,UIView内部会自动创建一个图层(CALyer对象),通过UIView的layer属性可以访问这个层. 基本属性  Bounds;position;frame;background

IOS 开发UI篇—iOS开发中三种简单的动画设置

一.首尾式动画 // beginAnimations表示此后的代码要"参与到"动画中     [UIView beginAnimations:nil context:nil]; //设置动画时长     [UIView setAnimationDuration:2.0];            self.headImageView.bounds = rect;     // commitAnimations,将beginAnimation之后的所有动画提交并生成动画     [UIVi

iOS UI-三种简单的动画设置

一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所有动画提交并生成动画 [UIView commitAnimations]; 说明:如