UIStepper

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

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 初始化并设置frame
UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
// 最小值
stepper.minimumValue = 0;

// 最大值
stepper.maximumValue = 10;
// 设置当前值
stepper.value = 4;
// 设置每次+2或-2,默认是1
stepper.stepValue = 2;

// 设置颜色
[stepper setTintColor:[UIColor orangeColor]];
// 添加事件
[stepper addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

// 处理事件是否一直回调,默认是YES
stepper.continuous = YES;

// 设置能否重复操作,默认是YES
stepper.autorepeat = YES;

// 设置是否能循环,默认是NO
stepper.wraps = YES;

[self.view addSubview:stepper];
}
-(void)valueChange:(UIStepper*)stepper{
NSLog(@"%lf",stepper.value);
}
@end

UISlider

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

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// slider高度是固定的
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(100, 100, 200, 30)];
slider.tag = 10;
// 最小值
slider.minimumValue = 0;
// 最大值
slider.maximumValue = 255;
// 设置当前值
slider.value = 200;

// 未滑动到的颜色
slider.maximumTrackTintColor = [UIColor greenColor];
// 已经滑动过的颜色
slider.minimumTrackTintColor = [UIColor orangeColor];
// 小球(滑块)颜色
slider.thumbTintColor = [UIColor blackColor];
// 设置图片
// 最左端图片
slider.minimumValueImage = [UIImage imageNamed:@"blackHeart"];
// 最右端图片
slider.maximumValueImage = [UIImage imageNamed:@"redHeart"];
// 设置滑块正常状态下的图片
[slider setThumbImage:[UIImage imageNamed:@"blackHeart"] forState:UIControlStateNormal];
// 设置滑块高亮状态下的图片
[slider setThumbImage:[UIImage imageNamed:@"redHeart"] forState:UIControlStateHighlighted];

[slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];

UISlider *greenSlider = [[UISlider alloc]initWithFrame:CGRectMake(100, 200, 200, 80)];
greenSlider.tag = 11;
greenSlider.minimumValue = 0;
greenSlider.maximumValue = 255;
[greenSlider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:greenSlider];

UISlider *blueSlider = [[UISlider alloc]initWithFrame:CGRectMake(100, 300, 200, 100)];
blueSlider.tag = 12;
blueSlider.minimumValue = 0;
blueSlider.maximumValue = 255;
[blueSlider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:blueSlider];
}
-(void)valueChange:(UISlider*)slider{
NSLog(@"---%lf",slider.value);
UISlider *redSlider = (UISlider *)[self.view viewWithTag:10];
UISlider *greenSlider = (UISlider *)[self.view viewWithTag:11];
UISlider *blueSlider = (UISlider *)[self.view viewWithTag:12];

[self.view setBackgroundColor:[UIColor colorWithRed:redSlider.value/255.0 green:greenSlider.value/255.0 blue:blueSlider.value/255.0 alpha:1.0]];


}

@end

UIsegementControl

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
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 初始化UISegmentedControl
UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
// 设置背景色
segmentControl.backgroundColor = [UIColor yellowColor];
// 添加第一段
[segmentControl insertSegmentWithTitle:@"第一段" atIndex:0 animated:YES];
// 添加第二段
[segmentControl insertSegmentWithTitle:@"第二段" atIndex:1 animated:YES];
// 添加第三段
// 设置文字和图片,谁在后面,显示谁
[segmentControl insertSegmentWithTitle:@"第三段" atIndex:2 animated:YES];

UIImage *image = [UIImage imageNamed:@"qq"];
// 显示原始图片
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// [segmentControl insertSegmentWithImage: image atIndex:2 animated:YES];
[segmentControl setImage:image forSegmentAtIndex:2];


// 选中颜色
segmentControl.tintColor = [UIColor redColor];
// 设置选中某一段
segmentControl.selectedSegmentIndex = 1;

