Category Archives: Programming

[iPhone]Debug Log用マクロ

xcodeでデバッグログを出力するときは普通NSLog()を使うのだが、これが結構重たい上、Releaseビルドでも出力されてしまうのでどうしたもんかと思っていたのだが、設定とマクロでうまくできるのがわかったのでメモ。

プロジェクトの設定を開く。構成でDebugを選択
左下のボタンを押してユーザー定義の設定を追加
GCC_PREPROCESSOR_DEFINITIONSを追加し、DEBUGを設定する
e38394e382afe38381e383a3-4

以下のマクロを、インクルードされるヘッダに定義する

#ifdef DEBUG
#   define TRACE(fmt, ...) NSLog((@"%s(%d) " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define TRACE(...)
#endif

ログを出力したい場所で以下の様に使う

TRACE("startDownload genre=%@",genreId);

出力結果
2009-04-16 18:57:18.519 Beatport[14249:20b] -[TrackListViewController startDownload:](76) startDownload genre=10

「オブジェクト名 メソッド名(行番号) ログ」
という形式で出力される。マクロを以下の様に変更するにすると、ファイル名(行番号)で出力される

#   define TRACE(fmt, ...) NSLog((@"%s(%d) " fmt), __FILE__, __LINE__, ##__VA_ARGS__);

出力結果
2009-04-16 19:01:19.699 Beatport[14317:20b] 2009-04-16 19:01:45.633 Beatport[14317:20b] /Users/takatronix/Documents/Beatport/Classes/TrackListViewController.m(76) startDownload genre=10

tAkatronixおすすめのiPhone開発本

iPhone デベロッパーズ クックブック
Erica Sadun
ソフトバンククリエイティブ
売り上げランキング: 84906
詳解 Objective-C 2.0
詳解 Objective-C 2.0
posted with amazlet at 10.01.31
荻原 剛志
ソフトバンククリエイティブ
売り上げランキング: 4163
iPhoneプログラミングUIKit詳解リファレンス
所 友太
リックテレコム
売り上げランキング: 3089
iPhone Core Audioプログラミング
永野 哲久
ソフトバンククリエイティブ
売り上げランキング: 22615

[FX]MetaTrader4でカスタムインジケータを作る

MetaTrader4で自動売買を実現する前に勉強がてらカスタムインジケータを作ってみた。よくある移動平均だけど、終値のほかに高値、安値も表示するようにしてみた。

初めてMT4でプログラミングしてみたんだけど、カスタムインジケータを作るくらいなら超簡単でした。これくらいのものなら、一瞬で作れるね。ハラショーー。

高値を赤、終値を緑、安値を青で表示し、移動平均のパラメータを外だししてみる。

#property copyright "tAkatronix"
#property link      "http://freeek.jp"

#property indicator_chart_window
#property indicator_buffers 3

#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_color3 Blue

//---- input parameters
extern int       nMA_Period;

double   BufHigh[];
double   BufClose[];
double   BufLow[];

int init()
  {
   //    初期化
   SetIndexBuffer(0,BufHigh);
   SetIndexBuffer(1,BufClose);
   SetIndexBuffer(2,BufLow);

   return(0);
  }

int start()
  {
   int    nBars =IndicatorCounted();

      for(int i = nBars-1;i >=0;i--)
      {
         BufHigh[i] = 0;
         BufClose[i] = 0;
         BufLow[i] = 0;
         for(int j=0; j < nMA_Period;j++)
         {
            BufHigh[i] = BufHigh[i] + High[i+j];
            BufClose[i] = BufClose[i] + Close[i+j];
            BufLow[i] = BufLow[i] + Low[i+j];
         }
         BufHigh[i] = BufHigh[i] / nMA_Period;
         BufClose[i] = BufClose[i] / nMA_Period;
         BufLow[i] = BufLow[i] / nMA_Period;

      }

   return(0);
  }

