Category Archives: Cocoa

Fade-in animation at UIView’s drawRect

Although UIView’s animateWithDuration:animations: method is quite useful, if you want a view to animate a drawing code written in drawRect method, you can’t use this method.

CABasicAnimation class is suitable for solving this problem like this.

- (void)drawRect:(CGRect)rect
{
    CABasicAnimation *animation;

    animation = [CABasicAnimation animation];
    [animation setDuration:0.5];

    [[self layer] addAnimation:animation
                        forKey:@"contents"];

    // drawing code
}

Reference

How to use Xcode 3 color themes in Xcode 4

I’m using either MyBlackboard (In Japanese) or Railscasts color themes in Xcode 3, these color themes, however, couldn’t be used in Xcode 4.

I want to convert 3’s xccolortheme files into 4’s dvtcolortheme files, and found the just script which converted it.
mrevilme’s gist: 488120 — Gist

The usage of this tiny python script is very simple like the following command.

$ xcode3_theme_to_xcode4.py MyBlackBoard.xcolortheme

This step is very quick, and finally, I moved the dvtcolortheme file to the directory.

mkdir -p ~/Library/Developer/Xcode/UserData/FontAndColorThemes/
mv MyBlackBoard.dvtcolortheme ~/Library/Developer/Xcode/UserData/FontAndColorThemes/

If you’ve already started Xcode, restart it and you’ll find the theme at the preference of Xcode.

References

How to support OAuth in ASIHTTPRequest

ASIHTTPRequest is a brilliant library, when you want to handle HTTP requests.
At this time, however, this library doesn’t support OAuth.

You can support it easily with OAuthCore library which is developed by atebits who is the developer of Tweetie a.k.a. Twitter for Mac/iPhone.
atebits / OAuthCore / overview – Bitbucket

A sample code is following.

#import "OAuthCore.h"

NSString * const CONSUMER_KEY = @"YOUR_CONSUMER_KEY";
NSString * const CONSUMER_SECRET = @"YOUR_CONSUMER_SECRET";
NSString * const ACCESS_TOKEN = @"YOUR_ACCESS_TOKEN";
NSString * const ACCESS_TOKEN_SECRET = @"YOUR_ACCESS_TOKEN_SECRET";

NSURL *URL = [NSURL URLWithString:@"http://example.com/api"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:URL];

[request setPostValue:@"test" forKey:@"test_value"];
[request buildPostBody];

NSString *header = OAuthorizationHeader([request url],
                                        [request requestMethod],
                                        [request postBody],
                                        CONSUMER_KEY,
                                        CONSUMER_SECRET,
                                        ACCESS_TOKEN,
                                        ACCESS_TOKEN_SECRET);


[request addRequestHeader:@"Authorization" value:header];

If you want to get “Access Token” and “Access Toke Secret”, such as you authorize with xAuth, you should set both ACCESS_TOKEN and ACCESS_TOKEN_SECRET, not nil but @””.

References

Google Data APIs Objective-C Client LibraryをiOS SDKで使用するための準備

以下の文章は、Google Data APIs Objective-C Client LibraryをiOS SDKで使用するための準備 | アクトインディ技術部隊報告書と同じものです。

今回はGoogle Data APIs Objective-C Client LibraryをiOS SDKで使用するための準備について書きたいと思います。

Google Data APIs Objective-C Client LibraryはGoogleの様々なサービスをObjective-Cから使うことができるライブラリです。
Google Data APIs Objective-C Client Library

あらかじめGData.xcodeprojを、ライブラリを使用したいプロジェクトの「ファイルとグループ」欄に追加しておきましょう。

ライブラリをコンパイルする

コンパイルオプションを指定してコンパイルします。
今回はPicasaサービスとそれに付随してOAuthを使用したので、以下のオプションを指定しました。

-DGDATA_INCLUDE_PHOTOS_SERVICE=1
-DGDATA_INCLUDE_OAUTH=1

iPhone Simulator版とiPhone OS版をReleaseでコンパイルします。

lipoでFat Binaryにする

cd build
lipo -create Release-iphoneos/libGDataTouchStaticLib.a Release-iphonesimulator/libGDataTouchStaticLib.a -output libGDataTouchStaticLib.a

