容易被忽视的API总结

来源:互联网 发布:数据报表图片 编辑:程序博客网 时间:2024/06/05 06:29

1.使用UIScrollViewKeyboardDismissMode实现了Message app的行为

像Messages app一样在滚动的时候可以让键盘消失是一种非常好的体验。然而,将这种行为整合到你的app很难。幸运的是,苹果给UIScrollView添加了一个很好用的属性keyboardDismissMode,这样可以方便很多。

现在仅仅只需要在Storyboard中改变一个简单的属性,或者增加一行代码,你的app可以和办到和Messages app一样的事情了。

这个属性使用了新的UIScrollViewKeyboardDismissMode enum枚举类型。这个enum枚举类型可能的值如下:

  1. UIScrollViewKeyboardDismissModeNone        // the keyboard is not dismissed automatically when scrolling 
  2. UIScrollViewKeyboardDismissModeOnDrag      // dismisses the keyboard when a drag begins 
  3. UIScrollViewKeyboardDismissModeInteractive // the keyboard follows the dragging touch off screen, and may be 
  4.  pulled upward again to cancel the dismiss 


2.使用Core Image来检测眨眼以及微笑

iOS给Core Image增加了两种人脸检测功能:CIDetectorEyeBlink以及CIDetectorSmile。这也就是说你现在可以在照片中检测微笑以及眨眼。

以下是在app中使用它的方法:

  1. UIImage *image = [UIImage imageNamed:@"myImage"]; 
  2. CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace 
  3.                                           context:nil 
  4.                                           options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}]; 
  5.   
  6. NSDictionary *options = @{ CIDetectorSmile: @YES, CIDetectorEyeBlink: @YES }; 
  7.   
  8. NSArray *features = [detector featuresInImage:image.CIImage options:options]; 
  9.   
  10. for (CIFaceFeature *feature in features) { 
  11.     NSLog(@”Bounds: %@”, NSStringFromCGRect(feature.bounds)); 
  12.   
  13.     if (feature.hasSmile) { 
  14.     NSLog(@”Nice smile!”); 
  15.     } else { 
  16.     NSLog(@”Why so serious?”); 
  17.     } 
  18.     if (feature.leftEyeClosed || feature.rightEyeClosed) { 
  19.     NSLog(@”Open your eyes!”); 
  20.     } 

3.给UITextView增加了链接

现在在iOS添加你自己的Twitter账户更加简单了,现在你可以给一个NSAttributedString增加链接了,然后当它被点击的时候唤起一个定制的action。

首先,创建一个NSAttributedString然后增加给它增加一个NSLinkAttributeName 属性,见以下:

  1. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@”This is an example by @marcelofabri_”]; 
  2. [attributedString addAttribute:NSLinkAttributeName 
  3.                          value:@"username://marcelofabri_" 
  4.                          range:[[attributedString string] rangeOfString:@”@marcelofabri_”]]; 
  5.   
  6.   
  7. NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor], 
  8.                                  NSUnderlineColorAttributeName: [UIColor lightGrayColor], 
  9.                                  NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)}; 
  10.   
  11. // assume that textView is a UITextView previously created (either by code or Interface Builder) 
  12. textView.linkTextAttributes = linkAttributes; // customizes the appearance of links 
  13. textView.attributedText = attributedString; 
  14. textView.delegate = self; 

这样就可以让链接在文本中显示。然而,你也可以控制当链接被点击的时候会发生什么,实现这个可以使用UITextViewDelegate协议的新的shouldInteractWithURL方法,就像这样:

  1. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 
  2.     if ([[URL scheme] isEqualToString:@”username”]) { 
  3.         NSString *username = [URL host]; 
  4.         // do something with this username 
  5.         // … 
  6.         return NO; 
  7.     } 
  8.     return YES; // let the system open this URL 

4.设置UIImage的渲染模式:UIImage.renderingMode

着色(Tint Color)是iOS7界面中的一个重大改变,你可以设置一个UIImage在渲染时是否使用当前视图的Tint Color。UIImage新增了一个只读属性:renderingMode,对应的还有一个新增方 法:imageWithRenderingMode:,它使用UIImageRenderingMode枚举值来设置图片的renderingMode属 性。该枚举中包含下列值:

  1. UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。 
  2. UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。 
  3. UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。 

renderingMode属性的默认值是UIImageRenderingModeAutomatic,即UIImage是否使用Tint Color取决于它显示的位置。其他情况可以看下面的图例

以下的代码说明了使用一个既定的rendering模式创建图片是多么简单:

  1. UIImage *img = [UIImage imageNamed:@"myimage"]; 
  2. img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 

5.tintcolor VS barTintColor

有些类,比如说UINaviagtionBar,UISearchBar,UITabBar以及UIToolbar已经有了这么命名的属性。他们现在有了一个新的属性:barTintColor。

为了避免使用新属性的时候犯错误,如果你的appp需要支持iOS6以前的系统的时候,请检查一下。

  1. UINavigationBar *bar = self.navigationController.navigationBar; 
  2. UIColor *color = [UIColor greenColor]; 
  3. if ([bar respondsToSelector:@selector(setBarTintColor:)]) { // iOS 7+ 
  4.     bar.barTintColor = color; 
  5. else { // what year is this? 2012? 
  6.     bar.tintColor = color; 
0 0
原创粉丝点击