What’s New in Cocoa Touch


WWDC 2011的视频真心不错,身为iOS开发者必看,但是光看也记不住什么,就发个博文一方面当做笔记,一方面与懒得看视频的人分享一下其中的部分内容.

虽然很久以前就把Session 100看完了,但还是决定先用这个练练手,内容我尽量覆盖全,并且提供一些我测试的例子,也会贴些图.写过博客的都知道贴图是很繁琐的,所以请原谅的我的懒惰,我真的没有那么充裕的时间去截很多图.

WWDC 2011视频以及 Keynote(PDF格式)下载详见这里http://developer.apple.com/videos/(需要注册个开发者账号,但是不是交$99那种,“野生"开发者即可)


UIStepper

UIStepper

一个新控件,附带很少的属性,和UIButton用法差不多:

1
2
3
4
5
6
7
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(44, 44, 100, 44)];
[self.view addSubview:stepper];
[stepper addTarget:self action:@selector(stepperTouched:) forControlEvents:UIControlEventTouchUpInside];
 
- (void)stepperTouched:(id)sender {
NSLog(@"stepper touched");
}

UIAlertView

终于可以在AlerView里输入文字了,真是佩服iOS5之前那些牛人怎么想到AlertView的各种使用技巧的.

1
2
3
4
5
6
7
8
typedef enum {
      UIAlertViewStyleDefault = 0,
      UIAlertViewStyleSecureTextInput,
      UIAlertViewStylePlainTextInput,
      UIAlertViewStyleLoginAndPasswordInput
  } UIAlertViewStyle;
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle;
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex

UIScreenOverscanCompensation

跟外接屏幕有关,本人目前为止还没遇到使用这个功能的情景,不多评论

1
2
3
4
5
6
7
typedef enum {
     UIScreenOverscanCompensationScale,
     UIScreenOverscanCompensationInsetBounds,
     UIScreenOverscanCompensationInsetApplicationFrame
 } UIScreenOverscanCompensation;
 @property(nonatomic) UIScreenOverscanCompensation
 overscanCompensation;

UIScreen

可以像iOS一样调节亮度了,两个新属性:

1
2
@property (nonatomic) CGFloat brightness;
@property (nonatomic) BOOL wantsSoftwareDimming;

结合之前的UIStepper使用:

1
2
3
4
5
6
7
8
9
10
11
12
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(44, 44, 100, 44)];
[stepper setMaximumValue:2.0];
[stepper setMinimumValue:-2.0];
[stepper setStepValue:0.1];
[self.view addSubview:stepper];
[stepper addTarget:self action:@selector(stepperTouched:) forControlEvents:UIControlEventTouchUpInside];
[[UIScreen mainScreen] setWantsSoftwareDimming:YES];
 
- (void)stepperTouched:(id)sender {
    NSLog(@"stepper touched with value %f", [(UIStepper *)sender value]);
    [[UIScreen mainScreen] setBrightness:[(UIStepper *)sender value]];
}

brightness范围在0.0-1.0,wantsSoftwareDimming是使用软件模拟来获得更低的亮度,但是开启会损失性能,本人实测似乎没有效果,或许是方法不对.鉴于不太实用,暂且搁置.

 

UIScrollView delegate

新增一个delegate:

1
2
3
- (void)scrollViewWillEndDragging:(UIScrollView *)sv
       withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)tCO;

velocity是拖动的手指离开屏幕时ScrollView的移动速度,targetContentOffset是scroll停止的位置,通过拦截这个delegate我们就可以设置Scroll操作停止的位置,而不只是任由用户控制.比如下图,我想要在用户Scroll时可以让ScrollView停止在每格的正中间,就可以通过这个Delegate设置

UIScrollViewDelegate

UIReferenceLibraryViewController

可以调用iOS5自带的字典,这个详细的参考文档吧

UITableView(Controller)/UISplitViewController

新增了一些delegate

1
2
3
4
5
6
7
8
9
10
11
12
- (BOOL)splitViewController:(UISplitViewController *)svc
   shouldHideViewController:(UIViewController *)vc
   inOrientation:(UIInterfaceOrientation)orientation;
 
- (BOOL)tableView:(UITableView *)tv
       shouldShowMenuForRowAtIndexPath:(NSIndexPath *)ip;
 - (BOOL)tableView:(UITableView *)tv
  canPerformAction:(SEL)action
 forRowAtIndexPath:(NSIndexPath *)ip;
 - (void)tableView:(UITableView *)tv
     performAction:(SEL)action
 forRowAtIndexPath:(NSIndexPath *)indexPath;

UITableViewCell可以通过NIB文件重用了

1
2
 - (void)registerNib:(UINib *)nib
 forCellReuseIdentifier:(NSString *)identifier;

关于TableView的详细内容还要等到Session 125 UITableView Changes, Tips & Tricks

iPad Keyboard相关

UIWindow keyboard notifications,iPad的屏幕键盘可以移动,可以分成两块,下面两个就是支持这些操作的

1
2
NSString *const UIKeyboardWillChangeFrameNotification;
NSString *const UIKeyboardDidChangeFrameNotification;

UIImage

新增两个工厂方法:

1
2
3
4
5
6
+ (UIImage *)animatedImageNamed:(NSString *)name
                       duration:(NSTimeInterval)duration;
+ (UIImage *)animatedImageWithImages:(NSArray *)images
                            duration:(NSTimeInterval)duration;