コンパイルしたライブラリをXcodeに追加する

  • Xcodeの「ターゲット」でアプリを指定し右クリックし「情報を見る」を選択。
  • 「一般」 – 「リンク済みライブラリ」の下の「+」をクリック。
  • 「その他を追加…」をクリックし、さきほどコンパイルしたlibGDataTouchStaticLib.aを選択。

コンパイルするための設定を追加する

  • プロジェクトを右クリックし、「情報を見る」を選択。
  • 「ビルド」を選ぶ。
  • 「ヘッダ検索パス」に/usr/include/libxml2を追加。
  • 「ユーザヘッダ検索パス」にライブラリのフォルダを追加(このとき「再帰的」にチェックを入れる)。
  • 「常にユーザパスを検索」にチェックを入れる。
  • 「他のリンカフラグ」に -all_load -ObjC -lxml2 を指定。

参考文献

Objective-Cでメンバ変数に動的にアクセスする方法

以下の文章はObjective-Cでメンバ変数に動的にアクセスする方法 | アクトインディ技術部隊報告書と同様のものです。

今回、複数回使い回したいViewがあったのですが、一方で様々なところで使われるものではありませんでした。
こんなときにわざわざクラスを作るのはどうも重い感じがしてしまいます。
そこでUIViewのサブクラスを作らずに、メソッドで作ることにしました。

  • UIViewController
    • 今回作成したいView
    • 今回作成したいView

というように複数個必要です。
このViewの中身はUILabelが2つだけ。

さらに出来ることなら、作成するViewの中身であるUILabelを、このViewを持っているViewControllerのメンバ変数からアクセスしたい。
以前はTagを割り当ててアクセスしていたのですが、意外と面倒だったのでそれ以外の方法がないかどうか調べてみました。

実際には以下のようなコードを使用することで出来ました。

#include <objc/runtime.h>

@implementation HogeViewController

- (UIView *)generateView:(NSString *)titleLabelName contentLabelName:(NSString *)contentLabelName
{
    UIView   *resultView;
    UILabel  *titleLabel, *contentLabel;

    // 便宜上CGRectZeroを使います。
    resultView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

    titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [resultView addSubview:titleLabel];
    [titleLabel release];

    object_setInstanceVariable(self, [titleLabelName UTF8String], titleLabel);

    contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [resultView addSubview:contentLabel];
    [contentLabel release];

    object_setInstanceVariable(self, [contentLabelName UTF8String], contentLabel);

    return resultView;
}

@end

titleLabelName, contentLabelNameにはそれぞれに割り当てたいメンバ変数の名称を指定します。
こんな感じにobject_setInstanceVariable関数を使用してあげれば、割り当てたいメンバ変数に割り当てることが出来ます。

P.S.
といいつつも、実は様々なところで使われる必要がわかり、まじめにUIViewのサブクラスを作成したため、上記のコードはお蔵入りになりました。

参考文献

UITableViewにcellを動的に追加/削除する2つの方法。

以下の文章はUITableViewにcellを動的に追加/削除する2つの方法。 | アクトインディ技術部隊報告書と同様のものです。

UITableViewにcellを動的に追加/削除するということについて書こうと思います。

特にこれまでUITableViewのbeginUpdatesとendUpdatesという二つのメソッドを、いったいどんな場合に使用したらいいのか明確になっていませんでした。
ここら辺を重点的に書きたいと思います。

以下のAppleの公式ドキュメントにかなり詳しく書かれています。
Table View Programming Guide for iPhone OS: Inserting and Deleting Rows and Sections

cellを一つずつ追加/削除していく場合

cellを一つずつ追加/削除していく場合には、beginUpdates/endUpdatesの出番はありません。
追加/削除したいcellのindexPathを作成してinsert(or delete)RowsAtIndexPaths:withRowAnimation:メソッドを呼ぶだけ。
単純です。

ただし注意点はinsertRowsAtIndexPaths:withRowAnimation:メソッドを呼ぶと、UITableViewDataSourceのtableView:cellForRowAtIndexPath:メソッドが呼ばれるということ。
すなわち、cellの中身をNSArray等で管理している場合にはあらかじめNSArrayの内容を変更しておかなければなりません。

cocoa*life – UITableViewCellを削除ボタンを使って削除する

複数のcellをいっぺんに追加/削除する場合

ここまではbeginUpdatesやendUpdatesを使用する必要はありません。
では、どういうときに使用するべきなのか?

