“ 设置 ” 应用程序演示视图 & 视图控制器
演示细节说明
演练 —— 加法计算器
@interface ViewController : UIViewController
// 数字1
@property (weak, nonatomic) IBOutlet UITextField *number1;
// 数字2
@property (weak, nonatomic) IBOutlet UITextField *number2;
// 结果标签
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
- (IBAction)calculate;
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Actions
// 计算结果
- (IBAction)calculate
{
// 获取界面控件的数值
NSInteger number1 = [_number1.text integerValue];
NSInteger number2 = [_number2.text integerValue];
// 用计算结果设置标签内容
[_resultLabel setText:[NSString stringWithFormat:@"%d", number1 + number2]];
// 关闭键盘
[self.view endEditing:YES];
}
@end
加法计算器的扩充 —— 监听文本控件的回车事件
#pragmamark -UITextField代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//判断接收回车事件的是哪一个文本控件
if (_number1 ==textField) {
[_number2 becomeFirstResponder];
} else {
[selfcalculate];
}
returnYES;
}
ViewController.h
@interface ViewController : UIViewController <UITextFieldDelegate>
// 用户名
@property (weak, nonatomic) IBOutlet UITextField *qqNoTextField;
// 密码
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
// 消息标签
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
// 登录
- (IBAction)login;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITextField代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (_qqNoTextField == textField) {
[_passwordTextField becomeFirstResponder];
} else {
[self login];
}
return YES;
}
#pragma mark - Actions
- (IBAction)login
{
// 获取输入信息
NSString *text = [NSString stringWithFormat:@"%@QQ号的密码是:\n[%@]", _qqNoTextField.text, _passwordTextField.text];
// 设置标签信息
[_messageLabel setText:text];
// 关闭键盘
[self.view endEditing:YES];
}
@end
[UIView beginAnimations:nil context:nil];
// 设置动画持续时间
[UIView setAnimationDuration:.3];
// 需要动画的代码段
[UIView commitAnimations];
对比beginAnimations动画的方式,块动画具有以下几点优势:
UIButton *button=[[UIButton alloc] initWithFrame:rect];
UIButton*button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
在OC开发中,实例化任何类型的非自定义对象,都请首先尝试一下是否存在快速定义方法。如果存在快速定义方法,就尽量不要使用init之类的方法实例化对象!
[button setTitle:@"按钮" forState:UIControlStateNormal];
[button setTitle:@"有人摸我" forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[button setBackgroundColor:[UIColor orangeColor]];
UIImage*image = [UIImage imageNamed:@"sub_black_add.png"];// 加载图像
[button setImage:image forState:UIControlStateNormal];// 设置按钮图像
[buttonsetBackgroundImage:image forState:UIControlStateNormal];// 设置按钮背景图像
[button addTarget:self action:@selector(tapButton) forControlEvents:UIControlEventTouchUpInside];// 设置按钮点击监听
@interface ViewController : UIViewController
@end
@interface ViewController ()
{
// 在定义成员变量时,变量名必须要使用下划线!!!
UIButton *_button;
}
@end
@implementation ViewController
// 按钮监听
- (void)tapButton
{
NSLog(@"我被摸了");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 1. 创建黄色按钮
// 1.1 实例化按钮
_button = [UIButton buttonWithType:UIButtonTypeCustom];
// 1.2 设置按钮文字
[_button setTitle:@"我是按钮" forState:UIControlStateNormal];
// 1.3 设置按钮背景颜色
[_button setBackgroundColor:[UIColor orangeColor]];
// 1.4 设置按钮监听事件
[_button addTarget:self action:@selector(tapButton) forControlEvents:UIControlEventTouchUpInside];
// 1.5 设置按钮位置
[_button setFrame:CGRectMake(110.0, 100.0, 100.0, 100.0)];
// 1.6 将按钮添加到视图
[self.view addSubview:_button];
// 2. 创建两个容器视图
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 300.0, 160.0, 160.0)];
[self.view addSubview:view1];
// 2.1 需要添加4个按钮
// 有一个问题,就图片的文件名好像没有什么规律
// 想法:可不可以用NSArray记录住图像的文件名,然后通过循环的方式创建按钮
// 初始化一个按钮文件名的数组
NSArray *imageNames = @[@"up", @"down", @"prev", @"next"];
NSLog(@"%@", imageNames);
[self addActionButtonsToView:view1 images:imageNames action:@selector(translationAction:)];
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(160.0, 300.0, 160.0, 160.0)];
[self.view addSubview:view2];
NSArray *imageNames2 = @[@"rotate_ccw", @"rotate_cw", @"add", @"remove"];
[self addActionButtonsToView:view2 images:imageNames2 action:@selector(transformAction:)];
// 3. 分别将4个按钮加入不同的容器视图
}
#pragma mark - 创建操作按钮
- (void)addActionButtonsToView:(UIView *)view images:(NSArray *)images action:(SEL)action
{
// 通过循环的方式建立4个按钮
for (NSInteger i = 0; i < 4; i++) {
// 1) 实例化按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 2) 设置按钮边框
CGRect frame = CGRectMake((i % 2) * 80, (i / 2) * 80, 80.0, 80.0);
[btn setFrame:frame];
// 3) 设置按钮普通图像
// 3.1 需要普通图片的文件名 sub_black_xxx.png,其中的xxx是在数组中定义的
NSString *textNormal = [NSString stringWithFormat:@"sub_black_%@.png", images[i]];
UIImage *imageNormal = [UIImage imageNamed:textNormal];
[btn setImage:imageNormal forState:UIControlStateNormal];
// 4) 设置按钮长按图像
NSString *textHigh = [NSString stringWithFormat:@"sub_blue_%@.png", images[i]];
UIImage *imageHigh = [UIImage imageNamed:textHigh];
[btn setImage:imageHigh forState:UIControlStateHighlighted];
// 5) 设置按钮监听方法
[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
// 6) 设置按钮的Tag,以区分按钮
[btn setTag:i];
// 7) 将按钮添加到容器视图
[view addSubview:btn];
}
}
#pragma mark - 按钮操作
// 按钮的上下左右移动操作
- (void)translationAction:(UIButton *)sender
{
NSLog(@"我干活了 %d", sender.tag);
// 移动按钮位置可以通过设置中心点实现
CGPoint point = [_button center];
switch (sender.tag) {
case 0: // 上
point.y -= 50.0;
if(point.y <= -100)
{
point.y = 480;
}
break;
case 1: // 下
point.y += 50.0;
if(point.y >= 580)
{
point.y = -100;
}
break;
case 2: // 左
point.x -= 50.0;
if(point.x <= -100)
{
point.x = 320;
}
break;
case 3: // 右
point.x += 50.0;
if(point.x >= 420)
{
point.x = 0;
}
break;
default:
break;
}
// 设置按钮的中心点
[_button setCenter:point];
}
// 按钮的形变操作
- (void)transformAction:(UIButton *)sender
{
NSLog(@"我要变形了 %d", sender.tag);
CGAffineTransform transform;
switch (sender.tag) {
case 0: // 逆时针
transform = CGAffineTransformRotate(_button.transform, -M_PI_4);
break;
case 1: // 顺时针
transform = CGAffineTransformRotate(_button.transform, M_PI_4);
break;
case 2: // 放大
transform = CGAffineTransformScale(_button.transform, 1.2, 1.2);
break;
case 3: // 缩小
transform = CGAffineTransformScale(_button.transform, 1.0 / 1.2, 1.0 / 1.2);
break;
default:
break;
}
// 设置按钮的形变
[UIView animateWithDuration:1.0f animations:^{
[_button setTransform:transform];
}];
}
@end
UILabel*label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 210.0, 320.0, 40.0)];// 实例化UILabel并指定其边框
[label setText:@"HelloWorld"];// 设置label显示的文本
[label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:40]];// 设置字体和字体大小
[label setBackgroundColor:[UIColor greenColor]];// 设置背景色
[label setTextColor:[UIColor whiteColor]];// 设置文本的颜色
[label setShadowColor:[UIColor colorWithWhite:0.1f alpha:0.8f]];// 设置文本的阴影色彩和透明度
[label setShadowOffset:CGSizeMake(2.0f, 2.0f)];// 设置阴影的偏移量
[label setTextAlignment:NSTextAlignmentCenter];// 设置文本在label中的对齐方式
[label setLineBreakMode:NSLineBreakByWordWrapping];// 换行技巧:如下换行可实现多行显示,但要求label有足够的宽度。 指定换行模式
[label setNumberOfLines:2];// 指定label的行数,为0时没有最大行数限制
[label setTransform:CGAffineTransformMakeRotation(M_PI_4)];// 设置label的旋转角度
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 实例化UILabel并指定其边框
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 80.0, 320.0, 300.0)];
// 设置label显示的文本
[label setText:@"Hello World Hello World Hello World Hello World Hello World"];
// 设置字体和字体大小
[label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:40]];
// 设置背景色
[label setBackgroundColor:[UIColor greenColor]];
// 设置文本的颜色
[label setTextColor:[UIColor whiteColor]];
// 设置文本的阴影色彩和透明度
[label setShadowColor:[UIColor colorWithWhite:0.1f alpha:0.8f]];
// 设置阴影的偏移量
[label setShadowOffset:CGSizeMake(2.0f, 2.0f)];
// 设置文本在label中的对齐方式
[label setTextAlignment:NSTextAlignmentCenter];
// 换行技巧:如下换行可实现多行显示,但要求label有足够的宽度。
// 指定换行模式
[label setLineBreakMode:NSLineBreakByWordWrapping];
// 指定label的行数,为0时没有最大行数限制
[label setNumberOfLines:0];
// 设置label的旋转角度
[label setTransform:CGAffineTransformMakeRotation(M_PI_4)];
[self.view addSubview:label];
}
@end