记录
iOS
常用代码片段。
判断当前网络状态 Demo
1 | /** 网络状态枚举 */ |
屏幕亮度渐变 Demo
在
didFinishLaunchingWithOptions:
保存默认亮度值。1
2
3[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:NO]
forKey:IS_NEED_CHANGE_BRIGHTNESS];
[GradualBrightness sySaveDefaultBrightness];在
applicationWillResignActive:
恢复默认亮度值(如果需要)。1
2
3
4
5
6NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
BOOL isNeedChangeBrightness = [[userDefault objectForKey:IS_NEED_CHANGE_BRIGHTNESS] boolValue];
if (YES == isNeedChangeBrightness)
{
[GradualBrightness syResumeBrightness];
}在
applicationDidBecomeActive:
设定新亮度值(如果需要)。1
2
3
4
5
6NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
BOOL isNeedChangeBrightness = [[userDefault objectForKey:IS_NEED_CHANGE_BRIGHTNESS] boolValue];
if (YES == isNeedChangeBrightness)
{
[GradualBrightness syConfigBrightness:CUSTOM_BRIGHTNESS_VAULE];
}在
viewWillAppear:
设定新亮度值(同时设为需要)。1
2
3[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES]
forKey:IS_NEED_CHANGE_BRIGHTNESS];
[GradualBrightness syConfigBrightness:CUSTOM_BRIGHTNESS_VAULE];在
viewWillDisappear:
恢复默认亮度值(同时设为不需要)。1
2
3[GradualBrightness syResumeBrightness];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:NO]
forKey:IS_NEED_CHANGE_BRIGHTNESS];实现代码
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/** 亮度变换步幅 */
#define GRADUAL_STRIDE 0.005f
static CGFloat defaultBrightness;
static NSOperationQueue *changeBrightnessQueue;
#pragma mark -- 保存默认亮度值
+ (void)sySaveDefaultBrightness
{
defaultBrightness = [UIScreen mainScreen].brightness;
}
#pragma mark -- 逐步设置亮度
+ (void)syConfigBrightness:(CGFloat)value
{
if (!changeBrightnessQueue)
{
changeBrightnessQueue = [[NSOperationQueue alloc] init];
changeBrightnessQueue.maxConcurrentOperationCount = 1;
}
[changeBrightnessQueue cancelAllOperations];
CGFloat brightness = [UIScreen mainScreen].brightness;
CGFloat stride = GRADUAL_STRIDE * ((value > brightness) ? 1 : -1);
NSInteger times = fabs((value - brightness) / GRADUAL_STRIDE);
for (NSInteger i = 1; i <= times; i++)
{
[changeBrightnessQueue addOperationWithBlock:^{
[NSThread sleepForTimeInterval:1 / 180.0];
[UIScreen mainScreen].brightness = brightness + i * stride;
}];
}
}
#pragma mark -- 逐步恢复亮度
+ (void)syResumeBrightness
{
[self syConfigBrightness:defaultBrightness];
}