IOS中动画的实现:以及视图的移动、缩放和旋转

来源:互联网 发布:html5 css3游戏源码 编辑:程序博客网 时间:2024/04/30 02:25

原文:http://blog.csdn.net/pop_xiaohao/article/details/7898714


一、动画和移动视图

IOS上实现动画的方式有多种,我们可以获得的最高层次的动画能力是通过UIKit,UIKit中包括一些Core Animation的较低层次的功能,并且包装成非常简洁的API供我们使用。

     UIKit中实现动画的起点是调用UIView类中的类方法beginAnimations:context:。第一个参数是一个可选动画的名称,第二个参数是一个可选的上下文,在之后传递给动画的委托方法时你可以获取它。在你通过beginAnimations:context:开始一个动画后,它实际上不会马上执行而是知道你调用UIView类的commintAnimations类方法。在begainAnimations:context:和commitAnimations之间,你对一个试图对象的计算(如移动)将在commitAnimations被调用之后开始进行动画。

UIKit执行动画时的重要方法,UIView类的类方法:

beginAnimationss:context: 开始一个动画块。调用这个类方法之后,你对试图任何动画属性的改变,都会在动画块提交之后形成动画。

setAnimationDuration:以秒为单位设置动画的持续时间。

setAnimationDelegate:设置一个对象来接受动画开始之前、动画期间或动画结束之后发生的各种事情的委托对象。设置一个委托对象不会立即开始发送动画委托消息。你必须对视图对象使用其他的setter方法来告诉UIKit你的委托对象中的那个选择器(selector)要接收哪些委托消息。

setAnimationDidstopSelector:设置动画完成时委托对象中应该被调用的方法。

setAnimationWillStartSelector:设置动画即将开始时委托对象中被调用选择器。

setAnimationDelay:设置一个动画启动的延迟时间。以秒为单位

setAnimationRepeatCount:设置一个动画块需要重复它的动画的次数

- (void) startTopLeftImageViewAnimation{ 
 
    /* Start from top left corner */  
    [self.xcodeImageView1 setFrame:CGRectMake(0.0f, 
    [self.xcodeImageView1 setAlpha:1.0f]; 
    [UIView beginAnimations:@"xcodeImageView1Animation" 
        context:xcodeImageView1]; 
 
    /* 3 seconds animation */ 
    [UIView setAnimationDuration:3.0f]; 
 
    /* Receive animation delegates */  
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector: 
        @selector(imageViewDidStop:finished:context:)]; 
 
    /* End at the bottom right corner */  
     [self.xcodeImageView1 setFrame:CGRectMake(220.0f, 
     [self.xcodeImageView1 setAlpha:0.0f]; [UIView commitAnimations];

- (void)imageViewDidStop:(NSString *) 
    paramAnimationID finished:(NSNumber *)paramFinished 
    context:(void *)paramContext{ 
 
    UIImageView *contextImageView = (UIImageView *)paramContext; 
    [contextImageView removeFromSuperview]; 

动画和缩放视图:

- (void) viewDidAppear:(BOOL)paramAnimated{ 
 
    [super viewDidAppear:paramAnimated]; 
 
/* Place the image view at the center  
    of the view of this view controller */ 
    self.xcodeImageView.center = self.view.center; 
 
/* Make sure no translation is applied to this image view */  
self.xcodeImageView.transform = CGAffineTransformIdentity; 
 
    /* Begin the animation */ 
    [UIView beginAnimations:nil context:NULL]; 
 
    /* Make the animation 5 seconds long */  
    [UIView setAnimationDuration:5.0f]; 
 
    /* Make the image view twice as large in width and height */  
    self.xcodeImageView.transform = 
    CGAffineTransformMakeScale(2.0f, 2.0f); 
 
    /* Commit the animation */  
    [UIView commitAnimations]; 


动画和旋转