// 获取某一段的title
NSString *title = [segmentControl titleForSegmentAtIndex:1];
NSLog(@"title = %@",title);
// 添加响应事件
[segmentControl addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:segmentControl];

}
-(void)valueChange:(UISegmentedControl*)seg{

NSInteger index = seg.selectedSegmentIndex;
switch (index) {
case 0:{
NSLog(@"0");

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"还钱" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"不还", nil];
[alert show];
}
break;
case 1:
NSLog(@"1");
break;
case 2:
NSLog(@"2");
break;

default:
break;
}
}

@end

Transform形变

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
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

// [self transform];
[self lastTransform];
}
#
-(void)lastTransform{
UIView *changeView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
changeView.tag = 20;
changeView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:changeView];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 50, 50, 50);
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)onClick{

// 仿射变化
// 通过center bounds transform来控制要显示的内容
UIView *changeView = [self.view viewWithTag:20];
// 在上次形变的基础上,继续形变
changeView.transform = CGAffineTransformTranslate(changeView.transform, 10, 0);

// 旋转
CGFloat angle = [self getAgreeFromDegree:10];
changeView.transform = CGAffineTransformRotate(changeView.transform, angle);

// 缩放
// changeView.transform = CGAffineTransformScale(changeView.transform, 0.9, 1.0);
NSLog(@"%@",NSStringFromCGRect(changeView.frame));
NSLog(@"%f",changeView.bounds.size.width);
NSLog(@"%f",changeView.bounds.size.height);
NSLog(@"%f",changeView.center.x);
NSLog(@"%f",changeView.center.y);

}
#pragma mark -
#pragma mark 形变
//形变
-(void)transform{
UIView *changeView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
[view1 setBackgroundColor:[UIColor blueColor]];
[changeView addSubview:view1];

changeView.backgroundColor = [UIColor yellowColor];

// 平移
changeView.transform = CGAffineTransformMakeTranslation(0, 100);
// 缩放
// changeView.transform = CGAffineTransformMakeScale(0.5, 1.0);

// 旋转
// 传入的值为正值时,顺时针转动
CGFloat angle = [self getAgreeFromDegree:20];
changeView.transform = CGAffineTransformMakeRotation(angle);

[self.view addSubview:changeView];
}
-(CGFloat)getAgreeFromDegree:(CGFloat)degree{
CGFloat agree = degree * M_PI / 180;
return agree;
}
//创建一个UIView
-(void)createUIView{
// 初始化UIView
UIView *orangeView = [[UIView alloc]init];
// 设置背景色
orangeView.backgroundColor = [UIColor orangeColor];
// 设置frame
orangeView.frame = CGRectMake(0, 0, 100, 100);
// 设置center
orangeView.center = CGPointMake(0, 0);
orangeView.center = self.view.center;

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

@end

UIActionSheet

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
#import "ViewController.h"

@interface ViewController ()<UIActionSheetDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 初始化
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:@"微博分享",@"微信分享",@"QQ分享", nil];
// 显示在指定的view上
[sheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
// 在这里做事件处理
switch (buttonIndex) {
case 0:
NSLog(@"%ld",buttonIndex);
break;
case 1:
NSLog(@"%ld",buttonIndex);
break;
case 2:
NSLog(@"%ld",buttonIndex);
break;
case 3:
NSLog(@"%ld",buttonIndex);
break;
case 4:
NSLog(@"%ld",buttonIndex);
break;


default:
break;
}
}
@end

Touch

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
#import "ViewController.h"

@interface ViewController ()
{
UIView *myview;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// 初始化myview
myview = [[UIView alloc]initWithFrame:CGRectMake(100,100, 100, 100)];
// 设置背景色
myview.backgroundColor = [UIColor lightGrayColor];
// 添加到视图上
[self.view addSubview:myview];

}
//开始触摸,只调用一次
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touch began");
}
//开始移动,会多次调用
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"----");
// UITouch
// 用来保存跟手指相关的信息,比如:触摸的位置
// 当手指移动时,系统会更新同一个UIToch对象,使之能够一直保存该手指在的触摸位置
// 当手指离开屏幕时,系统会销毁对应的UITouch对象
//
// 从touches中取出手指
UITouch *touch = [touches anyObject];

