iOS - 使用进阶

时间:2023-12-01 18:46:50

1. 状态栏显示风火轮

//  ViewController.m

//  1.状态栏显示风火轮

//

//  Created by wind on 16/11/13.

//  Copyright © 2016年 wind. All rights reserved.

//

/*

功能:转太烂显示风火轮

作用:执行某个长时间动作时,提示用户耐心等待动作的执行

*/

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@

end

2. 隐藏顶部状态栏

  info.plist种操作,如下:

iOS - 使用进阶

3. 获取App的当前版本号

iOS - 使用进阶

4. 列出设备上所有字体

//实际工作中,经常需要调整界面元素的字体种类,本节课将为你输出系统提供的所有字体.

//方便你的检索和使用

//遍历设备中的所有字体

for (NSString *family in [UIFont familyNames]) {

//输出字体组的名称

NSLog(@"**********************%@",family);

//遍历字体组中所有的字体

for (NSString *font in [UIFont fontNamesForFamilyName:family]) {

//输出字体组中字体的名称

NSLog(@"\t----------------------------%@",font);

}

}

5. 对 App进行截屏

//1. 如何截屏 2.存入系统相册

//创建一个颜色对象,使用一张背景图片,作为颜色对象的内容

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg.jpeg"]];

//将进行平铺的图案,作为根视图的背景颜色

self.view.backgroundColor = background;

//在图形上下文中,渲染当前窗口根视图的层.

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

//将上下文中的内容,转换成图像对象.

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

//关闭上下文

UIGraphicsEndImageContext();

//保存至系统相册

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

6. 使用UIApplication打开网页

//使用应用程序单例对象,打开网页的功能

//获取应用程序单例对象,使用它的打开网址功能,打开网页.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];

7. 使用UIApplication拨打电话

//使用应用程序单例对象,拨打电话的功能

//1.创建一个字符串,注意它的前缀

NSString *string = @"tel:18860358995";

//2.转换成网址对象

NSURL *url = [NSURL URLWithString:string];

//3.获取应用程序单例对象,使用它的单例对象,使用它的打开网址功能,调出通信面板,需要真机测试

[[UIApplication sharedApplication] openURL:url];

8. 使用UIApplication发送短信

//使用应用程序单例对象, 发送短信的功能

//1. 创建一个字符串,注意它的前缀

NSString *string = @"sms:18860358995";

//2. 转换成网址对象

NSURL *url = [NSURL URLWithString:string];

[[UIApplication sharedApplication] openURL:url];

9. 邮件功能的使用

//使用应用程序单例对象, 发送邮件功能

NSString *string = @"mailto://tapinfinity@gmail.com";

NSURL *url = [NSURL URLWithString:string];

[[UIApplication sharedApplication] openURL:url];

10. 给方法添加标注

iOS - 使用进阶

11. 使用XCode的版本管理功能(SCM)

//系统的版本控制功能

//使用版本控制,可以方便地查看代码修改纪录,及恢复为之前的版本.

NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>Test SVN.");

//注:可以在左侧项目蓝,看到某个文件名城的右侧,有个[M]标志,表示该文件已经被编辑,但是尚未被提交 eg:ViewController.m

//1. 选择版本控制选项

//2. 选择提交代码文件选项,将该代码文件,提交给版本控制.

//3. 提交代码前,还需要给这次的版本提交,添加说明文字,作为提交日志以方便日后查看

// 点击提交按钮,提交代码后, 文件名称右侧的[M]标志,已经消失

//修改代码,例:添加一行代码

iOS - 使用进阶iOS - 使用进阶

iOS - 使用进阶

iOS - 使用进阶

12. 实现App的Settings设置

13. App图标上显示数字

//给应用程序图标,添加数字角标

[UIApplication sharedApplication].applicationIconBadgeNumber = 3;

14. 检测陀螺仪设备的状态

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//添加framework:CoreMotion

//功能:负责三种数据,加速度值,陀螺仪值,设备移动值

BOOL isGyroscopeAvailable = [self isGyroscopeAvailable];

NSLog(@">>>>>>>>>>%d",isGyroscopeAvailable);

}

-(BOOL)isGyroscopeAvailable {

//判断系统版本是否为4.0以上

#ifdef __IPHONE_4_0

//初始化管理动作对象

CMMotionManager *motionManager = [[CMMotionManager alloc] init];

//获得陀螺仪设备是否可用

BOOL gyroscopeAvailabel = motionManager.gyroAvailable;

return gyroscopeAvailabel;

//系统版本低于4.0

#else

return NO;

#endif

}

15. 检测定位设备的状态

/*

任何想要获得用户位置信息的应用,都会用到CoreLocation框架.可方便获取设备位置.

*/

//判断设备是否已经开启地理位置的服务

BOOL locationServicesEnabled = [CLLocationManager locationServicesEnabled];

//判断设备是否具备磁力感应,磁力感应可以控制地理位置的精确度

BOOL magnetometerAvailbale = [CLLocationManager headingAvailable];

NSLog(@">>>>>>>>locationServiceEnabled:%d",locationServicesEnabled);

NSLog(@">>>>>>>>locationServiceEnabled:%d",magnetometerAvailbale);

16. 检测前后相机设备的状态

//检测设备的后摄像头是否可用

BOOL cameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];

//检测设备的前摄像头是否可用

BOOL frontAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];

NSLog(@"后摄像头状态:%d",cameraAvailable);

NSLog(@"前摄像头状态:%d",frontAvailable);

17. 使用Quartz2D绘制基本图形

(1) 自定义QuartzView(继承UIView)

@implementation QuartzVew

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

// Drawing code

