本文整理 iOS 5 中多項新增 API 的用法,並附上簡要範例。
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(200, 100, 0, 0)];
[stepper sizeToFit];
stepper.value = 0;
stepper.minimumValue = 0;
stepper.maximumValue = 1;
stepper.stepValue = 0.1;
[stepper addTarget:self action:@selector(stepperAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:stepper];
[stepper release];
- (void)stepperAction:(UIStepper *)stepper
{
NSLog(@"stepper value:%f", stepper.value);
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"Hello World"
delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
// alert.alertViewStyle = UIAlertViewStylePlainTextInput; // 樣式一
// alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; // 樣式二
// alert.alertViewStyle = UIAlertViewStyleSecureTextInput; // 樣式三
[alert show];
[alert release];
// 回傳指定索引值的 TextField(僅 iOS 5.0 以上)
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex
{
return textField;
}
UIScreen *mainScreen = [UIScreen mainScreen]; mainScreen.brightness = 0.5; // 設定螢幕亮度為 50% // 預設為 NO。設為 YES 可透過 wantsSoftwareDimming 宣告此應用需要把 // 螢幕亮度調到比中等亮度更暗的層級。(注意:開啟此選項可能影響效能, // 因為這種昏暗是以軟體方式實作的。) mainScreen.wantsSoftwareDimming = YES;
NSString *key = @"hello";
// 判斷任何已安裝的字典裡是否有 key 的定義
if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:key])
{
UIReferenceLibraryViewController *controller =
[[UIReferenceLibraryViewController alloc] initWithTerm:key];
[controller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:controller animated:YES];
[controller release];
}
// 這個 delegate 方法詢問:在特定方向上,左側要如何處理。
// 要隱藏就回傳 YES,要保留在螢幕上就回傳 NO。
- (BOOL)splitViewController:(UISplitViewController *)svc
shouldHideViewController:(UIViewController *)vc
inOrientation:(UIInterfaceOrientation)orientation
{
return YES;
}
// 為 tableview 註冊一個 nib
UINib *nib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"identifier"];
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 重用前面註冊過的 cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
// other code
return cell;
}
iOS 5 強化了 UIImage 的動畫能力,可透過 animatedImagesWithImages:duration: 等方法快速建立序列動畫。
// 把程式中所有 slider 改為紅色 [[UISlider appearance] setMinimumTrackTintColor:[UIColor redColor]];
此控制項提供類似翻書效果的頁面控制方式,可用來實作圖書般的翻頁體驗。
iOS 5 引入 UIDocument,作為 iCloud 開發的基礎,可謂 iCloud 開發的準備。
ALAssetsLibrary 提供對 iOS 裝置中相片與影片的存取。可透過 valueForProperty 取得圖片資訊,包括類型、Location、時長、方向、日期、格式與 URL 位址。
self.view.backgroundColor = [UIColor whiteColor];
self.assetsLibrary = [[ALAssetsLibrary alloc] init];
dispatch_queue_t dispatchQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(dispatchQueue, ^(void)
{
// 遍歷所有相冊
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
// 遍歷每個相冊中的項目 ALAsset
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
__block BOOL foundThePhoto = NO;
if (foundThePhoto)
*stop = YES;
// ALAsset 的類型
NSString *assetType = [result valueForProperty:ALAssetPropertyType];
// 若是照片(ALAssetTypeVideo / ALAssetTypeUnknown 為其他類型)
if ([assetType isEqualToString:ALAssetTypePhoto])
{
foundThePhoto = YES;
*stop = YES;
// 取得資源的表示方式
ALAssetRepresentation *assetRepresentation = [result defaultRepresentation];
CGFloat imageScale = [assetRepresentation scale];
UIImageOrientation imageOrientation =
(UIImageOrientation)[assetRepresentation orientation];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
CGImageRef imageReference = [assetRepresentation fullResolutionImage];
// 對找到的圖片進行操作
UIImage *image = [[UIImage alloc] initWithCGImage:imageReference
scale:imageScale
orientation:imageOrientation];
if (image != nil)
{
// 呈現
self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = image;
[self.view addSubview:self.imageView];
}
else
{
NSLog(@"Failed to create the image.");
}
});
}
}];
}
failureBlock:^(NSError *error)
{
// 讀取失敗的處理
}];
});
GLKit 大幅簡化了 OpenGL ES 的使用,可用來快速建立圖形應用。
iOS 5 帶來了強大的 Core Image 框架,可對影像進行高效能的濾鏡與效果處理。
Core Data 是 Cocoa 中負責物件圖管理與資料持久化的框架,適合處理複雜的資料模型與儲存需求。