003-iOS10新特性

一、语音识别

1.简介

苹果官方在iOS10文档中新增了API Speech,方便我们处理语音识别

Speech具备以下特点:

可以实现连续的语音识别

可以对语音文件或者语音流进行识别

最佳化自由格式的听写(可理解为多语言支持)和搜索式的字符串

2.使用步骤

1)info.plist配置

2)核心代码

 1 #import "ViewController.h"
 2 #import <Speech/Speech.h>  // 语音识别头文件
 3 
 4 @interface ViewController ()
 5 
 6 @end
 7 
 8 @implementation ViewController
 9 
10 - (void)viewDidLoad
11 {
12     [super viewDidLoad];
13     
14     [self initUI];  // 界面
15 }
16 
17 #pragma mark - 界面
18 - (void)initUI
19 {
20     // 1.创建本地化标识符
21     NSLocale *local = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
22     
23     // 2.创建一个语音识别对象
24     SFSpeechRecognizer *sf = [[SFSpeechRecognizer alloc] initWithLocale:local];
25     
26     // 3.将bundle 中的资源文件加载出来返回一个url
27     NSURL *url =[[NSBundle mainBundle] URLForResource:@"你若懂我,该有多好.mp3" withExtension:nil];
28     NSLog(@"%@", url);
29     
30     // 4.将资源包中获取的url传递给request对象
31     SFSpeechURLRecognitionRequest *res = [[SFSpeechURLRecognitionRequest alloc] initWithURL:url];
32     
33     // 5.发送一个请求
34     [sf recognitionTaskWithRequest:res resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
35         if (error!=nil) {
36             NSLog(@"语音识别解析失败,%@",error);
37         } else {
38             //解析正确
39             NSLog(@"---%@", result.bestTranscription.formattedString);
40         }
41     }];
42 }
43 
44 @end

 

二、UITabBarController中的方法改进

1.简介

在iOS10之前,tabBarItem上的文字颜色,默认是蓝色,上面的新消息提醒数字badge默认是红色的,未选中的TabBarItem的文字颜色默认是黑色的,我们修改的话,也只能修改它的默认颜色 ,其它的就不能进行个性化定制,使用起来非常的不方便,iOS10之后我们可以轻松个性化定制了

2.核心代码

 1 #import "AppDelegate.h"
 2 #import "OneController.h"
 3 #import "TwoController.h"
 4 #import "ThreeController.h"
 5 
 6 @interface AppDelegate ()
 7 
 8 @end
 9 
10 @implementation AppDelegate
11 
12 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
13 {
14     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
15     self.window.backgroundColor = [UIColor blackColor];
16     [self.window makeKeyAndVisible];
17     
18     // 初始化三个根视图控制器
19     OneController *oneVC = [[OneController alloc] init];
20     oneVC.tabBarItem.title = @"首页";
21     oneVC.tabBarItem.badgeValue =@"90";
22     TwoController *twoVC = [[TwoController alloc] init];
23     twoVC.tabBarItem.title = @"圈子";
24     ThreeController *threeVC = [[ThreeController alloc] init];
25     threeVC.tabBarItem.title = @"社交";
26     
27     // 初始化UITabBarController
28     UITabBarController *tabBarVC = [[UITabBarController alloc] init];
29     tabBarVC.viewControllers = @[oneVC, twoVC, threeVC];
30 
31     // 改变tabbar上面的文字默认颜色
32     oneVC.tabBarController.tabBar.tintColor = [UIColor redColor];
33     twoVC.tabBarController.tabBar.tintColor = [UIColor redColor];
34     threeVC.tabBarController.tabBar.tintColor = [UIColor redColor];
35     // tabBarVC.tabBar.tintColor = [UIColor redColor];
36     
37     // 使用iOS10新推出的修改tabbar未选中的tintColor颜色
38     // 在iOS10之前的版本默认tabbar未选中的tintColor颜色为黑色
39     oneVC.tabBarController.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];
40     twoVC.tabBarController.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];
41     threeVC.tabBarController.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];
42     // tabBarVC.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];
43 
44     // 数字提醒的颜色(文本颜色、文本背景颜色)
45     // 在iOS10之前的版本默认文本背景颜色为红色
46     // oneVC.tabBarItem.badgeColor = [UIColor orangeColor];
47     // 在iOS10之前的版本默认文本颜色为白色
48     // [oneVC.tabBarItem setBadgeTextAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateNormal];
49     for (UITabBarItem *item in tabBarVC.tabBar.items) {
50         // 改变tabbar上面的数字提醒的文本背景颜色
51         [item setBadgeColor:[UIColor orangeColor]];
52         // 改变tabbar上面的数字提醒的文本颜色
53         [item setBadgeTextAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateNormal];
54         // 改变tabbar上面的文字大小
55         [item setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} forState:UIControlStateNormal];
56     }
57     
58     self.window.rootViewController = tabBarVC;
59     
60     return YES;
61 }
62 
63 @end

 

 