ポイントはbeginUpdates/endUpdatesに関することが書かれているセクションのタイトルがBatch Insertion and Deletion of Rows and Sectionsであるということ。
batchというのは英語で束という意味なので、「いっぺん」に複数のcellを処理したいときに使うということになります。

つまり、このメソッドを使用するかしないかの選択基準の違いは、一つずつかいっぺんかということになります。
もちろん複数のcellを追加/削除したい場合であっても、一つずつ追加/削除することができます。

beginUpdatesとendUpdatesで追加/削除のメソッド群を挟みます。
endUpdatesを呼び出したあと、tableView:cellForRowAtIndexPath:メソッドが呼ばれ、追加と削除の結果が表示されます。

beginUpdatesとendUpdatesで挟まれた追加/削除の挙動は、公式ドキュメントに以下のように書かれています。

The code calls the deleteRowsAtIndexPaths:withRowAnimation: method after it calls insertRowsAtIndexPaths:withRowAnimation:. However, this is not the order in which UITableView completes the operations. It defers any insertions of rows or sections until after it has handled the deletions of rows or sections. This happens regardless of ordering of the insertion and deletion method calls.

Deletions within an animation block specify which rows and sections in the original table should be removed; insertions specify which rows and sections should be added to the resulting table.

引用元: Table View Programming Guide for iPhone OS: Inserting and Deleting Rows and Sections(強調は引用者による)

この文章をまとめてみました。
(前提条件としてbeginUpdates/endUpdatesで囲まれている中では)

  • deleteRowsAtIndexPaths:withRowAnimation:メソッドをinsertRowsAtIndexPaths:withRowAnimation:メソッドの前に呼ぶことは必須ではない(not the order)。
  • 実際のcellの追加は削除のあとに行われる。上記メソッドの呼び出し順序は影響を及ぼさない(regardless of ordering of the insertion and deletion)。
  • 削除時のindexPathにはbeginUpdates呼び出し前、元々の(original)テーブルに対するものを指定し、追加時は削除後の結果となる(resulting)テーブルに対するものを指定する。

このような仕組みにすることで、以下のページに書かれているように1つのcellを削除するごとにindexPathを調整することが必要なく、削除を直感的に行うことが出来ます。
テーブルのセルの削除や追加にbeginUpdates/endUpdatesは必要か(2) – iPhoneアプリ開発まっしぐら★ – iPhoneアプリ開発グループ

ということで、beginUpdates/endUpdatesメソッドはcellをいっぺんに追加/削除したい場合に使えばよい!ということになります。

参考文献

Core Animationで遊んでみる (2)

以下はCore Animationで遊んでみる (2) | アクトインディ技術部隊報告書と同じものです。

以前のエントリに引き続き、Core Animationで少し遊んでみたいと思います。
これも簡単にできるフェード処理です。

以前のエントリと同様、QuartzCore.frameworkをincludeし

#import <QuartzCore/QuartzCore.h>

とimportすることが必要です。

さて実際にフェード処理を記述してみましょう。
たとえば、hogeViewというViewをフェードアウトしてみます。

CATransition    *transition;
transition = [CATransition animation];

// トランジション時間の設定(これだと1秒)。
[transition setDuration:1.0f];

// トランジションの種類の設定、今回はフェード。
[transition setType:kCATransitionFade];

// トランジション中に起こす処理を記述。
[hogeView setHidden:YES];

// トランジションを実行。
[[[self view] layer] addAnimation:transition forKey:@"layerAnimation"];

フェードインの時はsetHiddenをNoですね。

この方法を用いて、もう一つサンプルを書いてみます。
UINavigationItemのright/leftButtonItemを設定する際にはanimated:YESとすることでフェードイン/アウトしてくれますが、TitleViewはそのような項目はないのでこの方法を使えないかと思いやってみたらできました。

TitleViewにはUISegmentedControlを設定することにします。

CATransition    *transition;
transition = [CATransition animation];

[transition setDuration:1.0f];
[transition setType:kCATransitionFade];
[[self navigationItem] setTitleView:segmentedControl];
[[[[self navigationController] navigationBar] layer] addAnimation:transition forKey:@"layerAnimation"];

Core Animationで遊んでみる (1)

以下はCore Animationで遊んでみる (1) | アクトインディ技術部隊報告書と同様のものです。

Mac OS X 10.5 Leopardの目玉機能の一つはCore Animationでした。
Appleの触れ込みによると、Core Animationを利用すると簡単にアニメーション処理を追加することができるそうです。

