一个会闪动的炫酷名片

建一个iOS>Application>Single View Application工程

代码如下:(ps:代码中彩色的是自己手动添加的代码,黑色是工程中本来就有的。)

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic,assign) NSInteger R;
@property (nonatomic,assign) NSInteger G;
@property (nonatomic,assign) NSInteger B;
@property (nonatomic,retain) UIButton *btn;
@property (nonatomic,assign) BOOL      stop;

@end

AppDelegate.m


#import "AppDelegate.h"

@interface AppDelegate ()
//创建用来记录点击
@property (nonatomic) BOOL selected;
//创建用来记录随机数
@property (nonatomic) int times;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
        self.window=[[UIWindow alloc]init];
        self.window.frame=[UIScreen mainScreen].bounds;
        self.window.backgroundColor=[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1];//白色全1 黑色全0
        [self.window makeKeyAndVisible];

        
        UIViewController *VC=[[UIViewController alloc]init];
        UINavigationController *naVC=[[UINavigationController alloc]initWithRootViewController:VC];
        [VC setTitle:@"名片"];
        self.window.rootViewController=naVC;

    
        UILabel *labName=[[UILabel alloc]initWithFrame:CGRectMake(140, 430, 150, 20)];
        labName.text=@"姓名:王颜华";
        labName.backgroundColor=[UIColor whiteColor];
        [self.window addSubview:labName];
    
        UILabel *labGender=[[UILabel alloc]initWithFrame:CGRectMake(155, 450, 100, 20)];
        labGender.text=@"性别:女";
        [self.window addSubview:labGender];
    
        UILabel *labAge=[[UILabel alloc]initWithFrame:CGRectMake(155, 470, 100, 20)];
        labAge.text=@"年龄:21";
        [self.window addSubview:labAge];
    
        UILabel *labQQ=[[UILabel alloc]initWithFrame:CGRectMake(140, 490, 120, 20)];
        labQQ.text=@"QQ:453132184";
        [self.window addSubview:labQQ];

    
        self.btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _btn.frame = CGRectMake(150, 550, 100, 40);
        [_btn setTitle:@"点我~" forState:(UIControlStateNormal)];
        [_btn addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.window addSubview:_btn];
    
        self.stop = NO;

    


     return YES;
}

-(IBAction)button:(id)sender{

    UIButton *btn=(UIButton *)sender;
    //bool类型的点击事件
    self.selected=!self.selected;
    //如果现在是点击了button,进入ifelse语句
    if (self.selected) {
        //如果点击了button,改名
        self.stop = NO;
        [btn setTitle:@"暂停" forState:(UIControlStateNormal)];
    }
    else{
        //再次点击,改名
        self.stop = YES;
        [btn setTitle:@"点我~" forState:(UIControlStateNormal)];
    }
    
    //创建事件类型nstimer,当点击后触发nstimer类型
    //scheduledtimerwithtimeinterview是时间
    //target由谁携带触发事件
    //selector 触发事件的名字
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changeColor:) userInfo:nil repeats:YES];

}


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

   
    UIView *yellowView=[[UIView alloc]initWithFrame:CGRectMake(220, 220, 50, 50)];
    yellowView.backgroundColor=[UIColor colorWithRed:(arc4random()%254+1)/255.0 green:(arc4random()%254+1)/255.0 blue:(arc4random()%254+1)/255.0 alpha:1];
    
    [self.window addSubview:yellowView];
    
    UIView *blueView=[[UIView alloc]initWithFrame:CGRectMake(70, 170, 50, 50)];
    blueView.backgroundColor=[UIColor colorWithRed:(arc4random()%254+1)/255.0 green:(arc4random()%254+1)/255.0 blue:(arc4random()%254+1)/255.0 alpha:1];
    [self.window addSubview:blueView];
    
    UIView *blackView=[[UIView alloc]initWithFrame:CGRectMake(170, 170, 50, 50)];
    blackView.backgroundColor=[UIColor colorWithRed:(arc4random()%254+1)/255.0 green:(arc4random()%254+1)/255.0 blue:(arc4random()%254+1)/255.0 alpha:1];
    [self.window addSubview:blackView];
    
    UIView *orangeView=[[UIView alloc]initWithFrame:CGRectMake(220, 120, 50, 50)];
    orangeView.backgroundColor=[UIColor colorWithRed:(arc4random()%254+1)/255.0 green:(arc4random()%254+1)/255.0 blue:(arc4random()%254+1)/255.0 alpha:1];
    [self.window addSubview:orangeView];
    
    UIView *greenView=[[UIView alloc]initWithFrame:CGRectMake(270, 170, 50, 50)];
    greenView.backgroundColor =[UIColor colorWithRed:(arc4random()%254+1)/255.0 green:(arc4random()%254+1)/255.0 blue:(arc4random()%254+1)/255.0 alpha:1];
    [self.window addSubview:greenView];
    
    UIView *grayView=[[UIView alloc]initWithFrame:CGRectMake(120, 220, 50, 50)];
    grayView.backgroundColor=[UIColor colorWithRed:(arc4random()%254+1)/255.0 green:(arc4random()%254+1)/255.0 blue:(arc4random()%254+1)/255.0 alpha:1];
    [self.window addSubview:grayView];
    
    UIView *purpleView=[[UIView alloc]initWithFrame:CGRectMake(120,120,50,50)];
    purpleView.backgroundColor=[UIColor colorWithRed:(arc4random()%254+1)/255.0 green:(arc4random()%254+1)/255.0 blue:(arc4random()%254+1)/255.0 alpha:1];
    [self.window addSubview:purpleView];
    
    
    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(170, 270, 50, 50)];
    redView.backgroundColor=[UIColor redColor];
    [self.window addSubview:redView];

    
    if (_stop) {
        [timer invalidate];
    }

}


- (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


这个小程序是运行在iPhone6模拟机上的 图中的色块是不断的变化的 类似跑马灯

但是因为屏幕大小的问题,其他机型未测试,可能会乱掉···

另外有一个bug 就是内存不断在增加,未来可能会崩掉···

一个会闪动的炫酷名片_第1张图片

你可能感兴趣的:(UI小程序)