@property (nonatomic, readonly) NSArray *images;
@property (nonatomic, readonly) NSTimeInterval duration;

可以把多个图片放到一个UIImage里,显示时就可以顺序切换图片,和gif动画效果一样.

此外还有:

1
2
3
4
-(UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)i;
+(UIImage *)animatedResizableImageNamed:(NSString *)name
            capInsets:(UIEdgeInsets)capInsets
            duration:(NSTimeInterval)duration;

设置一个图片的缩放范围,比如:可以不缩放外围的圆角,而只缩放中间部分,这个详细的看文档吧.

StoryBoard

简单的说就是可以直接在InterfaceBuilder里面添加ViewController之间切换流程,App速成更方便了. 具体内容在Session 309 Introducing Interface Builder Storyboarding

Appearance Proxy(UIAppearance protocol)

可以为某一种控件设置样式,对App中所有相同控件有效

1
[[UISlider appearance] setMinimumTrackTintColor:[UIColor redColor]];

具体内容在Session 114 Customizing the Appearance of UIKit Controls

UIPageViewController

可以像iBooks那样翻页了,效果和iBooks相同没有任何缩水

Creating
1
2
3
4
5
6
7
- (id)initWithTransitionStyle:
               (UIPageViewControllerTransitionStyle)style
         navigationOrientation:
(UIPageViewControllerNavigationOrientation)orientation options:(NSDictionary *)options;
 
 id <UIPageViewControllerDelegate> delegate;
 id <UIPageViewControllerDataSource> dataSource;
UIPageViewControllerDelegate
1
2
3
4
5
6
7
- (void)pageViewController:(UIPageViewController *)pvc
         didFinishAnimating:(BOOL)finished
    previousViewControllers:(NSArray *)prevViewCs
        transitionCompleted:(BOOL)completed;
- (UIPageViewControllerSpineLocation)
                   pageViewController:(...)pvc
 spineLocationForInterfaceOrientation:(...)o;
UIPageViewControllerDataSource
1
2
3
4
5
6
- (UIViewController *)
          pageViewController:(UIPageViewController *)pvc
 viewControllerBeforeViewController:(UIViewController*)c;
 - (UIViewController *)
          pageViewController:(UIPageViewController *)pvc
 viewControllerAfterViewController:(UIViewController *)c;

UIViewController

开放了一些API方便开发者写自定的Container

1
2
3
4
5
6
7
8
9
10
11
-(void)addChildViewController:(UIViewController *)cvc;
-(void)removeFromParentViewController;
 
@property (readonly) NSArray *childViewControllers;
 
-(void)transitionFromViewController:(UIViewController *)fVC
    toViewController:(UIViewController *)toViewController
            duration:(NSTimeInterval)duration
             options:(UIViewAnimationOptions)options
          animations:(void (^)(void))animations
          completion:(void (^)(BOOL finished))completion;

具体在Session 102 Implementing UIViewController Containment

UIDocument

文件操作,有很方便的错误处理:

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
- (id)initWithFileURL:(NSURL *)url;
 
@property (readonly) NSURL *fileURL;
@property (readonly, copy) NSString *localizedName;
@property (readonly, copy) NSString *fileType; @property (copy) NSDate *fileModificationDate;
@property (retain) NSUndoManager *undoManager;
 
- (void)openWithCompletionHandler:
                (void (^)(BOOL success))completionHandler;
- (void)closeWithCompletionHandler:
                (void (^)(BOOL success))completionHandler;
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError;
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError;
 
- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)interactionPermitted;
- (void)finishedHandlingError:(NSError *)error recovered:(BOOL)recovered;
- (void)userInteractionNoLongerPermittedForError:(NSError *)error;
 
enum {
    UIDocumentStateNormal,
    UIDocumentStateClosed = 1 << 0,
    UIDocumentStateInConflict = 1 << 1,
    UIDocumentStateSavingError = 1 << 2,
    UIDocumentStateEditingDisabled = 1 << 3
};
typedef NSInteger UIDocumentState;
 
@property (readonly) UIDocumentState documentState; NSString *const UIDocumentStateChangedNotification;

具体在Session 116 Storing Documents in iCloud Using iOS 5

AssetsLibrary

没接触过这部分:

• ALAssetsLibrary

1
- (void)addAssetsGroupAlbumWithName:(NSString *)name resultBlock:(ALAssetsLibraryGroupResultBlock)r;

• ALAssetsGroup

1
- (BOOL)addAsset:(ALAsset *)asset;

• Modifying assets

1
2
3
4
5
6
-(void)writeModifiedImageDataToSavedPhotosAlbum:(NSData *)d
                metadata:(NSDictionary *)metadata
        completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)block;
-(void)setImageData:(NSData *)data
           metadata:(NSDictionary *)metadata
    completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)block;

其他更新

  • GameKit

    Session 402 403 Essential Game Technologies for iOS

  • Core Data

    What’s New in Core Data on iOS

  • NewsstandKit

    Building Newsstand Apps

  • Twitter

    Session 124 Twitter Integration

  • CoreImage

    Session 422 Using Core Image on iOS & Mac OS X

  • GLKit

    Session 414 Advances in OpenGL ES for iOS 5

Session 100 只是简短的介绍一下iOS 5的新增内容,视频还是值得一看的,各位开发者不要错过

—以上—

Tagged with:  

发表评论

电子邮件地址不会被公开。 必填项已用*标注