if (touch.view == myview) {

// 该方法记录了前一个触摸点的位置
CGPoint previousPoint = [touch previousLocationInView:self.view];

/*
返回值表示触摸在self.view上的位置
这里的位置是针对self.view的坐标系(以self.view的左上角为原点(0,0))
如果传nil时,返回的触摸点在UIWindow的位置
*/

// 该方法记录了当前点的位置
CGPoint currentPoint = [touch locationInView:self.view];

NSLog(@"1------>%@",NSStringFromCGPoint(previousPoint));
NSLog(@"2------>%@",NSStringFromCGPoint(currentPoint));
// 获取x偏移量
CGFloat x = currentPoint.x - previousPoint.x;
// 获取y偏移量
CGFloat y = currentPoint.y - previousPoint.y;

// 获取myview中心点
CGPoint center = myview.center;

center.x += x;
center.y += y;

myview.center = center;
}
}
//结束触摸,只调用一次
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touch end");
}
//触摸取消(例如来电打断)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

}
@end

UIScrollView

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>//遵守协议

{
// 滚动视图
UIScrollView *_scrollView;

}

@property (weak, nonatomic) IBOutlet UIButton *myButton;
@end

@implementation ViewController

- (IBAction)onClick:(UIButton *)sender {
[_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)viewDidLoad {
[super viewDidLoad];

[self createScrollView];
[self addSubView];
[self customScrollView];

[self.view bringSubviewToFront:self.myButton];

/*
// [self.myButton addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];

//-(void)onClick{
// NSLog(@"onClick");
//}
*/


}


//创建ScrollView
-(void)createScrollView{
// 初始化UIScrollView
_scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];

// _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
// 设置背景色
_scrollView.backgroundColor = [UIColor orangeColor];
// 设置代理
_scrollView.delegate = self;

[self.view addSubview:_scrollView];

}
-(void)addSubView{
// 获取文件路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"海贼02" ofType:@"jpg"];
// 转换成NSData
NSData *data = [NSData dataWithContentsOfFile:path];
// 初始化图片
UIImage *image = [UIImage imageWithData:data];
// 图片初始化后,会直接获得图片的width 和 height
// NSLog(@"width = %lf---height = %lf",image.size.width,image.size.height);

// 初始化UIImageView
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageView.image = image;

// 设置滚动范围
// 必须给出滚动范围,这样滚动视图才能知道滚动的范围
_scrollView.contentSize = CGSizeMake(image.size.width, image.size.height);


[_scrollView addSubview:imageView];
}
// 定制ScrollView
-(void)customScrollView{

// 设置偏移量
// [_scrollView setContentOffset:CGPointMake(200, 200)];

// 设置距离边框的距离
// [_scrollView setContentInset:UIEdgeInsetsMake(100, 100, 100, 100)];

#pragma mark 滚动相关

// 设置是否可以超出边界
// 默认是YES,超出边界后,有回弹效果
// 设置NO,没有回弹效果
_scrollView.bounces = YES;

// 在这里,想测试下面两个属性时,使用《海贼03.jpg》
// 当contentSize的宽度小于scrollView的宽度时,仍允许左右拖动,默认是NO
_scrollView.alwaysBounceHorizontal = YES;
// 当contentSize的高度小于scrollView的高度时,仍允许上下手动,默认是NO
_scrollView.alwaysBounceVertical = YES;

// 设置是否按页滚动,即每次滚动一个scrollView的宽度或高度,默认是NO
// _scrollView.pagingEnabled = YES;

// 设置是否允许滚动,默认是YES
_scrollView.scrollEnabled = YES;

// 设置是否显示滚动指示,默认是YES
_scrollView.showsHorizontalScrollIndicator = YES;
_scrollView.showsVerticalScrollIndicator = YES;

// 设置滚动指示的样式
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// UIScrollViewIndicatorStyleDefault,
// UIScrollViewIndicatorStyleBlack,
// UIScrollViewIndicatorStyleWhite

// 设置滚动条距离scrollView边框的距离
_scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 30, 60);

