2015.3.17
//button除了alloc init方法创建以外,系统封装了类方法,在以前非常实用,不过在ios7中这种方法的实用性有些下降了。(因为取消了圆角风格)
+ (id)buttonWithType:(UIButtonType)buttonType;
typedef NS_ENUM(NSInteger, UIButtonType) {
//默认,如果只设置了普通背景图,没有设置高亮背景图,点击时会将普通背景图变灰
UIButtonTypeCustom = 0,
//点击时会将背景图半透明,如果button只设置了普通状态下的文字,那么文字会有阴影效果
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),
UIButtonTypeRoundedRect = UIButtonTypeSystem,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
//这三种在ios7以后都差不多,是一个信息标记
UIButtonTypeContactAdd,
//加号标记
//2种标记都预设了图片和size大小22*22,不需要在自己设置大小和图片
};
//设置不同状态下的字体,字体颜色,背景图片
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
//状态最常用下面3个
UIControlStateNormal = 0, //默认
UIControlStateHighlighted = 1 << 0, //被点击时,(当按住按钮不放时就是这个状态)
UIControlStateSelected = 1 << 2, //选中(这个状态是得手动设置为选中或非选中)
//注:工作中高亮和选中这2个状态一般不会同时出现,如果同时设置可能引起冲突
//给按钮添加一个点击事件
//第一个参数名是目标对象(也就是给谁发消息),第二个参数是传一个方法名,(注意要这目标对象里有这个方法,不然就挂了)
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
//最后一个参数一般用UIControlEventTouchUpInside,点击松开时触发
//和上一个对应,删除一个点击事件
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
tag值判断
UIControlStateUIControlEvents
//创建定时器,每隔几秒就运行某个函数一次
NSTimer *_timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(run) userInfo:nil repeats:YES];
//取消定时器
[_timer invalidate];
------------------------------------------------------------------------------------------------------------------------------------------
#import "AppDelegate.h"
#import "RootViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NSLog(@"1111");
//创建一个视图控制器(页面)
RootViewController *rvc = [[RootViewController alloc] init];
NSLog(@"2222");
//将页面和window关联(设为程序的首页)
self.window.rootViewController = rvc;
NSLog(@"333");
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
NSLog(@"4444");
return YES;
}
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(@"初始化函数,这个方法不需要直接调用");
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//专门用来写UI的,所有的UI的添加都写在这个方法里
NSLog(@"%s",__func__);
//每一个视图控制器(页面)都自带一个view,全屏的,默认的颜色的透明
self.view.backgroundColor = [UIColor yellowColor];
//使用类方法创建一个btn,可以通过这种方式设置btn的类型
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(40, 80, 100, 40);
btn.backgroundColor = [UIColor redColor];
//设置普通状态下的文字、文字颜色、背景图片
[btn setTitle:@"点我" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"a"] forState:UIControlStateNormal];
//高亮状态下的文字、文字颜色、背景图片
[btn setTitle:@"点住了" forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[btn setBackgroundImage:[UIImage imageNamed:@"b"] forState:UIControlStateHighlighted];
//给btn添加一个点击事件
//让btn开始监听TouchUpInside这个事件,一旦接收到这个事件,就会让self调用-btnClick 这个方法。
//OC里的事件-消息机制
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//点击事件对应的消息(一般情况下直接把它叫成点击事件)
- (void)btnClick
{
NSLog(@"一一一一一一一");
}
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一分割一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
#import "AppDelegate.h"
#import "RootViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
RootViewController *rvc = [[RootViewController alloc] init];
self.window.rootViewController = rvc;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
_label = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 280, 60)];
_label.backgroundColor = [UIColor grayColor];
_label.textAlignment = NSTextAlignmentCenter;
_label.text = @"哥不是随便的人";
_label.font = [UIFont systemFontOfSize:30];
[self.view addSubview:_label];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
btn1.frame = CGRectMake(30, 160, 260, 40);
btn1.backgroundColor = [UIColor grayColor];
[btn1 setTitle:@"点我" forState:UIControlStateNormal];
//给一个view添加标记,默认是0,所以设置的时候要避免使用0
btn1.tag = 1;
[btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
UIButton *aBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 230, 260, 40)];
//使用alloc init创建的btn,类型是custom。
aBtn.backgroundColor = [UIColor grayColor];
aBtn.tag = 2;
[aBtn setTitle:@"点我" forState:UIControlStateNormal];
[aBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aBtn];
}
- (void)btnClick:(UIButton *)sender
{
NSLog(@"tag = %d",sender.tag);
if (sender.tag == 1) {
_label.text = @"点了第1个";
} else {
_label.text = @"2";
}
sender.hidden = YES;
//延迟调用,self在0.5秒以后执行runLater:,并且将sender作为参数
[self performSelector:@selector(runLater:) withObject:sender afterDelay:0.5];
}
- (void)runLater:(UIButton *)btn
{
btn.hidden = NO;
}
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一分割一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
#import "AppDelegate.h"
#import "HomeViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HomeViewController *hvc = [[HomeViewController alloc] init];
self.window.rootViewController = hvc;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
UILabel *resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 300)];
resultLabel.backgroundColor = [UIColor grayColor];
resultLabel.font = [UIFont systemFontOfSize:100];
resultLabel.textAlignment = NSTextAlignmentCenter;
resultLabel.text = @"随机";
[self.view addSubview:resultLabel];
resultLabel.tag = 1;
//设置圆角
resultLabel.layer.masksToBounds = YES;
resultLabel.layer.cornerRadius = 150;
//设置边框
resultLabel.layer.borderColor = [[UIColor blackColor] CGColor];
resultLabel.layer.borderWidth = 5;
//获取屏幕高度
CGFloat height_for_screen = [UIScreen mainScreen].bounds.size.height;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(130, height_for_screen-40-30, 60, 40);
btn.backgroundColor = [UIColor grayColor];
//设置btn字体大小
btn.titleLabel.font = [UIFont systemFontOfSize:10];
[btn setTitle:@"start" forState:UIControlStateNormal];
[btn setTitle:@"stop" forState:UIControlStateSelected];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnClick:(UIButton *)sender
{
sender.selected = !sender.selected;
if (sender.selected) {
//启动一个时间器,让self每隔0.05秒调用一次timeRun
_timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
} else {
//停止时间器
[_timer invalidate];
}
}
- (void)timeRun
{
//1,声明一个lable类型指针,指向self.view中tag值为1的view
//2,要告诉编译器这个view是UILabel类型的(强转)
UILabel *label = (UILabel *)[self.view viewWithTag:1];
int row = arc4random_uniform(4)+1;
int num = arc4random_uniform(6)+1;
label.text = [NSString stringWithFormat:@"%d%d",row,num];
}
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一分割一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
可动国际象棋
#import "AppDelegate.h"
#import "HomeViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HomeViewController *hvc = [[HomeViewController alloc] init];
self.window.rootViewController = hvc;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
[self createChess];
[self createBtn];
}
- (void)createChess
{
for (int i = 0; i<8; i++) {
for (int j = 0; j<8; j++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(j*40, i*40+60, 40, 40)];
if ((i+j)%2) {
view.backgroundColor = [UIColor whiteColor];
} else {
view.backgroundColor = [UIColor blackColor];
}
[self.view addSubview:view];
}
}
}
- (void)createBtn
{
NSArray *titleArr = [NSArray arrayWithObjects:@"车",@"马",@"象",@"王",@"后",@"象",@"马",@"车", nil];
for (int i = 0; i<8; i++) {
for (int j = 0; j<8; j++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:30];
if (i<2) {
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
if (!i) {
[btn setTitle:[titleArr objectAtIndex:j] forState:UIControlStateNormal];
} else {
[btn setTitle:@"兵" forState:UIControlStateNormal];
}
}
if (i>5) {
[btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
if (i == 7) {
[btn setTitle:[titleArr objectAtIndex:7-j] forState:UIControlStateNormal];
} else {
[btn setTitle:@"兵" forState:UIControlStateNormal];
}
}
btn.frame = CGRectMake(j*40, i*40+60, 40, 40);
btn.tag = j+i*8+1;
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
}
- (void)btnClick:(UIButton *)sender
{
NSLog(@"tag = %d",sender.tag);
if (sender.tag<17 || sender.tag>48) {
_tempBtn = sender;
} else {
if (_tempBtn) {
//交换frame
CGRect rect = _tempBtn.frame;
_tempBtn.frame = sender.frame;
sender.frame = rect;
_tempBtn = nil;
}
}
}