2012年4月11日水曜日

UIPopoverControllerを使ってみる

先日ActionSheetの使い方を載せましたがちょっと下のiBooksみたいな画面を表示する場合はUIPopoverControllerを使います。

- (IBAction) tapAction:(id)sender
{
    // 表示するViewController
    SelectViewController *svc = [[[SelectViewController alloc] init] autorelease];

    // Popoverの領域の大きさを設定
    svc.contentSizeForViewInPopover = svc.view.frame.size;

    svc.delegate = self;

    // Popoverのインスタンス生成
    UIPopoverController *popover = [[UIPopoverController alloc]
                                    initWithContentViewController: svc];

    // Popoverを表示する
    [popover presentPopoverFromBarButtonItem:sender
                permittedArrowDirections:UIPopoverArrowDirectionAny
                animated:YES];
}

Popoverの表示領域を設定しない場合は、画面いっぱいにViewが表示されます。
ここでは svc.view.frame.size を指定していますがこうしておくとViewのサイズに合わせて表示されます。
独自のViewController(上ではSelectViewController)を使用する場合は予めimportしておく必要があります。

他にも使い方としてはカメラロールの表示も同じやり方で出来ます。

- (IBAction) tapAction:(id)sender
{
    UIImagePickerController *ipc = [[[UIImagePickerController alloc] init] autorelease];
    ipc.delegate = self;
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    // Popoverのインスタンス生成
    UIPopoverController *popover = [[UIPopoverController alloc]
                                    initWithContentViewController: ipc];

    // Popoverを表示する
    [popover presentPopoverFromBarButtonItem:sender
                permittedArrowDirections:UIPopoverArrowDirectionAny
                animated:YES];
}

注意することも書いてみたのでそちらもどうぞ
UIPopoverControllerを使う際に注意すること


twitter連携 テスト

テストです
twitter feedを利用してbloggerの更新をtwitterと連動

2012年4月10日火曜日

Toolbarにボタンを追加する

インタフェースビルダーを使用してToolbarにボタンを配置することもできますがコードからボタンを配置する場合
// 巻き戻りボタン生成
UIBarButtonItem *rewindItem
     = [[[UIBarButtonItem alloc]
         initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
         target:self
         action:@selector(doRewind)]
         autorelease];
// 再生ボタン生成
UIBarButtonItem *playItem
     = [[[UIBarButtonItem alloc]
         initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
         target:self
         action:@selector(doPlay)]
         autorelease];
// 早送りボタン生成
UIBarButtonItem *forwardItem
     = [[[UIBarButtonItem alloc]
         initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
         target:self
         action:@selector(doForward)]
         autorelease];
// 可変長スペース生成
UIBarButtonItem *flexibleItem
     = [[[UIBarButtonItem alloc]
         initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
         target:nil
         action:nil]
         autorelease];
// 固定長スペース生成
UIBarButtonItem *fixedItem
     = [[[UIBarButtonItem alloc]
         initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
         target:nil
         action:nil]
         autorelease];
fixedItem.width = 100;

// ツールバーにボタンを配置
self.toolbarItems = [NSArray arrayWithObjects:flexibleItem, rewindItem, fixedItem, playItem, fixedItem, forwardItem, flexibleItem, nil];

上のようなかんじでUIBarButtonItemインスタンスを生成して、NSArrayに格納しUIToolbarのitemsに渡します。ツールバーの並びはNSArrayに格納した順番になります。
また、ボタンだけでなく可変長・固定長のスペースも同様に配置します。
これを実行すると以下のように表示されます。

また上のサンプルコードでは initWithBarButtonSystemItem を利用してボタンを生成しましたがこれは iOS が持っているUIBarButtonSystemItem を指定するメソッドです。
指定した文字列や画像を指定する場合
-initWithTitle: style: target: action:
文字列のボタンを生成する
-initWithImage: style: target: action:
指定した画像のボタンを生成する



2012年4月9日月曜日

画面をフルスクリーンで表示して各ツールバーを透明にする

カメラロールみたいなイメージで画面はフルスクリーンで表示してステータスバーやナビゲーションバー・ツールバーを透過
// フルスクリーンの設定
self.wantsFullScreenLayout = YES;

// 各バーの表示
[UIApplication sharedApplication].statusBarHidden = NO;
self.navigationController.navigationBarHidden = NO;
self.navigationController.toolbarHidden = NO;