//重写父类方法

//1. 获得当前视图的上下文

CGContextRef context = UIGraphicsGetCurrentContext();

//2. 在图形上下文中,创建和设置背景填充颜色为黄色.

CGContextSetRGBFillColor(context, 1.0, 1.0, 0.0, 1.0);

//3. 使用黄色填充区域

CGContextFillRect(context, rect);

//4. 创建和设置边框颜色为黑色

CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);

//5. 设置画笔的线条粗细

CGContextSetLineWidth(context, 2.0);

//6. 设置矩形的位置及尺寸

CGRect rect2 = CGRectMake(100, 350, 100, 30);

//7. 将矩形边框添加至上下文,作为绘制矩形的准备工作

CGContextAddRect(context, rect2);

//8. 绘制举行边框

CGContextStrokePath(context);

//9. 设置画笔的线条粗细

CGContextSetLineWidth(context, 2.0);

//10. 设置填充颜色

CGContextSetRGBFillColor(context, 1.0, 0.0, 1.0, 1.0);

//11. 创建一个字体对象

UIFont *font = [UIFont boldSystemFontOfSize:31];

NSString *text = @"Hello";

//12. 设置文字的现实位置和尺寸

CGRect rect3 = CGRectMake(100, 120, 100, 30);

//13. 将文字绘制在指定的区域内

[text drawInRect:rect3 withFont:font];

//14. 设置画笔的线条粗细

CGContextSetLineWidth(context, 5.0);

//15. 设置线条样式

CGContextSetLineCap(context, kCGLineCapButt);

//16. 设置边框的颜色

CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);

//17. 创建一个坐标数组

CGPoint points[5];

//18. 设定坐标数组的内容

points[0] = CGPointMake(100, 200);

points[1] = CGPointMake(100, 300);

points[2] = CGPointMake(200, 300);

points[3] = CGPointMake(200, 200);

points[4] = CGPointMake(100, 200);

//19. 将坐标连线置入上下文

CGContextAddLines(context, points, 5);

//20. 执行绘制边框动作

CGContextStrokePath(context);

}

(2) 根视图控制器对应的类ViewDidLoad中:

//设定视图显示区域

CGRect rect = self.view.bounds;

//初始化自定义视图对象

QuartzVew *view = [[QuartzVew alloc] initWithFrame:rect];

//将视图添加至当前窗口的根视图

[self.view addSubview:view];

//////////////////////////////////////////////////////////////////////////

18. App图标名称的国际化

/*

1.点击项目名称,进入项目设置面板

2.切换简体中文

* *实现通过修改多语言环境下的配置文件,实现程序名称的国际化

*/

//////////////////////////////////////////////////////////////////////////

19. App内容文本的国际化

//通过一个宏,查找国际化资源文件中,键值标签所指向的字符串

NSString *str = NSLocalizedString(@"label", @"");

//创建一个标签对象,用老显示获得的字符串

UILabel *label = [[UILabel alloc] init];

label.frame = CGRectMake(130, 100, 250, 60);

label.text = str;

[self.view addSubview:label];

20. 计算两个日期间的差值

//计算日期间的差值

NSDate *dateA = [NSDate date];

NSDate *dateB = [[NSDate alloc] initWithTimeInterval:-60*60*24*1000 sinceDate:[NSDate date]];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMoviesDirectory | NSDayCalendarUnit

fromDate:dateA

toDate:dateB

options:0];

NSLog(@"Different in date components: %i/%i/%i",components.day,components.month,components.year);

21. 正则表达式的使用

//检测邮箱格式的正确性

NSString *str = @"1983457078@gmail.com";

//创建一个正则表达式类,帮助您检测字符串,是否匹配正则表达式.

NSRegularExpression *regexEmail = [NSRegularExpression regularExpressionWithPattern:@"^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w{2,3}){1,3})$" options:0 error:nil];

//返回第一个匹配的结果

NSTextCheckingResult *isEmail = [regexEmail firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];

//根据结果做相应的处理

if (isEmail) {

UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Perfect!" message:@"It's an email" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];

[alertview show];

}else {

UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Sorry." message:@"It's not an email" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];

[alertview show];

}

22. 本地同志的使用

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//本地通知,进行应用程序的本地同志

//创建一个本地同志对象

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

NSDate *now = [NSDate date];

//设置在当前时间的2秒后,触发本地通知

localNotification.fireDate = [now dateByAddingTimeInterval:2];

//设置重复次数

localNotification.repeatInterval = 0;

//设置时区:默认

localNotification.timeZone = [NSTimeZone defaultTimeZone];

//设置通知的提醒声音模版.

localNotification.soundName = UILocalNotificationDefaultSoundName;

//设置通知的文本内容

localNotification.alertBody = @"Hi,it's time to make a decision!";

//设置程序图标右上角显示的数字

localNotification.applicationIconBadgeNumber = 1;

//创建一个字典对象,用来传递数据

NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"infoValue"  forKey:@"infoKey"];

localNotification.userInfo = infoDic;

//开始定时执行通知,点击切换至项目代理文件.

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}

- (void)applicationWillResignActive:(UIApplication *)application {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

//将程序右上角的数字减1

application.applicationIconBadgeNumber -= 1;

}

//新建一个代理方法,用来处理客户点击通知后的时间

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"HURRY UP" message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alertview show];

//获得传递来的字典对象

NSDictionary *dic = notification.userInfo;

//输出传递的内容

NSLog(@"The value of infoKey:%@",[dic objectForKey:@"infoKey"]);

//将程序图标右上角数字减1

application.applicationIconBadgeNumber -= 1;

}

23. 使用Application Loader发布应用