一、UIButton(按钮)
1、按钮风格(系统、自定义)
//按钮风格
- (void)buttonStytle
{
/*
系统默认
UIButtonTypeSystem
显示详情
UIButtonTypeDetailDisclosure
UIButtonTypeInfoLight
UIButtonTypeInfoDark
一个'+'
UIButtonTypeContactAdd
UIButtonTypeRoundedRect = UIButtonTypeSystem
*/
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.frame = CGRectMake(100, 50, VIEW_WIDTH-200, 50);
button.backgroundColor = [UIColor redColor];
//设置着色(系统默认的是蓝色)
//button.tintColor = [UIColor whiteColor];
//可以解决圆角问题
button.layer.cornerRadius = 10;
[self.view addSubview:button];
}
2、基本属性(标题、颜色、状态)
//基本属性
- (void)baseAttributes
{
//自定义风格没有按下的效果
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 150, VIEW_WIDTH-200, 100);
button.backgroundColor = [UIColor greenColor];
//这种方式只能设置颜色,文本
button.titleLabel.font = [UIFont systemFontOfSize:50];
//设置标题
//正常状态
[button setTitle:@"正常" forState:UIControlStateNormal];
//标题颜色
[button setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
//高亮状态
[button setTitle:@"高亮" forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//高亮状态发光效果开关
button.showsTouchWhenHighlighted = YES;
//禁用状态,button.enabled为NO时有效
[button setTitle:@"禁用" forState:UIControlStateDisabled];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
//button.enabled = NO;
//选中状态,button.selected为YES时有效
[button setTitle:@"选中" forState:UIControlStateSelected];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
button.selected = YES;
//选中状态标题label的背景色
button.tintColor = [UIColor yellowColor];
[self.view addSubview:button];
}
3、图片及边界(背景、内容、边界、渲染模式)
//设置背景图片和和图片和title布局
- (void)iconAndEdge
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 300, VIEW_WIDTH-200, 200);
button.backgroundColor = [UIColor blueColor];
//设置背景图(小了拉伸,大了压缩)
[button setBackgroundImage:[UIImage imageNamed:@"start"] forState:UIControlStateNormal];
//设置图标
//设置渲染模式
UIImage *image = [[UIImage imageNamed:@"webChat"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[button setImage:image forState:UIControlStateNormal];
//设置标题
[button setTitle:@"标题" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:25];
//既有图标又有标题时,默认居中布局
//设置对齐方式
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
//适应字体及内容,否则titleLabel的尺寸为(0,0)
[button.titleLabel sizeToFit];
//NSLog(@"label:%@", NSStringFromCGRect(button.titleLabel.frame));
CGSize titleSize = button.titleLabel.frame.size;
CGSize buttonSize = button.frame.size;
//设置图标四周边界
[button setImageEdgeInsets:UIEdgeInsetsMake((buttonSize.height-image.size.height-titleSize.height)/2, (buttonSize.width-image.size.width)/2, 0, 0)];
//设置标题四周边界
[button setTitleEdgeInsets:UIEdgeInsetsMake((buttonSize.height+image.size.height-titleSize.height)/2, (buttonSize.width-titleSize.width)/2-image.size.width, 0, 0)];
[self.view addSubview:button];
}
4、添加事件及处理方法
//添加点击事件
- (void)createButtonOne
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, VIEW_WIDTH-200,100);
button.backgroundColor = [UIColor redColor];
button.showsTouchWhenHighlighted = YES;
button.layer.cornerRadius = 15;
/*添加事件
参数1:事件由谁(哪个对象)来处理
参数2:事件怎么处理(处理方法)
参数3:什么事件
UIControlEventTouchUpInside 按下并在一定范围内抬起
UIControlEventTouchUpOutside 按下并在一定范围外抬起
*/
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(buttonHandle) forControlEvents:UIControlEventTouchUpOutside];
[self.view addSubview:button];
}
- (void)buttonClick
{
#define Random arc4random()%256/255.0
self.view.backgroundColor = [UIColor colorWithRed:Random green:Random blue:Random alpha:1.0];
}
- (void)buttonHandle
{
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)createButtonTwo
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 250, VIEW_WIDTH-200,100);
button.backgroundColor = [UIColor greenColor];
button.showsTouchWhenHighlighted = YES;
button.layer.cornerRadius = 15;
[button setTitle:@"开始" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:50];
[button addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)changeTitle:(UIButton *)button
{
NSString *title = button.currentTitle;
if ([title isEqualToString:@"开始"]) {
[button setTitle:@"暂停" forState:UIControlStateNormal];
} else {
[button setTitle:@"开始" forState:UIControlStateNormal];
}
}
5、UILabel&UIButton使用
//创建Label
- (void)createLabel
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, VIEW_WIDTH- 200,100)];
label.backgroundColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:50];
label.textColor = [UIColor whiteColor];
label.text = @"0";
label.tag = 100;
[self.view addSubview:label];
[label release];
}
//创建Button
- (void)createButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 250, VIEW_WIDTH-200, 100);
button.backgroundColor = [UIColor greenColor];
button.showsTouchWhenHighlighted = YES;
button.layer.cornerRadius = 20;
[button addTarget:self action:@selector(addNumber) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(clearNumber) forControlEvents:UIControlEventTouchUpOutside];
[self.view addSubview:button];
}
//button相应事件,改变label值
- (void)addNumber
{
//找到label
UILabel *label = (UILabel *)[self.view viewWithTag:100];
NSInteger number = [label.text integerValue];
label.text = [NSString stringWithFormat:@"%ld", number+1];
}
//button响应事件,改变label标题显示
- (void)clearNumber
{
UILabel *label = (UILabel *)[self.view viewWithTag:100];
label.text = @"0";
}
二、NSTimer(定时器)
1、创建
//创建定时器
- (void)createTimer
{
/*创建定时器
参数1:时间间隔,单位为秒
参数2:事件谁来处理
参数3:怎么处理
参数4:传递的参数
参数5:是否重复
*/
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeColor:) userInfo:nil repeats:YES];
//定时器创建后默认是开启的,需要先关闭定时器
[_timer setFireDate:[NSDate distantFuture]];
}
定时器响应事件
- (void)changeColor:(NSTimer *)timer
{
#define Random arc4random()%256/255.0
self.view.backgroundColor = [UIColor colorWithRed:Random green:Random blue:Random alpha:1.0];
}
2、开启
3、关闭
创建Button响应事件进行改变定时器状态
- (void)createButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 200, self.view.frame.size.width-200, 100);
button.backgroundColor = [UIColor greenColor];
button.layer.cornerRadius = 20;
button.showsTouchWhenHighlighted = YES;
[button setTitle:@"开始" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:50];
[button addTarget:self action:@selector(changeStatus:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)changeStatus:(UIButton *)button
{
NSString *title = button.currentTitle;
if ([title isEqualToString:@"开始"]) {
//将定时器的开启时间设置为很久以前的一个时间会开启定时器
[_timer setFireDate:[NSDate distantPast]];
[button setTitle:@"暂停" forState:UIControlStateNormal];
} else {
//将定时器的开启时间设置为将来的一个时间会关闭定时器
[_timer setFireDate:[NSDate distantFuture]];
[button setTitle:@"开始" forState:UIControlStateNormal];
}
}
4、销毁
- (void)dealloc
{
if (_timer.isValid) {
//让定时器失效
[_timer invalidate];
_timer = nil;
}
[super dealloc];
}
三、坐标系相关结构
1、CGPoint、CGSize、CGRect
CGPoint:点,用于描述视图的位置
CGSize:尺寸,用于描述视图的位置
CGRect:矩形,既有位置CGPoint ,又有CGSize,大小
2、frame:描述视图在父视图中的位置及大小
是相对于父视图的坐标系
3、center:视图的中心点,修改它会影响frame
4、bounds:边界,原点会影响子视图的位置,大小会影响frame
是相对于自身的坐标系的
5、transform:形变属性,比如:平移、缩放、旋转
//视图的旋转和平移
- (void)createSceneTwo
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 350, 100, 150)];
view.backgroundColor = [UIColor blueColor];
//平移(x'=x+100,y'=y+50)
view.transform = CGAffineTransformMakeTranslation(100, 150);
//缩放(x'=2*x,y'=0.5*y),中心点不变
view.transform = CGAffineTransformMakeScale(4,0.5);
//旋转,角度单位是弧度,顺时针为正
view.transform = CGAffineTransformMakeRotation(M_PI_4);
//以上三种方式效果不叠加,若想效果叠加可以使用以下方式
//view.transform = CGAffineTransformRotate(view.transform, M_PI_2);
//view.transform = CGAffineTransformScale(view.transform, 2, 0.5);
view.transform = CGAffineTransformTranslate(view.transform, 100, 50);
//取消所有形变设置
//view.transform = CGAffineTransformIdentity;
[self.view addSubview:view];
[view release];
}