三、UIViewPropertyAnimator属性动画器

1.简介

在iOS10之前,我们使用UIView做动画效果或者定义一些Layer的动画,如果开始了,一般无法进行停止操作更不能暂停操作,而且一些非常复杂的动画处理起来也比较麻烦;但是在iOS10,苹果推出了一个全新的API--UIViewProertyAnimator,可供我们处理动画操作

UIViewPropertyAnimator是iOS10中新增的一个执行View动画的类,具有以下特点:

1)可中断性

2)可擦除

3)可反转性

4)丰富的动画时间控制功能

2.核心代码

  1 #import "OneController.h"
  2 
  3 @interface OneController ()
  4 
  5 @property (nonatomic, strong) UIView *myView;
  6 @property (nonatomic, strong) UIViewPropertyAnimator *propertyAnimator; // 属性动画器
  7 
  8 @end
  9 
 10 @implementation OneController
 11 
 12 - (void)viewDidLoad
 13 {
 14     [super viewDidLoad];
 15 
 16     [self initUI]; // 界面
 17 }
 18 
 19 #pragma mark - 界面
 20 - (void)initUI
 21 {
 22     self.view.backgroundColor = [UIColor cyanColor];
 23     
 24     // btn
 25     CGFloat spacing = 20;
 26     CGFloat btnY = 50;
 27     CGFloat btnW = (self.view.bounds.size.width - spacing * 5) / 4.0;
 28     CGFloat btnH = 35;
 29     NSArray *texts = @[@"开始动画", @"暂停动画", @"继续动画", @"停止动画"];
 30     for (int i = 0; i < 4; i++) {
 31         CGFloat btnX = spacing + (btnW + spacing) * i;
 32         UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
 33         btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
 34         [btn setTitle:texts[i] forState:UIControlStateNormal];
 35         btn.titleLabel.font = [UIFont systemFontOfSize:16];
 36         btn.tintColor = [UIColor whiteColor];
 37         btn.backgroundColor = [UIColor redColor];
 38         btn.layer.cornerRadius = 5;
 39         btn.layer.masksToBounds = YES;
 40         btn.tag = 10 + i;
 41         [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
 42         [self.view addSubview:btn];
 43     }
 44     
 45     // myView
 46     _myView =[[UIView alloc] initWithFrame:CGRectMake(50, 120, 100, 100)];
 47     _myView.backgroundColor =[UIColor yellowColor];
 48     [self.view addSubview:_myView];
 49 }
 50 
 51 #pragma mark - 点击事件
 52 // 开始/暂停/继续/停止
 53 - (void)btnClick:(UIButton *)sender
 54 {
 55     switch (sender.tag - 10) {
 56         case 0:
 57         {
 58             // 开始
 59             _propertyAnimator = [UIViewPropertyAnimator runningPropertyAnimatorWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
 60                 // 使用属性动画器改变myView的frame
 61                 _myView.frame = CGRectMake(150, 450, 250, 50);
 62             } completion:nil];
 63         }
 64             break;
 65         case 1:
 66             // 暂停
 67             [_propertyAnimator pauseAnimation];
 68             break;
 69         case 2:
 70             // 继续
 71             // 第一种继续动画的方法:startAnimation
 72             [_propertyAnimator startAnimation];
 73             
 74             // 第二种继续动画的方法:continueAnimation
 75             // 相较于第一种方法,第二种方法可以重新设置动画效果
 76             // 类UICubicTimingParameters和类UISpringTimingParameters均遵循协议UITimingCurveProvider
 77             // 1.类UICubicTimingParameters(普通动画效果)
 78             // UIViewAnimationCurve枚举值
 79             // UIViewAnimationCurveEaseInOut   // 快进快出
 80             // UIViewAnimationCurveEaseIn        // 快进渐出
 81             // UIViewAnimationCurveEaseOut     // 渐进快出
 82             // UIViewAnimationCurveLinear        // 线性
 83             // @property (nonatomic, readonly) UIViewAnimationCurve animationCurve;
 84             // @property (nonatomic, readonly) CGPoint controlPoint1;  // 控制点1(取值范围0~1.0)
 85             // @property (nonatomic, readonly) CGPoint controlPoint2; // 控制点2(取值范围0~1.0)
 86             // - (instancetype)initWithAnimationCurve:(UIViewAnimationCurve)curve;
 87             // - (instancetype)initWithControlPoint1:(CGPoint)point1 controlPoint2:(CGPoint)point2;
 88             // 2.类UISpringTimingParameters(弹簧动画效果)
 89             // @property(nonatomic, readonly) CGVector initialVelocity;
 90             // - (instancetype)initWithDampingRatio:(CGFloat)ratio initialVelocity:(CGVector)velocity;
 91             // - (instancetype)initWithDampingRatio:(CGFloat)ratio;
 92             // UISpringTimingParameters *springTP = [[UISpringTimingParameters alloc] initWithDampingRatio:0.2 initialVelocity:CGVectorMake(1, 1)];
 93             // UICubicTimingParameters *cubicTP = [[UICubicTimingParameters alloc] initWithAnimationCurve:UIViewAnimationCurveLinear];
 94             // 重新设置动画效果和动画总时长,继续动画
 95             // [_propertyAnimator continueAnimationWithTimingParameters:parameters durationFactor:30];
 96             break;
 97         case 3:
 98             // 停止
 99             [_propertyAnimator stopAnimation:YES];
100             break;
101         default:
102             break;
103     }
104 }
105 
106 @end

 