なんですが、アニメーションではないところでも、便利に使うことができます。
今回はiPhone SDKを用いて、そのちょっと便利な使い方を書こうと思います。

以下のサンプルでは全部
QuartzCore.frameworkをincludeし

#import <QuartzCore/QuartzCore.h>

とimportをすることが必要です。

UIViewを角丸にする

UIViewを角丸にするのはとても簡単です。

[[view layer] setCornerRadius:10.0];
[view setClipsToBounds:YES];

UIViewに枠線を追加する

[[view layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
[[view layer] setBorderWidth:1.0];

setBorderColorメッセージの引数はCGColorなので、上記のようにUIColorからCGColorに変更してあげる必要があります。

UITextFieldに入力されている文字数を動的に、非同期に数える。

以下はUITextFieldに入力されている文字数を動的に、非同期に数える。 | アクトインディ技術部隊報告書と同じものです。

とあるUITextFieldによる入力フォームと、UIBarButtonItemによる「完了」ボタンがある画面を考えます。

今回のエントリの目的はUITextFieldに文字が入力されていない、つまり空であるときは、「完了」ボタンを表示しない画面を作成するということです。

ポイントはUIControlEventEditingChangedイベントを使うこと。
最初はUITextFieldDelegateのtextField:shouldChangeCharactersInRange:replacementString:とかを使用することを考えましたが、文字を消去したときにボタンが消えない等々うまくいきませんでした。

サンプルコードを書いてみます。

- (void)setupUserInterface
{
    UIView      *contentView;
    UITextField *textField;

    contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self setView:contentView];
    [contentView release];

    doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                               target:self
                                                               action:@selector(respondsToDoneButtonTouchDown:)];

    textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    [contentView addSubview:textField];
    [textField release];
    [textField addTarget:self
                  action:@selector(respondsToEditingChanged:)
        forControlEvents:UIControlEventEditingChanged];
}


- (void)respondsToEditingChanged:(id)sender
{
    if ([sender isKindOfClass:[UITextField class]])
    {
        // 本当はsenderを用いずにメンバ変数とかにしておけばいいのだけれど、今回はあえて型変換をしてみる。
        if ([[(UITextField *)sender text] length])
        {
            [[self navigationItem] setRightBarButtonItem:doneButton animated:YES];
        }
        else
        {
            [[self navigationItem] setRightBarButtonItem:nil animated:YES];
        }
    }
}

参考
Disable button if textField is empty – iPhone SDK Development

iPhoneによる位置情報関係のまとめ

以下はiPhoneによる位置情報関係のまとめ | アクトインディ技術部隊報告書と同様のものです。

今回も前回のGeohashに引き続き、位置情報に関する内容です。
ググれば出てくる内容なので、新しいことはなんにもないのが申し訳ないところです。

位置情報に関するライブラリは

  • CoreLocation
  • MapKit

などがあります。

GPSで現在位置を取得するにはCoreLocationを使用し、地図を表示したり現在地の大まかな住所を取得するためにはMapKitを使用します。

GPSで現在位置を取得するには

CoreLocationではCLLocationManagerDelegateを実装し

- (void)setup
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    if ([locationManager locationServicesEnabled])
    {
        [locationManager setDelegate:self];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [locationManager setDistanceFilter:kCLDistanceFilterNone];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%@", newLocation);
}

このようにすればLogに現在地の情報が取得できます。

地図を表示するには

- (void)setup
{
    CGRect screen = [[UIScreen mainScreen] bounds];
    MKMapView *mapView = [[MKMapView alloc] initWithFrame:screen];
    [[self view] addSubview:mapView];
    [mapView release];

    MKCoordinateRegion  region;
    region.center = [location coordinate];
    region.span.latitudeDelta = 0.005;
    region.span.longitudeDelta = 0.005;
    [mapView setRegion:region animated:YES];
}

とすれば、locationで指定した座標に移動します。

現在地を取得するには

MKReverseGeocoderDelegateを実装し

- (void)setup
{
    MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:[location coordinate]];
    [reverseGeocoder setDelegate:self];
    [reverseGeocoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    NSLog(@"%@", [placemark title]);
}

とすることで取得することができます。
すべてのAPIが非常にシンプルに作られているので、使用するのは非常に簡単です。