UIView动画设置

创建一个红绿灯,红、绿每5秒变化一次颜色,要求变亮的灯(色块)面积也要同时变大。黄灯闪亮3秒钟(0.9秒亮黄,0.1秒亮黑),点击按钮开始执行。
//AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
//AppDelegate.m

//  AppDelegate.m
//  traffic light
//
//  Created by apple on 15/4/9.
//  Copyright (c) 2015年 hecheng. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()
{
    UIView *_redView;
    UIView *_greenView;
    UIView *_yellowView;
    CGFloat x;
    CGFloat y;

}

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //计算屏幕尺寸
    UIScreen *screens=[UIScreen mainScreen];
    CGRect rect=screens.bounds;

    //计算屏幕中心位置
    x=rect.size.width/2;
    y=rect.size.height/2;
    self.window=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] ;
    self.window.backgroundColor=[UIColor whiteColor];
    [self.window makeKeyAndVisible];

    //设置红灯,初始化颜色为黑色
    _redView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    _redView.backgroundColor=[UIColor blackColor];
    [self.window addSubview:_redView];
    _redView.center=CGPointMake(x-100, y);

    //设置绿灯,初始化颜色为黑色
    _greenView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    _greenView.backgroundColor=[UIColor blackColor];
    [self.window addSubview:_greenView];
    _greenView.center=CGPointMake(x, y);

    //设置黄灯,初始化颜色为黑色
    _yellowView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    _yellowView.backgroundColor=[UIColor blackColor];
    [self.window addSubview:_yellowView];
    _yellowView.center=CGPointMake(x+100, y);

    //设置按钮
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"启动" forState:UIControlStateNormal];
    btn.frame=CGRectMake(0, 0, 50, 50);
    btn.center=CGPointMake(x, y-80);
    [btn addTarget:self action:@selector(didClicked) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor=[UIColor blackColor];
    [self.window addSubview:btn];

    return YES;

}

- (void)animationWillStart:(NSString *)animationID context:(void *)context {
    NSLog(@"%@", animationID);
    NSLog(@"%s", __func__);
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if ([animationID isEqualToString:@"red"]) {
        [UIView beginAnimations:@"green" context:NULL];
        [UIView setAnimationDelay:5];
        _redView.bounds=CGRectMake(0, 0, 50, 50);
        _greenView.bounds=CGRectMake(0, 0, 100, 100);
        _greenView.backgroundColor=[UIColor greenColor];
        _redView.backgroundColor=[UIColor blackColor];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        [UIView commitAnimations];

    }
    else if ([animationID isEqualToString:@"green"]) {
        [UIView beginAnimations:@"yellow" context:NULL];
        [UIView setAnimationDelay:5];
        _greenView.bounds=CGRectMake(0, 0, 50, 50);
        _yellowView.bounds=CGRectMake(0, 0, 100, 100);
        _greenView.backgroundColor=[UIColor blackColor];
        _yellowView.backgroundColor=[UIColor yellowColor];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        [UIView commitAnimations];

    }
    else if ([animationID isEqualToString:@"yellow"]) {
        [UIView beginAnimations:@"black1" context:NULL];
        [UIView setAnimationDelay:0.9];

        _yellowView.backgroundColor=[UIColor blackColor];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        [UIView commitAnimations];
    }
    else if ([animationID isEqualToString:@"black1"]) {
        [UIView beginAnimations:@"yellow1" context:NULL];
        [UIView setAnimationDelay:0.1];

        _yellowView.backgroundColor=[UIColor yellowColor];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        [UIView commitAnimations];
    }
    else if ([animationID isEqualToString:@"yellow1"]) {
        [UIView beginAnimations:@"black2" context:NULL];
        [UIView setAnimationDelay:0.9];
        _yellowView.backgroundColor=[UIColor blackColor];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        [UIView commitAnimations];
    }
    else if ([animationID isEqualToString:@"black2"]) {
        [UIView beginAnimations:@"yellow2" context:NULL];
        [UIView setAnimationDelay:0.1];

        _yellowView.backgroundColor=[UIColor yellowColor];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        [UIView commitAnimations];
    }
    else if ([animationID isEqualToString:@"yellow2"]) {
        [UIView beginAnimations:@"black3" context:NULL];
        [UIView setAnimationDelay:0.9];
        _yellowView.backgroundColor=[UIColor blackColor];
//        _redView.backgroundColor=[UIColor redColor];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        [UIView commitAnimations];
    }
    else if ([animationID isEqualToString:@"black3"]) {
        [UIView beginAnimations:@"red" context:NULL];
        [UIView setAnimationDelay:0.1];
//        _yellowView.backgroundColor=[UIColor blackColor];
        _yellowView.bounds=CGRectMake(0, 0, 50, 50);
        _redView.bounds=CGRectMake(0, 0, 100, 100);
        _redView.backgroundColor=[UIColor redColor];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        [UIView commitAnimations];
    }

}