extern int nMA_Period;
と、したパラメータはインジケータを挿入した時に表示されるダイアログで数値を入力できる。
mt1

んで、移動平均のパラメータを4にした結果
mt2

[iPhone]ActionSheetでCancelボタンが効かない問題

現在作成中のiPhoneアプリでダウンロード中の状態表示をするようにしてみた。

ActionSheetを作成して、それにUIProgressViewを貼り付けるまでは簡単だった。

オブジェクトをコントローラクラスに追加し、

	UIActionSheet	*actionSheet;
	UIProgressView *progressBar;
	UILabel			*progressLabel;

ViewDidLoadでActionSheetを作成

-(void)createActionSheet {

	if(!actionSheet) {
		actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please wait...\n\n\n\n"
												  delegate:self
										 cancelButtonTitle:NSLocalizedString(@"Cancel",nil)
									destructiveButtonTitle:nil
										 otherButtonTitles:nil];

		actionSheet.actionSheetStyle = UIBarStyleBlackTranslucent;

	}
	if(!progressBar) {

		progressBar = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 40.0f, 260.0f, 90.0f)];
		progressBar.progressViewStyle = UIProgressViewStyleDefault;
		progressBar.progress = 0.0f;
		[actionSheet addSubview:progressBar];

		progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 50.0f, 240.0f, 20.0f)];
		progressLabel.backgroundColor = [UIColor clearColor];
		progressLabel.textColor = [UIColor whiteColor];
		progressLabel.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
		progressLabel.text = NSLocalizedString(@"Downloading...", nil);

		[actionSheet addSubview:progressLabel];
	}

}

ダウンロードの開始のタイミングでActionSheetを作成

