几个简单动画代码

1. UIView 

animateWithDuration:animations:completion:

Animate changes to one or more views using the specified duration and completion handler.

+ (void)animateWithDuration:(NSTimeInterval) duration animations:(void (^)(void)) animations completion:(void (^)(BOOL finished)) completion
Parameters
duration

The total duration of the animations, measured in seconds. If you specify a negative value or 0, the changes are made without animating them.

animations

A block object containing the changes to commit to the views. This is where you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL.

completion

A block object to be executed when the animation sequence ends. This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called. If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle. This parameter may be NULL.

Discussion

This method performs the specified animations immediately using the UIViewAnimationOptionCurveEaseInOut andUIViewAnimationOptionTransitionNone animation options.

For example, if you want to fade a view until it is totally transparent and then remove it from your view hierarchy, you could use code similar to the following:

[UIView animateWithDuration:0.2
     animations:^{view.alpha = 0.0;}
     completion:^(BOOL finished){ [view removeFromSuperview]; }];

During an animation, user interactions are temporarily disabled for the views being animated. (Prior to iOS 5, user interactions are disabled for the entire application.)

Availability
  • Available in iOS 4.0 and later.
Declared In
UIView.h

eg:

- (void)ShowGameButton {

    

    singleGameBt.alpha = 0;

    [UIView animateWithDuration:0.1 animations:^{ singleGameBt.alpha = 1; } completion:^(BOOL finished){ }];

    

    doubleGameBt.alpha = 0;

    [UIView animateWithDuration:0.5 animations:^{ doubleGameBt.alpha = 1; } completion:^(BOOL finished){ }];

    

    connectedGameBt.alpha = 0;

    [UIView animateWithDuration:0.9 animations:^{ connectedGameBt.alpha = 1; } completion:^(BOOL finished){ }];

    

    gameSettingBt.alpha = 0;

    [UIView animateWithDuration:1.3 animations:^{ gameSettingBt.alpha = 1; } completion:^(BOOL finished){ }];

    

    aboutBt.alpha = 0;

    [UIView animateWithDuration:1.7 animations:^{ aboutBt.alpha = 1; } completion:^(BOOL finished){ }];

    

    moreGameBt.alpha = 0;

    [UIView animateWithDuration:2.1 animations:^{ moreGameBt.alpha = 1; } completion:^(BOOL finished){ }];

    

}




2. UIImageView 

animationImages

An array of UIImage objects to use for an animation.

@property(nonatomic, copy) NSArray *animationImages
Discussion

The array must contain UIImage objects. You may use the same image object more than once in the array. Setting this property to a value other than nil hides the image represented by the image property. The value of this property is nil by default.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIImageView.h

animationDuration

The amount of time it takes to go through one cycle of the images.

@property(nonatomic) NSTimeInterval animationDuration
Discussion

The time duration is measured in seconds. The default value of this property is equal to the number of images multiplied by 1/30th of a second. Thus, if you had 30 images, the value would be 1 second.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIImageView.h

animationRepeatCount

Specifies the number of times to repeat the animation.

@property(nonatomic) NSInteger animationRepeatCount
Discussion

The default value is 0, which specifies to repeat the animation indefinitely.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIImageView.h


eg:

UIImageView* campFireView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];

campFireView.animationImages = [NSArray arrayWithObjects:

                                    [UIImage imageNamed:@"logo1.png"],

                                    [UIImage imageNamed:@"logo2.png"],

                                    [UIImage imageNamed:@"logo3.png"],

                                    [UIImage imageNamed:@"logo4.png"],

                                    [UIImage imageNamed:@"logo5.png"],

                                    [UIImage imageNamed:@"logo5.png"],

                                    [UIImage imageNamed:@"logo5.png"],

                                    [UIImage imageNamed:@"logo5.png"],

                                    [UIImage imageNamed:@"logo4.png"],

                                    [UIImage imageNamed:@"logo3.png"],

                                    [UIImage imageNamed:@"logo2.png"],

                                    [UIImage imageNamed:@"logo1.png"], nil];

campFireView.animationDuration = 2.8;

campFireView.animationRepeatCount = 1;  

[campFireView startAnimating];

//[self.view insertSubview:campFireView atIndex:3]; 

//[self.view bringSubviewToFront:campFireView];

[self.view addSubview:campFireView];





3. CALayer 

addSublayer:

Appends the layer to the receiver’s sublayers array.

- (void)addSublayer:(CALayer *) aLayer
Parameters
aLayer

The layer to be added to the receiver’s sublayers array.

Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
CALayer.h



eg:

// 界面加云层动画

cloudImgview = [[UIImageView allocinitWithImage:[UIImage imageNamed:@"clound.png"]];

cloudPoint = CGPointMake(-1.0, -1.0);  

cloudImgview.layer.position = CGPointMake(0,108);  // start position-->(cloudImgview)

[self.view.layer addSublayer: cloudImgview.layer];

[NSTimer scheduledTimerWithTimeInterval: 0.08

                                 target: self

                               selector: @selector(handleTimer:)

                               userInfo: nil

                                repeats: YES];



//control the clound move 0.08s

- (void) handleTimer:(NSTimer *)timer {


    CGSize size;

    CGPoint origin;

    size = [cloudImgview image].size


    // 设置cloudPoint.x == 1

    ifcloudImgview.layer.position.x <= ((size.width / 2) + 1) - self.view.frame.origin.)

        cloudPoint.x = 1.0;

    // 设置origin: x递增, y不变(由image1赋值)

    if(cloudImgview.layer.position.x + (size.width / 2) + 1 >= 1520

    {

origin.x = 0;

origin.y = cloudImgview.layer.position.y;

    }else {

origin = cloudImgview.layer.position;

    }

    origin.x += cloudPoint.x;


    cloudImgview.layer.position = origin;  // 动态改变cloudImgview的坐标(x

    /* notify the system that your view’s contents need to be redrawn */

    //[ self.view setNeedsDisplayInRect: image1.layer.frame ];  


}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值