//按钮响应方法
- (void)didClicked{
    [UIView beginAnimations:@"red" context:NULL];
//    [UIView setAnimationDuration:<#(NSTimeInterval)#>]
    _redView.backgroundColor=[UIColor redColor];
    _redView.bounds=CGRectMake(0, 0, 100, 100);
//    _redView.center=CGPointMake(x, y+100);
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];

}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

效果图如下

要求基本实现

时间: 2024-12-19 15:02:52

UIView动画设置的相关文章

UIView动画设置参数

在动画方法中有一个option参数,UIViewAnimationOptions类型,它是一个枚举类型,动画参数分为三类,可以组合使用: 1.常规动画属性设置(可以同时选择多个进行设置) UIViewAnimationOptionLayoutSubviews:动画过程中保证子视图跟随运动. UIViewAnimationOptionAllowUserInteraction:动画过程中允许用户交互. UIViewAnimationOptionBeginFromCurrentState:所有视图从当

iOS动画:UIView动画和CALayer动画(CABasicAnimation、CAKeyframeAnimation的使用)

iOS中的动画有两种实现方式,一种是UIView来实现动画,另一种动画是通过CALayer来实现,下面介绍两种动画的简单实现: 一.UIView动画的实现 UIView使用Context来实现动画 关键代码: //参数1 动画名称 参数2 要实现动画的对象上下文          [UIView beginAnimations:@"attribute" context:_showImageView];          //设置动画的时间     [UIView setAnimatio

UIView中的动画设置

两种状态的改变通过动画来渐变,设置动画效果,一般采用的方式有以下几种: 方式一: [UIView beginAnimations:(NSString *) context:<#(void *)#>]; [UIView setAnimationDuration:<#(NSTimeInterval)#>]; /*****这里插入需要产生动画的片段*****/ [UIViewcommitAnimations]; 其中NSString和void这两个参数可设为nil:NSTimeInter

UIView动画效果

做出UI界面,实现程序功能,是重中之重,但是通过动画提升使用体验,一般人应该不会拒绝吧. 那么问题又来了,怎么做? 一: 稳扎稳打: 一步一步来吧,毕竟,心急吃不了热豆腐. 1.开启一个动画 2,设置该动画的各种属性:动画时长.延时执行.自动返回.动画重复次数.转场动画... 3,设置动画的UI的结束时的状态是什么,UI的最终位置等. 4,提交动画. 大功告成.具体细节如下: //===---开始动画 ---=== [UIView beginAnimations:nil context:nil]

UIView动画

1.淡入效果 // MARK: - UIView动画-淡入 @IBAction func simpleAnimationFadeIn() { UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(2.0)//设置动画时间 testImageView.alpha = 0.0 UIView.commitAnimations() // //通过闭包实现 UIView淡入 // UIView.animateWithDu

GIF动画,菊花动画,UIView动画,CoreAnimation动画(CALayer动画)的用法

1.GIF动画 1 // 创建一个显示图片的imageView // viewController创建 2 UIImageView *showGifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 414, 736)]; 3 [self.view addSubview:showGifImageView]; 4 5 6 //创建一个存储图片的数组 7 NSMutableArray *saveImageViewArray

iOS开发——动画编程OC篇&amp;(六)UIView动画

UIView动画 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持 执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之间 常见方法解析: + (void)setAnimationDelegate:(id)d

iOS动画1 — UIView动画

iOS动画1 — UIView动画 iOS动画基础是Core Animation核心动画.Core Animation是iOS平台上负责图形渲染与动画的基础设施.由于核心动画的实现比较复杂,苹果提供了实现简单动画的接口—UIView动画.UIView动画封装在UIView的Category中,主要实现一些简单和常用的动画.UIView动画是对核心动画进行了一层封装,所以最终动画还是通过Core Animation的接口实现. 主要的动画效果都可以通过UIView动画和Core Animation

ios之UIview动画

一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持 执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之间 常见方法解析: + (void)setAnimationDelegate:(id)delegate