iOS图像动画iOS 开发 iOS开发

UIView-Animation动画 Version0.3

2015-06-19  本文已影响152人  石丘
执行方法

//准备开始动画,animationID 标识动画,context自定义消息
+(void)beginAnimations:(NSString *)animationID context:(void *)context;

// 运行动画
+ (void)commitAnimations;

// 设置代理 default = nil
+ (void)setAnimationDelegate:(id)delegate;

// 将要开始动画时执行某些方法
+ (void)setAnimationWillStartSelector:(SEL)selector;

// 动画结束执行某些操作
+ (void)setAnimationDidStopSelector:(SEL)selector;

// default = 0.2 ,设置动画持续事件
+ (void)setAnimationDuration:(NSTimeInterval)duration;

// default = 0.0 , 设置动画开始时间
+ (void)setAnimationDelay:(NSTimeInterval)delay;

// default = now ([NSDate date]) , 设置动画开始日期
+ (void)setAnimationStartDate:(NSDate *)startDate;

// default = UIViewAnimationCurveEaseInOut 设置动画运动效果
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;

// default = 0.0 设置动画重复次数
+ (void)setAnimationRepeatCount:(float)repeatCount;

//default = NO. 动画自动重复,重复次数不能为0
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;

// default = NO. If YES如果是,当前视图的位置总是用于新的动画,否则,最终状态是用于动画。
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;

// current limitation - only one per begin/commit
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;

// ignore any attribute changes while set.
+ (void)setAnimationsEnabled:(BOOL)enabled;

// 返回一个bool值,表示动画是否结束了
+ (BOOL)areAnimationsEnabled;

//新开一条线程执行
+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation

🌰
<code>
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
要执行的动画
[UIView commitAnimations];

</code>
+(void)animateWithDuration:(NSTimeInterval)duration//时间 delay:(NSTimeInterval)delay//延迟 options:(UIViewAnimationOptions)options//动画方式 animations:(void (^)(void))animations//变换内容 completion:(void (^)(BOOL finished))completion//变换结束

动画效果

跟父类作为一个整体一起动画,此方式为默认方式
default
UIViewAnimationOptionLayoutSubviews

动画过程中 允许用户点击 比如button
turn on user interaction while animating
UIViewAnimationOptionAllowUserInteraction

从当前状态开始动画,父层动画运动期间,开始子层动画
start all views from current value, not initial value
UIViewAnimationOptionBeginFromCurrentState

执行动画后瞬间跳到初始状态继续无限次的执行同一动作
repeat animation indefinitely
UIViewAnimationOptionRepeat

反向执行一个动画,当动画执行完一遍后,沿原路径反向执行一遍.
if repeat, run animation back and forth
UIViewAnimationOptionAutoreverse

忽略继承自父层持续时间,使用自己持续时间(如果存在)
ignore nested duration
UIViewAnimationOptionOverrideInheritedDuration

忽略继承自父层的线性效果,使用自己的线性效果(如果存在)
ignore nested curve
UIViewAnimationOptionOverrideInheritedCurve

允许同一个view的多个动画同时进行
animate contents (applies to transitions only)
UIViewAnimationOptionAllowAnimatedContent

控制两个subview的显示示和隐藏(功能类似于addsubviewremovefromsuperview),使用这个属性(一般在transitionFromView:toView:duration:options:completion:这个方法中使用),FromView会被隐藏toView会被显示
flip to/from hidden state instead of adding/removing
UIViewAnimationOptionShowHideTransitionViews

UIViewAnimationOptionCurveEaseInOut // default
UIViewAnimationOptionCurveEaseIn
UIViewAnimationOptionCurveEaseOut
UIViewAnimationOptionCurveLinear

UIViewAnimationOptionTransitionNone // default
UIViewAnimationOptionTransitionFlipFromLeft // 从左向右扭转翻页
UIViewAnimationOptionTransitionFlipFromRight // 从右向左扭转翻页
UIViewAnimationOptionTransitionCurlUp // 卷曲翻页,从下往上
UIViewAnimationOptionTransitionCurlDown // 卷曲翻页,从上往下
UIViewAnimationOptionTransitionCrossDissolve // 重叠,当一个view从一个位置到另一个位置时,当前的view会由透明状态逐渐到达显示到目的位置,原来的位置将会保留一个影子,并逐渐消失
UIViewAnimationOptionTransitionFlipFromTop // 从上向下扭转翻页
UIViewAnimationOptionTransitionFlipFromBottom // 从下向上扭转翻页

#define kDegreesToRadian(x) (M_PI * (x) / 180.0)
 
#define kRadianToDegrees(radian) (radian*180.0)/(M_PI)
 
- (void)viewDidLoad
{
    [superviewDidLoad];
    self.title = @"测试动画";
    self.view.backgroundColor = [UIColorlightGrayColor];
    
    
    myTest1 = [[UILabelalloc]initWithFrame:CGRectMake(10, 100, 60, 40)];
    myTest1.backgroundColor = [UIColorblueColor];
    myTest1.textAlignment = NSTextAlignmentCenter;
    myTest1.text = @"张明炜";
    myTest1.textColor = [UIColorwhiteColor];
    [self.viewaddSubview: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:[selfrotation:2degree:kRadianToDegrees(90) direction:1repeatCount: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://grayluo.github.io/WeiFocusIo/core%20animation/2015/12/07/coreanimation/

http://www.cnblogs.com/ludashi/p/4160208.html?utm_source=tuicool&utm_medium=referral

各种动画方式demo:https://github.com/lizelu/CATransitionDemo.git

上一篇下一篇

猜你喜欢

热点阅读