2015-12-16-UI8_Block-----block 快语法(闭包)
//
// AppDelegate.h
// UI8_Block
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//
import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
// UI8_Block
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//
import "AppDelegate.h"
import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];RootViewController *rotVC = [[RootViewController alloc]init];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:rotVC];
self.window.rootViewController = navi;[rotVC release];
[navi release];/ Block 快语法*/
[_window release];
return YES;
} - (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
//
// RootViewController.h
// UI8_Block
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//
import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
//
// RootViewController.m
// UI8_Block
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//
import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];/block 快语法(闭包)/
// 类似: 函数指针
// 当做: 变量使用
// 使用: 函数回调
// 原理: 匿名函数// 1.无参无返回
// void func(void)
// void (*p)(void)
// void(^)(void) block类型
// block1 block名
// ^void(void){函数体} block值
void (^block1)(void) = ^void(void) {NSLog(@"block1");
};
//调用
block1();// 1.有返回有参
NSMutableArray (^block2)(NSString ) = ^(NSString *str) {NSMutableArray *arr = [NSMutableArray array]; [arr addObject:str]; return arr;
};
//调用
NSLog(@"%@", block2(@"suibian"));// button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
[button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];}
(void)button:(UIButton *)btn
{
NSLog(@"sss");// UIview动画
// 参数1: 动画时间
// 参数2: 执行的动画
/*
[UIView animateWithDuration:2 animations:^{// btn.backgroundColor = [UIColor greenColor];
}];
*/
// 参数3:动画完成后执行的block
[UIView animateWithDuration:2 animations:^{btn.center = CGPointMake(200, 200);
} completion:^(BOOL finished) {
[UIView animateWithDuration:2 animations:^{ btn.frame = CGRectMake(100, 100, 300, 300); // btn.alpha = 0; [UIView animateWithDuration:3 animations:^{ btn.frame = CGRectMake(0, 400, 50, 50); // btn.alpha = 0; [UIView animateWithDuration:5 animations:^{ btn.center = CGPointMake(300, 300); } completion:^(BOOL finished) { btn.frame = CGRectMake(0, 500, 300, 300); btn.backgroundColor = [UIColor yellowColor]; }]; }]; }];
}];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.
}
/*
pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.
}
*/
@end
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »