UIView 动画

(一)

[UIView beginAnimations:nilcontext:nil];

[UIView setAnimationDuration:0.75];     //设置动画时间

[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp              forView:self.viewcache:YES];                    //动画效果

[UIViewsetAnimationDelegate:self];           //设置代理 

[UIViewsetAnimationDidStopSelector:@selector(addScrollViewAction)];//动画停止后指向一个方法,必须添加了上一句代码,该行才有效果

[UIView commitAnimations];

(二)

[UIViewanimateWithDuration:0.3animations:^{  //老封面渐隐效果

       for(UIView *view in [self.scrollView subviews]) {

          if (view.tag != 200) {

              view.alpha = 0;

           }

       }

    } completion:^(BOOL finished){

       //移除老封面

       for(UIView *view in [self.scrollView subviews]) {

          if(view.tag !=kScrollViewbackgroundViewTag){

              [view removeFromSuperview];

           }

       }

       //添加新封面

       for(int i =0 ; i<categoryCount; i++){

          NSString *categoryId =(NSString*)[categoryIdArrayobjectAtIndex:i];

          CGRect mgzViewRect = CGRectMake(i%10%5*(self.scrollView.frame.size.width/5)+(i/10)*self.scrollView.frame.size.width,i%10/5*(self.scrollView.frame.size.height/2),self.scrollView.frame.size.width/5,self.scrollView.frame.size.height/2);

          MgzsView *mgzsView =[[MgzsView alloc]initWithFrame:mgzViewRect

                                          andCategoryId:categoryId];

          mgzsView.alpha = 0;

          [self.pageArray addObject:mgzsView];

          [self.scrollView addSubview:mgzsView];

           [mgzsViewrelease];

       }

       

       [UIView animateWithDuration:0.3animations:^{  //新封面渐显效果

          for (UIView *view in [self.scrollView subviews]) {

              if (view.tag != kScrollViewbackgroundViewTag) {

                 view.alpha =1;

              }

          

       }];

    }];

areAnimationsEnabled

返回一个布尔值表示动画是否结束。
+ (BOOL)areAnimationsEnabled
返回值
如果动画结束返回YES,否则NO。

beginAnimations:context:

开始一个动画块
+ (void)beginAnimations:(NSString *)animationID context:(void *)context
参数
animationID
动画块内部应用程序标识用来传递给动画代理消息-这个选择器运用setAnimationWillStartSelector:和setAnimationDidStopSelector: 方法来设置。
context
附加的应用程序信息用来传递给动画代理消息-这个选择器使用setAnimationWillStartSelector: 和setAnimationDidStopSelector: 方法。
讨论
这个值改变是因为设置了一些需要在动画块中产生动画的属性。动画块可以被嵌套。如果在没有在动画块中调用那么setAnimation类方法将什么都不做。使用 beginAnimations:context:来开始一个动画块并用commitAnimations类方法来结束一个动画块。
commitAnimations

结束一个动画块并开始当他在动画块外时。
+ (void)commitAnimations
讨论
如果当前的动画块是最外层的动画块,当应用程序返回到循环运行时开始动画块。动画在一个独立的线程中所有应用程序不会中断。使用这个方法,多个动画可以被实现。查看setAnimationBeginsFromCurrentState:来了解如果开始一个动画当另外一个动画在播放的时候。
layerClass

返回类用来创建这一个本类的layer实例对象。
+ (Class)layerClass
返回值
一个用来创建视图layer的类
讨论
重写子类来指定一个自定义类用来显示。当在创建视图layer时候调用。默认的值是CALayer类对象。
setAnimationBeginsFromCurrentState:
设置动画从当前状态开始播放。
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState
参数
fromCurrentState
YES如果动画需要从他们当前状态开始播放。否则为NO。
讨论
如果设置为YES那么当动画在运行过程中,当前视图的位置将会作为新的动画的开始状态。如果设置为NO,当前动画结束前新动画将使用视图最後状态的位置作 为开始状态。这个方法将不会做任何事情如果动画没有运行或者没有在动画块外调用。使用beginAnimations:context:类方法来开始并用 commitAnimations类方法来结束动画块。默认值是NO。
setAnimationCurve:
设置动画块中的动画属性变化的曲线。
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve
讨论
动画曲线是动画运行过程中相对的速度。如果在动画块外调用这个方法将会无效。使用 beginAnimations:context:类方法来开始动画块并用commitAnimations来结束动画块。默认动画曲线的值是UIViewAnimationCurveEaseInOut。

setAnimationDelay:

在动画块中设置动画的延迟属性(以秒为单位)
+ (void)setAnimationDelay:(NSTimeInterval)delay
讨论
这个方法在动画块外调用无效。使用beginAnimations:context: 类方法开始一个动画块并用commitAnimations类方法结束动画块。默认的动画延迟是0.0秒。

setAnimationDelegate:

设置动画消息的代理。
+ (void)setAnimationDelegate:(id)delegate
参数
delegate
你可以用setAnimationWillStartSelector:和setAnimationDidStopSelector: 方法来设置接收代理消息的对象。
讨论
这个方法在动画块外没有任何效果。使用beginAnimations:context:类方法开始一个动画块并用commitAnimations类方法结束一个动画块。默认值是nil

setAnimationDidStopSelector:

设置消息给动画代理当动画停止的时候。
+ (void)setAnimationDidStopSelector:(SEL)selector
参数
selector
当动画结束的时候发送给动画代理。默认值是NULL。这个选择者须有下面方法的签名:animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context。
animationID
一个应用程序提供的标识符。和传给beginAnimations:context: 相同的参数。这个参数可以为空。
finished
如果动画在停止前完成那返回YES;否则就是NO。
context
一个可选的应用程序内容提供者。和beginAnimations:context: 方法相同的参数。可以为空。
讨论
这个方法在动画块外没有任何效果。使用beginAnimations:context: 类方法来开始一个动画块并用commitAnimations类方法结束。默认值是NULL。

setAnimationDuration:

设置动画块中的动画持续时间(用秒)
+ (void)setAnimationDuration:(NSTimeInterval)duration
参数
duration
一段动画持续的时间。
讨论
这个方法在动画块外没有效果。使用beginAnimations:context: 类方法来开始一个动画块并用commitAnimations类方法来结束一个动画块。默认值是0.2。

setAnimationRepeatAutoreverses:
设置动画块中的动画效果是否自动重复播放。
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses
参数
repeatAutoreverses
如果动画自动重复就是YES否则就是NO。
讨论
自动重复是当动画向前播放结束後再重头开始播放。使用setAnimationRepeatCount: 类方法来指定动画自动重播的时间。如果重复数为0或者在动画块外那将没有任何效果。使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations方法来结束一个动画块。默认值是NO。

setAnimationRepeatCount:

设置动画在动画模块中的重复次数
+ (void)setAnimationRepeatCount:(float)repeatCount
参数
repeatCount
动画重复的次数,这个值可以是分数。
讨论
这个属性在动画块外没有任何作用。使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations类方法来结束。默认动画不循环。

setAnimationsEnabled:
设置是否激活动画
+ (void)setAnimationsEnabled:(BOOL)enabled
参数
enabled
如果是YES那就激活动画;否则就是NO
讨论
当动画参数没有被激活那么动画属性的改变将被忽略。默认动画是被激活的。

setAnimationStartDate:
设置在动画块内部动画属性改变的开始时间
+ (void)setAnimationStartDate:(NSDate *)startTime
参数
startTime
一个开始动画的时间
讨论
使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations类方法来结束动画块。默认的开始时间值由CFAbsoluteTimeGetCurrent方法来返回。
setAnimationTransition:forView:cache:
在动画块中为视图设置过渡
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache
参数
transition
把一个过渡效果应用到视图中。可能的值定义在UIViewAnimationTransition中。
view
需要过渡的视图对象。
cache
如果是YES,那么在开始和结束图片视图渲染一次并在动画中创建帧;否则,视图将会在每一帧都渲染。例如缓存,你不需要在视图转变中不停的更新,你只需要等到转换完成再去更新视图。
讨论
如果你想要在转变过程中改变视图的外貌。举个例子,文件从一个视图到另一个视图,然後使用一个UIView子类的容器视图,如下:
1.Begin an animation block.
2.Set the transition on the container view.
3.Remove the subview from the container view.
4.Add the new subview to the container view.
5.Commit the animation block.
1.开始一个动画块。 2.在容器视图中设置转换。 3.在容器视图中移除子视图。 4.在容器视图中添加子视图。 5.结束动画块。

setAnimationWillStartSelector:

当动画开始时发送一条消息到动画代理
+ (void)setAnimationWillStartSelector:(SEL)selector
参数
selector
在动画开始前向动画代理发送消息。默认值是NULL。这个selector必须由和beginAnimations:context: 方法相同的参数,一个任选的程序标识和内容。这些参数都可以是nil。
讨论
这个方法在动画块外没有任何作用。使用beginAnimations:context:类方法来开始一个动画块并用commitAnimations类方法来结束。

消息一览

+ (void)beginAnimations:(NSString *)animationID context:(void *)context;//开始动画
+ (void)commitAnimations;//提交动画
+ (void)setAnimationDelegate:(id)delegate;//动画代理
+ (void)setAnimationWillStartSelector:(SEL)selector;//动画开始前执行
+ (void)setAnimationDidStopSelector:(SEL)selector;//动画停止时执行
+ (void)setAnimationDuration:(NSTimeInterval)duration;//执行时间
+ (void)setAnimationDelay:(NSTimeInterval)delay;//延时
+ (void)setAnimationStartDate:(NSDate *)startDate;//动画开始Date
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;//动画曲线
+ (void)setAnimationRepeatCount:(float)repeatCount;//重复次数
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;//重复时自动反转
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;//从当前状态开始
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;//转换方式
+ (void)setAnimationsEnabled:(BOOL)enabled;//设置动画是否有效
+ (BOOL)areAnimationsEnabled;//动画是否有效

Q:(void)beginAnimations:(NSString *)animationID context:(void *)context 方法中 animationID 与 context参数的作用?
A:用于动画块标识、动画事务等分辨。这里是用于动画的委托处理时,如果设置了委托
+ (void)setAnimationDelegate:(id)delegate;
但未设置
+ (void)setAnimationWillStartSelector:(SEL)selector;或
+ (void)setAnimationDidStopSelector:(SEL)selector;
时,系统会默认调用
-(void) animationWillStart:(NSString *)animationID context:(void *)context 或
-(void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
这两个方法(如果已实现),在这两个方法中可以用作分辨动画。

UIView 动画示例

//第一种动画方式

- (void) first_animations

{
    [UIView beginAnimations:nil context:nil];  //启动动画动作
    [UIView setAnimationRepeatCount:1];//设置是否重复播放
    [UIView setAnimationDuration:1];//设置动画持续时间
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; //动画曲线,具体的应用 ,可以经过实验检测
    [UIView setAnimationDelegate:self];//动画块的某个方法(最下方),委托到本类的实例
    [UIView setAnimationDidStopSelector:@selector(resetView)];//动画结束后去执行的方法
    
    CGAffineTransform oneTransform = CGAffineTransformRotate(self.animatView.transform, degreesToRadian(180));//进行  CGAffineTransform 方式的动作(旋转拉伸等等)===>(对于CGAffineTransform  可以另外开辟一个关于CGAffineTransform使用详情的文章进行专门介绍 )

 

    CGAffineTransform twoTransform = CGAffineTransformTranslate(self.animatView.transform,0,-100);
    CGAffineTransform newTransform = CGAffineTransformConcat(oneTransform, twoTransform);
    [self.animatView setTransform:newTransform];
    [UIView commitAnimations];//有些网友说,这是动画结束.起始经过本人尝试试验.其实应该是,执行上方定义的动画块内容.
}

//第二中动画定义方式
- (void) second_animations
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [animation setDuration:1];
    [animation setRepeatCount:0];
    [animation setAutoreverses:YES];//自动反向动画
    [animation setFromValue:[NSNumber numberWithFloat:1.0]];
    [animation setToValue:[NSNumber numberWithFloat:0]];
    [animation setDelegate:self];
    [self.animatView.layer addAnimation:animation forKey:@"firstView-Opacity"];
}
- (void) third_animations
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.containView cache:YES];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
    [UIView commitAnimations];
}
- (void) fourth_animations
{
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;          /*  间隔时间*/
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  /* 动画的开始与结束的快慢*/
    transition.type = @"pageCurl";  //@"cube" @"moveIn" @"reveal" @"fade"(default) @"pageCurl" @"pageUnCurl" @"suckEffect" @"rippleEffect" @"oglFlip"
    transition.subtype = kCATransitionFromRight;
    transition.removedOnCompletion = YES;
    transition.fillMode = kCAFillModeBackwards;
    transition.delegate = self;
    [self.animatView.layer addAnimation:transition forKey:nil];  
}
-(void) resetView
{
    [self.animatView setTransform:CGAffineTransformRotate(self.animatView.transform, degreesToRadian(180))];
    self.animatView.frame = CGRectMake(0, 0, 280, 200);
}
#pragma mark Delegate Methods
- (void)animationDidStop:(CAAnimation *) theAnimation finished:(BOOL) flag {
    self.animatView.frame = CGRectMake(0, 0, 280, 200);
}

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值