_scrollView.scrollsToTop = YES;

}
#pragma mark scrollView delegate
//将要拖拽
//当开始滚动视图时,会执行该方法
//一次有效的滑动执行一次(开始滑动到手指松开),
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"将要拖拽");
}
//scrollView滚动时,就会调用该方法
//任何offset值的改变都会调用该方法。在滚动过程中,会调用多次
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// CGPoint point = scrollView.contentOffset;
// NSLog(@"偏移量%lf",point.x);
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
NSLog(@"将要结束拖拽");
}
//当手指离开屏幕的一瞬间,调用该方法,一次有效的滑动,只执行一次
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
// NSLog(@"%lf",scrollView.contentOffset.x);
NSLog(@"拖拽结束");
// [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

//将要减速
//该方法在scrollViewDidEndDragging 结束后执行
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"将要减速");
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"减速结束");

// [scrollView setContentOffset:CGPointMake(0, 0) animated: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
#pragma mark 缩放 delegate
//当将要开始缩放时,执行该方法。一次有效的缩放,执行一次
-(void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
NSLog(@"将要开始缩放");
}
//正在缩放
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
NSLog(@"正在缩放");
CGFloat value = scrollView.zoomScale;
NSLog(@"%f",value);
}
//结束缩放
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
NSLog(@"结束缩放");
}

//返回要缩放的UIView对象
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
// 不允许返回scrollView
// 只能返回scrollView的子视图
return scrollView.subviews[0];
}

//当用户点击状态栏后,滚动视图是否能够滚动到顶部
-(BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
// 当设置scrollToTop 为YES时,这里设置YES才会生效
return NO;
}
//当滚动视图滚动到最顶端的时候,会执行该方法
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
NSLog(@"%s",__func__);
}
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//初始化window
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//设置背景颜色
self.window.backgroundColor = [UIColor whiteColor];
//视图控制器
RootViewController *rootVC = [[RootViewController alloc]init];
// 初始化导航控制器,并设置基栈
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootVC];
nav.view.backgroundColor = [UIColor redColor];


//系统控制器设置为自创的
self.window.rootViewController = nav;

//让self.window 显示
[self.window makeKeyAndVisible];

return YES;
}

#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];


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

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 50);
[button setTitle:@"下一级" forState:UIControlStateNormal];
button.backgroundColor = [UIColor lightGrayColor];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

[self settingNavgationBar];
}
-(void)settingNavgationBar{

UINavigationBar *bar = self.navigationController.navigationBar;

// 设置navigationBar 的类型
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
// UIBarStyleDefault
// UIBarStyleBlack

// 透明度(毛玻璃)
bar.translucent = YES;
// 设置yes,子视图的坐标原点是(0,0)
// 设置no,子视图的坐标原点是(0,64)
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
label.backgroundColor = [UIColor greenColor];
[self.view addSubview:label];

// 设置导航条颜色
[bar setBarTintColor:[UIColor redColor]];

// 设置标题
// self.title = @"root";
self.navigationItem.title = @"root";

// 导航条按钮字体颜色
[bar setTintColor:[UIColor cyanColor]];

// 设置导航条字体颜色
[bar setTitleTextAttributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:20],
NSForegroundColorAttributeName:[UIColor purpleColor]
}];

// 设置隐藏导航条
// self.navigationController.navigationBarHidden = YES;

[bar setBackgroundImage:[UIImage imageNamed:@"header_bg44"] forBarMetrics:UIBarMetricsDefault];
// UIBarMetricsDefault 横竖屏都显示
// UIBarMetricsCompact 竖屏显示


// self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:secondVc action:@selector(abc)];