四、iOS10隐私权限设置

1.简介

iOS10开始对隐私权限更加严格,如果不去设置就会直接崩溃,现在很多遇到崩溃问题,一般解决办法都是在info.plist文件中添加对应的key-value就可以啦

2.info.plist中去设置权限

注意:对应key的value不可以为空

 

五、Xcode8运行一堆没用的logs解决方法

 

六、iOS10字体随着手机系统字体改变而改变

当我们手机的系统字体发生改变之后,在iOS10之前我们需要做很多工作使我们的App的label也会跟着一起变化,但是在iOS10中提供了一个label的属性:adjustsFontForContentSizeCategory来设置

 

七、iOS10的UIScrollView

在iOS10之后,UIScrollView新增refreshControl,只要是继承自UIScrollView的控件都有刷新功能

 

八、iOS10判断系统版本

[[UIDevice currentDevice] systemVersion].floatValue,

[[UIDevice currentDevice] systemVersion]

 

九、iOS10开始项目中有的文字显示不全问题

英文字母会不会也有这种问题,我又通过测试,后来发现英文字母没有问题,只有汉字有问题。目前只有一个一个修改控件解决这个问题,暂时没有其他好办法来解决

 

posted @ 2017-11-16 09:52  Frank9098  阅读(211)  评论(0)    收藏  举报