zoukankan      html  css  js  c++  java
  • 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

    效果图如下

    要求基本实现

  • 相关阅读:
    在 Anaconda下解决国内安装tensorflow等下载慢和中断,出错,异常问题的一点思路
    关于指针和结构体的一点教训
    ARM cortexM4中断优先级的一点理解。
    ubuntu16下的/etc/resolv.conf重置的解决方案
    linux安装dpkg安装缺少依赖项的解决
    莲藕的简单凉菜制作总结
    单片机一种简便的printf调试方案。
    usart下位机输出使用printf的格式化技巧
    关于xp操作系统下使用VC6++编写的上位机软件在win10中运行的问题
    百度面试两板斧:手写算法问基础
  • 原文地址:https://www.cnblogs.com/hecheng0314/p/4412837.html
Copyright © 2011-2022 走看看