UIImage *image = [UIImage imageNamed:@"qq.png"];
// 对图片进处理
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// UIImageRenderingModeAutomatic,返回一个色块
// UIImageRenderingModeAlwaysOriginal,返回图片本身 UIImageRenderingModeAlwaysTemplate,返回一个色块

// UIBarButtonItemStylePlain,
// UIBarButtonItemStyleDone,

// leftBarButtonItem 左按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftClick)];
// rightBarButtonItem 右按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"right" style:UIBarButtonItemStyleDone target:self action:@selector(rightClick)];

UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"item1" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];

UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"item2" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];



UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 40, 40);
[button setBackgroundColor:[UIColor redColor]];

[button setImage:image forState:UIControlStateNormal];

// 添加一个自定义的UIButton
UIBarButtonItem * item3 = [[UIBarButtonItem alloc]initWithCustomView:button];

// 设置rightBarButtonItems时,会把rightBarButtonItem 覆盖
self.navigationItem.rightBarButtonItems = @[item1,item2,item3];

UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
label1.backgroundColor = [UIColor redColor];
label1.text = @"label";

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
imageView.image = image;
// 设置titleView
self.navigationItem.titleView = imageView;

}
-(void)leftClick{
NSLog(@"---");
}
-(void)rightClick{
NSLog(@"%s",__func__);
}
-(void)onClick{

SecondViewController *secondVc = [[SecondViewController alloc]init];
// 进入下一级(入栈)
[self.navigationController pushViewController:secondVc animated:YES];


}
@end


#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// 视图将要出现的时候,隐藏navgationBar
self.navigationController.navigationBarHidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
// 视图将要消失的时候,显示navgationBar
self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];



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

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 50);
[button setTitle:@"下一级" forState:UIControlStateNormal];
button.backgroundColor = [UIColor lightGrayColor];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(100, 200, 100, 50);
[button1 setTitle:@"上一级" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor lightGrayColor];
[button1 addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}

-(void)abc{
NSLog(@"----");
}
-(void)backClick{
// 返回上一级
[self.navigationController popViewControllerAnimated:YES];
}
-(void)onClick{
ThirdViewController *thirdVc = [[ThirdViewController alloc]init];
// 进入下一级(入栈)
[self.navigationController pushViewController:thirdVc animated:YES];


}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


@end

#import "ThirdViewController.h"
#import "ForthViewController.h"
@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
[super viewDidLoad];


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

for (int i = 0 ; i < 3; i ++) {

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100 + i * 60, 100, 50);
button.tag = i + 10;
if (i == 0 ) {
[button setTitle:@"下一级" forState:UIControlStateNormal];
}else if (i == 1){
[button setTitle:@"上一级" forState:UIControlStateNormal];
}else if (i == 2){
[button setTitle:@"返回root" forState:UIControlStateNormal];
}

button.backgroundColor = [UIColor lightGrayColor];
[button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];


}

// self.navigationController.viewControllers
NSLog(@"%@",self.navigationController.viewControllers);

[self toolBarSetting];
}

-(void)toolBarSetting{
self.navigationController.toolbarHidden = NO;

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];

UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];

UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];

UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


self.toolbarItems = @[item,item3,item1,item2];

}
-(void)onClick:(UIButton*)button{
if (button.tag == 10) {
ForthViewController *forthVc = [[ForthViewController alloc]init];
// 把forthVc 入栈
[self.navigationController pushViewController:forthVc animated:YES];
}
if (button.tag == 11) {
// 返回上一级
[self.navigationController popViewControllerAnimated:YES];
}
if (button.tag == 12) {
// 返回root(基栈)
[self.navigationController popToRootViewControllerAnimated:YES];
}
}


@end

#import "ForthViewController.h"

@interface ForthViewController ()

@end

