プログラマメモ2 - programmer no memo2

rejectされた理由 2010/07/21

iphone appのレビューでrejectされてしまったのは、MKMapViewの使い方の問題だったわけですが、viewのサイズを大きくしてツールバーでgoogleさんのブランディングのロゴが消えてしまったのがいけなかったわけです。左すみにあるあのロゴです。
map使う人は注意しないといけませんね。



気がつかなかった。

UITouchのhash 2010/07/19

ccTouchesBeganからはじまって、ccTouchesMovedで動きを捕捉する際に、どうやったら最初にタッチしてから動かし続けている値なのかとわかるのかなーと考えていたわけですが、UITouchにはるhashが一貫して変化しないようなので、hashを保持してれば、問題なさそうだと。

NSUInteger touchHash = [touch hash];
GTekna Corp. » Track the same UITouch Object


あと、(NSSet*)touchesで受けたら、for (UITouch *touch in touches) でぐるぐるまわして使う。

次はタッチしたらそのスプライトの画像を変化させたいなと。

cocos2dです。タッチして表示したオブジェクトを動かすというコードを試している最中です。 2010/07/18
2010/07/19

cocos2dです。タッチして表示したオブジェクトを動かすというコードを試している最中です。
cocos2dはゲーム向けのフレームワークです。おそらく僕が生のcocoa touchを使ってコードを書いていくよりずいぶん楽ができるだろうと期待してます....


さて、画面に何やら表示するさいにはCCSpriteというのを使うようです。
で、それを画面でタッチして動かしたいわけなので、イベントをひろう方法は、えーと、
イベントをひろうのは下記
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;


で、ccTouchesBeganで、タッチした位置がスプライトと重なっているか判定したい。


BOOL isOk = NO;
-(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{

UITouch *touch =[touches anyObject];
CGPoint location =[touch locationInView:[touch view]];
location =[[CCDirector sharedDirector] convertToGL:location];
int offX = location.x;
int offY = location.y;

NSLog(@"%d %d", offX, offY);

CGRect スプライトRect = [self rectForSprite:スプライト];
if(CGRectContainsPoint(スプライトRect, location)) {
isOk = YES;
} else {
isOk = NO;
}
}


で、中で使われているrectForSpriteの実装は下記。
このメソッドは、Cocos2d: Trials and Tribulationsで紹介されていたものです。

-(CGRect)rectForSprite:(CCSprite *)sprite{

float h = [sprite contentSize].height;
float w = [sprite contentSize].width;
float x = sprite.position.x - w/2;
float y = sprite.position.y - h/2;

CGRect rect = CGRectMake(x,y,w,h);

return rect;
}


なんとかできてる気がするが...

UITableViewControllerを使わないなら、didSelectRowAtIndexPathで、deselectRowAtIndexPathをしないとだめよ。 2010/07/12

「そうは問屋が卸さない」って感じです。
iphoneアプリです。先日、iAdを取り込んで審査にだしましたが、また途中で、問題あることに気がついて、developer rejectしました。
今回の理由は、UITableViewControllerを使用しない方法をとったがため、セルを一度選択したら、その選択をはずすというコードがぬけてしまったことです。

あらためて、コードを追加。


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];

}


で、何故気がついたかというと電車の中で、ぱらぱらと、

iPhoneSDK開発のレシピ
高山 恭介 広部 一弥 松浦 晃洋
4798025798


を眺めていたら、レシピ052に、自前で解除しないと審査でリジェクトされると書いてあってまさかねーと思ったら案の定、解除していなかったわけです。

3度目の正直といきたいところです。

iAdをとりこんでみる練習 2010/07/11

テーブルリストがあるViewの一番下にiAdをおきたいなーと試行錯誤してます。
XIBを使用しないで、UITableViewControllerを使っていたのと、cocoaの仕組みを理解してないところが災いして手間取ってます。

まあ、この方法でいけるかなというところでメモ。