// 各バーのスタイル
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;

何らかの操作でこれらを非表示にしたい場合は各バーのHiddenをYESに変更したりalphaを0に設定する
(アニメーションで非表示にする場合、ナビゲーションバー・ツールバーはalphaを変更したほうがスムーズになります)
// アニメーションの設定
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationOptionCurveEaseInOut];

// 各バーを非表示
[UIApplication sharedApplication].statusBarHidden = YES;
self.navigationController.navigationBar.alpha = 0;
self.navigationController.toolbar.alpha = 0;

// アニメーションコミット
[UIView commitAnimations];




2012年4月3日火曜日

ActionSheetの使い方

ツールバーとかにボタンを配置してそこから何かを選択させる場合にはActionSheetを使います。


ViewController.h
@interface ViewController : UIViewController
<
    UIActionSheetDelegate
>
{
    UIActionSheet *aActionSheet;
}

ViewController.m
- (IBAction) doClick:(id) sender
{
    if (aActionSheet)
        return;

    aActionSheet = [[UIActionSheet alloc]
                                 initWithTitle:@"Type1"
                                 delegate:self
                                 cancelButtonTitle:@"cancel"
                                 destructiveButtonTitle:@"destructive"
                                 otherButtonTitles:@"button1", @"button2", nil];
    [aActionSheet showInView:self.view];
    [aActionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet
         clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [aLabelTitle setText:actionSheet.title];
    [aLabelButton
         setText:[NSString stringWithFormat:@"button%d", buttonIndex]];
}
- (void)actionSheet:(UIActionSheet *)actionSheet
         didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    aActionSheet = nil;
}

iPadの場合はアクションシート表示させる際、下のようにした方がいいみたいです。
(これをiPhoneで見ると微妙に動きが違う。。。)
// iPhoneの場合
[aActionSheet showInView: self.view];

// iPadの場合
[aActionSheet showFromBarButtonItem: aButton animated:YES];

アクションシートに destructiveButtonTitle とありますがこれの使い方がよくわからずいろいろと調べてみたんですが、iOSヒューマンインターフェイスガイドラインを見ると
「害を及ぼす可能性のあるアクションを実行するボタンを提供する必要のある場合は、赤野ボタンを使用する」と記載があります。
。。。。。よくわかりませんでした。


機種(iPad/iPhone)の判別

アプリが現在動作している機種(iPad/iPhone)をソースコード内で判別するる場合はUI_USER_INTERFACE_IDIOMを使うと簡単に判別することができます。
swith (UI_USER_INTERFACE_IDIOM()) {
    case UIUserInterfaceIdiomPad:
        NSLog(@"iPad");
        break;
    case UIUserInterfaceIdiomPhone:
        NSLog(@"iPhone");
        break;
    default:
        break;
}

また下のようなやり方もできます。(こっちのほうが一般的?)
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    NSLog(@"iPad");
} else {
    NSLog(@"iPhone");
}

iPad/iPhoneで処理を振り分けるときとかはいいかもしれません




ActionScriptで重なり順を制御

随時オブジェクトを追加していく場合など表示オブジェクト内の子オブジェクトの重なり順を変更したい場合はsetChildIndex()メソッドを使用します。
(スタイルシートで言うところのz-indexみたいなイメージ)
setChildIndex(child:DisplayObject, index:int):void
コンポーネントの順番を変更
getChildIndex(child:DisplayObject):int
コンポーネントの順番を取得
swapChildren(child1:DisplayObject, child2:DisplayObject):void
child1とchild2の順番を入れ替える
swapChildrenAt(index1:int, index2:int):void
表示オブジェクト内の2つのインデックス位置にあるコンポーネントを入れ替える

使い方はこんなかんじ
<mx:Script>
    <![CDATA[
        private function init():void {
            myParent.setChildIndex(myChild1, 1);
            myParent.setChildIndex(myChild2, 0);
        }
    ]]>
</mx:Script>
<mx:Canvas id="myParent" creationComplete="init();">
    <mx:Canvas id="myChild1" backgroundColor="0xff0000" x="50" y="50" width="100" height="100" />
    <mx:Canvas id="myChild2" backgroundColor="0x0000ff" x="100" y="100" width="100" height="100" />
</mx:Canvas>