@implementation ForthViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 设置背景色
[self.view setBackgroundColor:[UIColor orangeColor]];
// 初始化button
for (int i = 0 ; i < 3; i ++) {

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100 + i * 60, 100, 50);
button.tag = i + 10;
if (i == 0 ) {
[button setTitle:@"返回第二级" forState:UIControlStateNormal];
}else if (i == 1){
[button setTitle:@"上一级" forState:UIControlStateNormal];
}else if (i == 2){
[button setTitle:@"返回root" forState:UIControlStateNormal];
}

button.backgroundColor = [UIColor lightGrayColor];
[button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];


}

}
-(void)onClick:(UIButton*)button{
if (button.tag == 10) {
// 根据数组下标 获取要返回到的UIViewController
UIViewController *VC = self.navigationController.viewControllers[1];
// 返回到指定的UIViewController
[self.navigationController popToViewController:VC animated:YES];
}
if (button.tag == 11) {
// 返回上一级
[self.navigationController popViewControllerAnimated:YES];
}
if (button.tag == 12) {
// 返回到root(基栈)
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
@end

UIProgressView

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 "ViewController.h"

@interface ViewController ()
{
NSTimer *_timer;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 初始化UIProgressView并设置frame
// UIProgressView 高度是固定的
UIProgressView *progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
// 设置进度
// 0.0 .. 1.0, default is 0.0.
progressView.progress = 0.25;
// 已加载进度的颜色
progressView.progressTintColor = [UIColor redColor];
// 未加载进度的颜色
progressView.trackTintColor = [UIColor greenColor];
// 设置图片会覆盖上面设置的颜色
// 已加载进度的图片
progressView.progressImage = [UIImage imageNamed:@"1"];
// 未加载进度的图片
progressView.trackImage = [UIImage imageNamed:@"2"];

// 设置tag值
progressView.tag = 10;

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

// 初始化timer
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];

}
-(void)timerRun{
UIProgressView *progressView = (UIProgressView *)[self.view viewWithTag:10];

if (progressView.progress < 1.0) {
progressView.progress += 0.01;
}else{
NSLog(@"下载完成");
// 使计时器失效
[_timer invalidate];
}

}
@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
87
88
89
90
91
92
93
94
//
// myString.c
// 字符串函数
//
// Created by 刘晓磊 on 15/12/16.
// Copyright (c) 2015年 刘晓磊. All rights reserved.
//

#include "myString.h"

size_t myStrlen(const char * s)
{
size_t count = 0;
//参数合法性判断
if (NULL == s) {
printf("Input Param is invalid!\n");
return -1;
}

//因为字符串是以\0结尾的特殊常量,那么\0就可以作为一个结束标记
//通常对于字符串的操作都是采用while
while (*s != '\0') {
count++;
s++;
}

return count;
}

char * myStrcpy(char * dst, const char * src)
{
//需要一个指针变量,来保存dst的首地址
char * ptemp = NULL;
//参数合法性判断
if (NULL == dst || NULL == src) {
printf("Input Param is invalid!\n");
return NULL;
}

//保证传入的首地址变量里面的内容不要更改,始终保佑这个地址
ptemp = dst;
while (*src != '\0') {
*ptemp = *src;
ptemp++;
src++;
}
*ptemp = '\0';

return dst;
}

char * myStrcat(char * dst, const char * src)
{
char * ptemp = NULL;
//参数合法性判断
if (NULL == dst || NULL == src) {
printf("Input Param is invalid!\n");
return NULL;
}
ptemp = dst;

//1.找到dst的\0
while (*ptemp != '\0') {
ptemp++;
}
//找到之后,ptemp刚刚好停在'\0'的位置

while (*src != '\0') {
*ptemp = *src;
ptemp++;
src++;
}
*ptemp = '\0';

return dst;
}


int myStrcmp(const char * s1, const char * s2)
{
//参数合法性判断
if(NULL == s1 || NULL == s2) {
printf("Input param is invalid!\n");
exit(-1);
}
//找两个字符串不同的位置,以及'\0'的位置
//这里有一个条件不符合,我们都要跳出循环
while (*s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
s1++;
s2++;
}

return *s1 - *s2;
}