UITableViewController をUIViewController<uitableviewdelegate,uitableviewdatasource>にしちゃう。
UITableViewをインスタンス変数にしてしまう。
initでUIViewをつくって、self.viewに入れる。
delegateにしたので、self.tableView.delegate = self;self.tableView.dataSource = self;
しておく。
self.viewにtableViewとiAdのバナーを位置決めしてaddSubViewしてしまう。
deallocで値の解放を忘れずに。


ぐだぐだ悩むより[WWDC 2010 Session Videos - Session 112 - Integrating Ads with iAd]みるのが一番!!

enumerateObjectsUsingBlockの練習 2010/07/04

Objective-Cです。
enumerateObjectsUsingBlockを使ってみます。
Objective-CってC + Smalltalkと考えれば、何となくこの文法ってありなんだなーと。

Smalltalkってかなり昔からあるというのは知っています。
といいつつもSmalltalkのことわかってません....
※^これがでてくるところとか...
なじみがないので、混乱してしまいますが、温故知新ですかね。

#import <Foundation/NSArray.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>

// NSMutableArrayだよ!!
static NSMutableArray* array;
@interface MyData : NSObject {
NSString* name;
}
@property (nonatomic, retain) NSString* name;
@end

@implementation MyData
@synthesize name;
@end

int main(){

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


{
// データつくる

array = [[NSMutableArray alloc] init];
int i = 0;
for (i=0; i<5; i++) {
MyData* mydata = [[MyData alloc]init];
mydata.name = [NSString stringWithFormat:@"aaaa%d",i];
[array addObject:mydata];
}
}


{// その場で定義してその場で使うパターン
// ここでenumerate
// enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))
// 中身の確認
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"==>%@", [obj name]);
}];

}


// BLOCKで使ってソート!!
[array sortUsingComparator:^(id obj1, id obj2) {
NSLog(@"[%@]と[%@]を比較", [obj1 name], [obj2 name]);
return [[obj2 name] compare:[obj1 name]];
}];

{// 宣言したものを後から使うパターン
id enumerator = ^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"enumerator ==>%@", [obj name]);
};

[array enumerateObjectsUsingBlock:enumerator];
}

[pool drain];
return 0;
}


とりあえず参考
Smalltalk Blocks And Closures

sortUsingComparatorはNSComparatorでソートするのさ 2010/07/03

Objective-Cです。Blocksです。
iPhone3.2とiPhone4のAPIの比較をながめていて、sortUsingComparatorがあったのでまずは練習


- (void)sortUsingComparator:(NSComparator)cmptr


なんかBlocksってかっこいいと思ってしまうのは何故だろう。。。

コンパイルはこれでやってます。
gcc main.m -framework Foundation


forでくるくるまわして出力しているけど、ここはきっとenumerateObjectsUsingBlockを使って出力するべきところだね。きっと。

#import <Foundation/NSArray.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>

// NSMutableArrayだよ!!
static NSMutableArray* array;
@interface MyData : NSObject {
NSString* name;
}
@property (nonatomic, retain) NSString* name;
@end

@implementation MyData
@synthesize name;
@end

int main(){

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


{
// データつくる

array = [[NSMutableArray alloc] init];
int i = 0;
for (i=0; i<5; i++) {
MyData* mydata = [[MyData alloc]init];
mydata.name = [NSString stringWithFormat:@"aaaa%d",i];
[array addObject:mydata];
}
}

// 中身の確認
for (id a in array) {
NSLog(@"==>%@", [a name]);
}

// BLOCKで使ってソート!!
[array sortUsingComparator:^(id obj1, id obj2) {
NSLog(@"[%@]と[%@]を比較", [obj1 name], [obj2 name]);
return [[obj2 name] compare:[obj1 name]];
}];

// 中身の確認(ソート後の確認)
for (id a in array) {
NSLog(@"==>%@", [a name]);
}

// // BLOCKで使ってソート(はじめと同じ並びにするよ)!!
[array sortUsingComparator:^(id obj1, id obj2) {
NSLog(@"[%@]と[%@]を比較", [obj1 name], [obj2 name]);
return [[obj1 name] compare:[obj2 name]];

}];

// 中身の確認
for (id a in array) {
NSLog(@"==>%@", [a name]);
}

[pool drain];
return 0;
}


参考


よくわかってないが参考2