1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#import "RootViewController.h"

@interface RootViewController ()<UITextFieldDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createTextField];
}
//创建UITextField
-(void)createTextField{
// 初始化UITextField
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 400, 180, 50)];
// 设置背景色
textField.backgroundColor = [UIColor yellowColor];
// 设置边框样式
textField.borderStyle = UITextBorderStyleRoundedRect;


// 一键清除butotn
textField.clearButtonMode = UITextFieldViewModeWhileEditing;

textField.delegate = self;

// 添加到视图
[self.view addSubview:textField];
}
#pragma mark - delegate

//点击return键,执行的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"点击了return");
// 注销第一响应
[textField resignFirstResponder];
return YES;
}

//是否能进入编辑
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"将要开始编辑");
// YES:可以进入编辑
// NO:不能进入编辑
return YES;
}
//当上面的方法返回YES的时候,才会调用此代理方法
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"已经开始编辑");
}
//将要结束编辑
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"将要结束编辑");
// YES:可以结束编辑
// NO:不能结束编辑
return YES;
}
//当上面的方法返回YES的时候,才会调用此代理方法
//已经结束编辑
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"已经结束编辑");
}

//点击clearButton时调用的方法
-(BOOL)textFieldShouldClear:(UITextField *)textField{
// 当textField.text值是1的时候,不允许删除
if ([textField.text isEqualToString:@"1"]) {
return NO;
}
// YES:允许删除
// NO:不允许删除
return YES;
}

//获得每一次输入的字符
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

// NSLog(@"string = %@",string);

// 根据每次输入的字符,组成一个字符串
NSMutableString *mutableString = [[NSMutableString alloc]initWithString:textField.text];
[mutableString insertString:string atIndex:range.location];
NSLog(@"%@",mutableString);

return YES;
}
@end

View切换层级

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createSubViews];

}

-(void)createSubViews{

UILabel *redLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
redLabel.backgroundColor = [UIColor redColor];
redLabel.text = @"红色";
[self.view addSubview:redLabel];

UILabel *greenLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
greenLabel.backgroundColor = [UIColor greenColor];
greenLabel.text = @"绿色";
[self.view addSubview:greenLabel];

UILabel *blueLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 80, 100, 100)];
blueLabel.backgroundColor = [UIColor blueColor];
blueLabel.text = @"蓝色";
[self.view addSubview:blueLabel];

// 把子视图提到最前面
// [self.view bringSubviewToFront:redLabel];

// 把子视图放到最底层
// [self.view sendSubviewToBack:blueLabel];

// 把某一个子视图放到第0层
// [self.view insertSubview:blueLabel atIndex:0];

// 把blueLabel 放到greenLabel 下面
// [self.view insertSubview:blueLabel belowSubview:greenLabel];

// 把blueLabel放置到redLabel 上面
// [self.view insertSubview:redLabel aboveSubview:blueLabel];

NSLog(@"subViews = %@",self.view.subviews);

}

@end

键盘状态监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#import "RootViewController.h"

@interface RootViewController ()<UITextFieldDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
myView.backgroundColor = [UIColor blackColor];
[self.view addSubview:myView];


[self.view setBackgroundColor:[UIColor redColor]];

[self createTextField];
}
-(void)createTextField{
// 初始化UITextField并设置frame
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 400, 100, 50)];
// 设置背景色
textField.backgroundColor = [UIColor yellowColor];

textField.delegate = self;

// 添加到视图
[self.view addSubview:textField];


// 键盘状态改变的时候,会发出以下通知
// UIKeyboardWillShowNotification; 键盘即将显示
// UIKeyboardDidShowNotification; 键盘显示完毕
// UIKeyboardWillHideNotification; 键盘即将消失
// UIKeyboardDidHideNotification;键盘消失完毕
// UIKeyboardWillChangeFrameNotification 键盘即将改变frame
// UIKeyboardDidChangeFrameNotification 键盘已经改变frame

// 获取通知中心
NSNotificationCenter*center = [NSNotificationCenter defaultCenter];

// 监听键盘弹出通知
[center addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 监听键盘收起通知
[center addObserver:self selector:@selector(KeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

// 成为第一响应者
// [textField becomeFirstResponder];
}

//键盘将要消失时
-(void)KeyboardWillHide:(NSNotification*)noti{
NSLog(@"%@",noti.userInfo);
// UIKeyboardAnimationDurationUserInfoKey = "0.25";
// UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
// UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
// UIKeyboardFrameEndUserInfoKey = "NSRect: {{0,667}, {375, 258}}";


self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
//键盘将要出现
-(void)KeyboardWillShow:(NSNotification*)noti{
NSLog(@"%@",noti.userInfo);
// UIKeyboardAnimationDurationUserInfoKey = "0.25";
// UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
// UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
// UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
// UIKeyboardIsLocalUserInfoKey = 1;

CGRect rect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
NSLog(@"rect = %@",NSStringFromCGRect(rect));


self.view.frame = CGRectMake(0, -rect.size.height, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - textField delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
// 取消第一响应者
[textField resignFirstResponder];
return YES;
}
@end

手机晃动事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height


#import "ViewController.h"

@interface ViewController ()
{
UIImageView *_topImageView;
UIImageView *_bottomImageView;
UIImageView *_showImageView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createImageViews];
}
//检测到手机晃动
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
// 显示中间的图片
[self showImageView];
NSLog(@"晃动开始");
}
//晃动结束
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
// 隐藏中间的图片
[self hideImageView];
NSLog(@"晃动结束");
}

-(void)hideImageView{
[UIView animateWithDuration:2 animations:^{
// 设置返回原始位置
_topImageView.frame = CGRectMake(0, 0, screenWidth, screenHeight/2);
_bottomImageView.frame = CGRectMake(0, screenHeight/2, screenWidth, screenHeight/2);
}];
}
-(void)showImageView{
// 使用动画为了更美观
[UIView animateWithDuration:1 animations:^{

_topImageView.frame = CGRectMake(0, -100, screenWidth, screenHeight/2);
_bottomImageView.frame = CGRectMake(0, screenHeight/2 + 100, screenWidth, screenHeight/2);
}];
}
#pragma mark 创建UI
-(void)createImageViews{

_showImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];
_showImageView.center = self.view.center;
_showImageView.image = [UIImage imageNamed:@"ShakeHideImg_women"];
[self.view addSubview:_showImageView];


_topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2)];
_topImageView.image = [UIImage imageNamed:@"Shake_Logo_Up"];
[self.view addSubview:_topImageView];

_bottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
_bottomImageView.image = [UIImage imageNamed:@"Shake_Logo_Down"];
[self.view addSubview:_bottomImageView];
}


@end

OC画图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#import "Customview.h"

@implementation Customview
{
NSMutableArray *lineArray;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor whiteColor]];

UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"撤销" forState:UIControlStateNormal];
[button addTarget:self action:@selector(doButton:) forControlEvents:UIControlEventTouchDown];
button.frame=CGRectMake(110, 380, 100, 40);
[button setBackgroundColor:[UIColor redColor]];
[self addSubview:button];

UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"橡皮擦" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(doButton:) forControlEvents:UIControlEventTouchDown];
button2.frame=CGRectMake(110, 430, 100, 40);
[button2 setBackgroundColor:[UIColor redColor]];

[self addSubview:button2];

//实例化数组(用来存放移动点的数组)
lineArray =[[NSMutableArray alloc]init];
}
return self;
}

-(void)doButton:(id)button
{
[lineArray removeLastObject];
[self setNeedsDisplay];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//创建触摸事件对象
UITouch *touch = [touches anyObject];
//拿到初始点
CGPoint firstPoint = [touch locationInView:self];
//转换成value对象
NSValue *value = [NSValue valueWithCGPoint:firstPoint];
//创建存放每个点的内层数组(一条线)
NSMutableArray *pointArray = [[NSMutableArray alloc]init];
[pointArray addObject:value];
//把每一笔画对应 的数组放入外层数组
[lineArray addObject:pointArray];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//拿到移动 时的每一个点
CGPoint currentPoint = [touch locationInView:self];
//拿到最后笔画对应的数组
NSMutableArray *pointAaary = [lineArray lastObject];
NSValue *value = [NSValue valueWithCGPoint:currentPoint];
//把移动 的每一个点放入对应的数组
[pointAaary addObject:value];
//让视图重绘(调用该方法后self的drawrect方法会被调用)
[self setNeedsDisplay];

}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}
//iOS的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView 的类,并重写drawRect方法,在这里进行绘图操作,程序会自动调用此方法进行绘图。
- (void)drawRect:(CGRect)rect
{
//拿到当前绘图上下文环境
CGContextRef currentText = UIGraphicsGetCurrentContext();
//设置画笔线条的宽度
CGContextSetLineWidth(currentText, 2);
//将画笔设置为红色
CGColorRef cf = [UIColor redColor].CGColor;
CGContextSetStrokeColorWithColor(currentText, cf);

for (int i = 0; i < [lineArray count]; i++) {
//拿到每一个笔画对应的数组
NSMutableArray *pArray = [lineArray objectAtIndex:i];
for (int j = 0; j < [pArray count]-1; j++) {

NSValue *previousValue = [pArray objectAtIndex:j];
NSValue *currentValue = [pArray objectAtIndex:j+1];

CGPoint previousPoint = [previousValue CGPointValue];
CGPoint currentValuePoint = [currentValue CGPointValue];
//设置画笔的起点
CGContextMoveToPoint(currentText, previousPoint.x, previousPoint.y);
//设置两点确定一条直线的另一点
CGContextAddLineToPoint(currentText, currentValuePoint.x, currentValuePoint.y);
}
//提交描绘的轨迹,这才是真正画线的地方
CGContextStrokePath(currentText);
}


}
@end

delegate反向传值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()<SecondViewControllerDelegate>

@end
@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createButton];
}
-(void)createButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:@"下一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)onClick{
SecondViewController *secondVc = [[SecondViewController alloc]init];
secondVc.delegate = self;
[self.navigationController pushViewController:secondVc animated:YES];
}

-(void)changeColor:(UIColor *)color{
[self.view setBackgroundColor:color];
}

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createButton];

[self.view setBackgroundColor:[UIColor lightGrayColor]];
}
-(void)createButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:@"上一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)onClick{
if (self.delegate && [self.delegate respondsToSelector:@selector(changeColor:)]) {
[self.delegate changeColor:[UIColor yellowColor]];
}
[self.navigationController popViewControllerAnimated:YES];
}
@end