容易被忽视的API总结
来源:互联网 发布:数据报表图片 编辑:程序博客网 时间:2024/06/05 06:29
1.使用UIScrollViewKeyboardDismissMode实现了Message app的行为
像Messages app一样在滚动的时候可以让键盘消失是一种非常好的体验。然而,将这种行为整合到你的app很难。幸运的是,苹果给UIScrollView添加了一个很好用的属性keyboardDismissMode,这样可以方便很多。
现在仅仅只需要在Storyboard中改变一个简单的属性,或者增加一行代码,你的app可以和办到和Messages app一样的事情了。
这个属性使用了新的UIScrollViewKeyboardDismissMode enum枚举类型。这个enum枚举类型可能的值如下:
- UIScrollViewKeyboardDismissModeNone // the keyboard is not dismissed automatically when scrolling
- UIScrollViewKeyboardDismissModeOnDrag // dismisses the keyboard when a drag begins
- UIScrollViewKeyboardDismissModeInteractive // the keyboard follows the dragging touch off screen, and may be
- pulled upward again to cancel the dismiss
2.使用Core Image来检测眨眼以及微笑
iOS给Core Image增加了两种人脸检测功能:CIDetectorEyeBlink以及CIDetectorSmile。这也就是说你现在可以在照片中检测微笑以及眨眼。
以下是在app中使用它的方法:
- UIImage *image = [UIImage imageNamed:@"myImage"];
- CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
- context:nil
- options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];
- NSDictionary *options = @{ CIDetectorSmile: @YES, CIDetectorEyeBlink: @YES };
- NSArray *features = [detector featuresInImage:image.CIImage options:options];
- for (CIFaceFeature *feature in features) {
- NSLog(@”Bounds: %@”, NSStringFromCGRect(feature.bounds));
- if (feature.hasSmile) {
- NSLog(@”Nice smile!”);
- } else {
- NSLog(@”Why so serious?”);
- }
- if (feature.leftEyeClosed || feature.rightEyeClosed) {
- NSLog(@”Open your eyes!”);
- }
- }
3.给UITextView增加了链接
现在在iOS添加你自己的Twitter账户更加简单了,现在你可以给一个NSAttributedString增加链接了,然后当它被点击的时候唤起一个定制的action。
首先,创建一个NSAttributedString然后增加给它增加一个NSLinkAttributeName 属性,见以下:
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@”This is an example by @marcelofabri_”];
- [attributedString addAttribute:NSLinkAttributeName
- value:@"username://marcelofabri_"
- range:[[attributedString string] rangeOfString:@”@marcelofabri_”]];
- NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
- NSUnderlineColorAttributeName: [UIColor lightGrayColor],
- NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
- // assume that textView is a UITextView previously created (either by code or Interface Builder)
- textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
- textView.attributedText = attributedString;
- textView.delegate = self;
这样就可以让链接在文本中显示。然而,你也可以控制当链接被点击的时候会发生什么,实现这个可以使用UITextViewDelegate协议的新的shouldInteractWithURL方法,就像这样:
- - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
- if ([[URL scheme] isEqualToString:@”username”]) {
- NSString *username = [URL host];
- // do something with this username
- // …
- return NO;
- }
- return YES; // let the system open this URL
- }
4.设置UIImage的渲染模式:UIImage.renderingMode
着色(Tint Color)是iOS7界面中的一个重大改变,你可以设置一个UIImage在渲染时是否使用当前视图的Tint Color。UIImage新增了一个只读属性:renderingMode,对应的还有一个新增方 法:imageWithRenderingMode:,它使用UIImageRenderingMode枚举值来设置图片的renderingMode属 性。该枚举中包含下列值:
- UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
- UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。
- UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。
renderingMode属性的默认值是UIImageRenderingModeAutomatic,即UIImage是否使用Tint Color取决于它显示的位置。其他情况可以看下面的图例
以下的代码说明了使用一个既定的rendering模式创建图片是多么简单:
- UIImage *img = [UIImage imageNamed:@"myimage"];
- img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
5.tintcolor VS barTintColor
有些类,比如说UINaviagtionBar,UISearchBar,UITabBar以及UIToolbar已经有了这么命名的属性。他们现在有了一个新的属性:barTintColor。
为了避免使用新属性的时候犯错误,如果你的appp需要支持iOS6以前的系统的时候,请检查一下。
- UINavigationBar *bar = self.navigationController.navigationBar;
- UIColor *color = [UIColor greenColor];
- if ([bar respondsToSelector:@selector(setBarTintColor:)]) { // iOS 7+
- bar.barTintColor = color;
- } else { // what year is this? 2012?
- bar.tintColor = color;
- }
- 容易被忽视的API总结
- TextView容易被忽视的用法总结
- js中容易被忽视的事件问题总结
- 容易被忽视的synchronized
- j2se关键又容易被忽视总结
- 容易忽视但是功能灰常强大的Java API
- 容易忽视但是功能灰常强大的Java API
- 容易忽视但是功能灰常强大的Java API
- 容易被忽视的选房要素
- str_replace 容易被忽视的一点
- 容易被忽视的一个Access关键字
- Vi 编辑器容易被忽视的细节
- str_replace 容易被忽视的一点
- 问题的本身往往容易被忽视
- 一个容易被忽视的 strlen 细节
- 容易被忽视的一些PHP问题
- C++容易被忽视的基础知识
- 容易被忽视的心理细节
- C和C++混合编程FAQ
- android 制作.9.png图片
- WEB前端 学习手札
- 面试题2
- tatty
- 容易被忽视的API总结
- 数据库设计小结
- 完美世界笔试题
- Ubuntu 16.04下jdk和android studio的安装和出现问题的解决办法
- Oracle 表空间
- MySQL的表分区详解 - 查看分区数据量,查看全库数据量
- c++作业4
- 有序线性表的基本操作及实现
- 关于Volly框架使用的一些心得