- (IBAction)startDownload:(NSString*)genreId {
	downloadedContentLength = 0;
	progressBar.progress = 0;
	progressLabel.text = NSLocalizedString(@"Downloading", nil);
	[actionSheet showInView:self.view];
//  以下略

ダウンロードの処理の途中にUIProgressViewを更新してやると完成
img1

ここまではサンプルコード通りにやれば簡単だった。

しかしどうやってもキャンセルボタンが効かない。

ViewDidLoadでActionSheetを表示させるとキャンセルボタンが効くが画面の真ん中に表示されてしまう。

ActionSheetのボタンがキャンセルボタン一つだとうまくいかないが、ボタンを増やすとキャンセルが効く・・・。???
showInViewを使わずに、showFromTabBarを使うと上からアクションシートが降りてくるがキャンセルボタンが効く。

ググると同じような問題がたくさんあって、うまく動かないので別のスレッドを使えととかかいてある。

初期化のタイミングを変えたりスレッド化したり一晩さんざん悩んだあげく、よくよく調べてみるとSDK2.2のバグらしい。

んで、いろいろ調べた結果一番エレガントな回避コードは

こいつを

[actionSheet showInView:self.view];

こうしてやることだった。

[actionSheet showInView:self.view.window];

img2

tAkatronixおすすめのiPhone開発本

iPhone デベロッパーズ クックブック
Erica Sadun
ソフトバンククリエイティブ
売り上げランキング: 84906
詳解 Objective-C 2.0
詳解 Objective-C 2.0
posted with amazlet at 10.01.31
荻原 剛志
ソフトバンククリエイティブ
売り上げランキング: 4163
iPhoneプログラミングUIKit詳解リファレンス
所 友太
リックテレコム
売り上げランキング: 3089
iPhone Core Audioプログラミング
永野 哲久
ソフトバンククリエイティブ
売り上げランキング: 22615

[iPhone]プロパティリストを読み込む NSDictionary


UITableViewにbeatportのジャンルのリストを表示するのに一番スマートな方法を模索していていい感じにやる方法がわかったのでメモ。

ベタにコードに書かずにプロパティリストエディタでgenre.plistを作成しリソースに追加する。

plist

テーブルビューに表示する名称の配列とジャンルのデータを定義する

@interface RootViewController : UITableViewController {
NSArray*		names;
NSDictionary *dictGenre;
}
@property (retain) NSArray*		names;
@property (retain) NSDictionary* dictGenre;

プロパティリストファイルをNSDictionaryクラスに読み込み、キー一覧をソートしてNSArrayオブジェクトへ展開する迄

//		Genre.plistの読み込み
NSString *pathGenre = [[NSBundle mainBundle] pathForResource:@"Genre" ofType:@"plist"];

//		RootViewControllerを取得
RootViewController* rootViewController;
rootViewController = (RootViewController*)navigationController.topViewController;

rootViewController.title = @"Grenres";
rootViewController.dictGenre = [NSDictionary dictionaryWithContentsOfFile:pathGenre];

//	NSDicronayのkeyを配列に取り出してソート
NSArray *sortedArray =[[rootViewController.dictGenre allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
rootViewController.names = sortedArray;

UITableViewのセル描写のコールバックでソートしたジャンル名を表示してやる。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [names count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
  	cell.text = [names objectAtIndex:indexPath.row];
    return cell;
}

ウホッ
grenre
tAkatronixおすすめのiPhone開発本

iPhone デベロッパーズ クックブック
Erica Sadun
ソフトバンククリエイティブ
売り上げランキング: 84906
詳解 Objective-C 2.0
詳解 Objective-C 2.0
posted with amazlet at 10.01.31
荻原 剛志
ソフトバンククリエイティブ
売り上げランキング: 4163
iPhoneプログラミングUIKit詳解リファレンス
所 友太
リックテレコム
売り上げランキング: 3089
iPhone Core Audioプログラミング
永野 哲久
ソフトバンククリエイティブ
売り上げランキング: 22615

[iPhone]UIImageViewにUILableで動く時刻を貼り付ける


ぐるぐる動かしているイメージ(UIImageView)に動く時計の文字列を貼り付けるのをどうやって実現するかでなやんだのだが、UIImageViewにUILabelを貼り付けたらどうだろうと思ってやってみたらうまくいった。

InterfaceBuilderだとUIImageViewのSubViewとして貼り付けることができなかったので、このようにUILabel を作成し、UIImageView* bImageのサブビューとして貼り付けた。

UILabel* timeLabel;
CGSize szImage = bImage.bounds.size;
timeLabel = [[[UILabel alloc] initWithFrame: CGRectMake(5,160,szImage.width,170)] autorelease];
timeLabel.font = [UIFont boldSystemFontOfSize:15.0];
[bImage addSubview:timeLabel];
timeLabel.textColor = [UIColor whiteColor];			//	文字色白
timeLabel.backgroundColor = [UIColor clearColor];	//	背景透明
timeLabel.textAlignment = UITextAlignmentCenter;	//	センタリング

UIImageを回転させるとUILabelも同じように回転する。
MFCのCWndクラスとは比べものにならないくらい高機能だな。時代の変化を感じるw。
img_00041

時刻文字列の作成はタイマーのコールバックで以下のようにやった。

NSDate *date = [NSDate date];		//	現在の日付を得る
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd kk:mm:ss"];
NSString* strTime = [dateFormatter stringFromDate:date];
timeLabel.text = strTime;

img_00061

ぐーるぐるまわったり拡大しても時刻もスムースに同期して動くね。

tAkatronixおすすめのiPhone開発本

iPhone デベロッパーズ クックブック
Erica Sadun
ソフトバンククリエイティブ
売り上げランキング: 84906
詳解 Objective-C 2.0
詳解 Objective-C 2.0
posted with amazlet at 10.01.31
荻原 剛志
ソフトバンククリエイティブ
売り上げランキング: 4163
iPhoneプログラミングUIKit詳解リファレンス
所 友太
リックテレコム
売り上げランキング: 3089
iPhone Core Audioプログラミング
永野 哲久
ソフトバンククリエイティブ
売り上げランキング: 22615