2012年9月15日 星期六

Cocos2D - CCSprite SetPosition

最近開始摸cocos2d
發現CCSprite類別的成員
如果要在init以外的地方重新設定位置
似乎不那麼容易

例如以下程式碼


clothe = [CCSprite spriteWithFile:@"clothe.png"];
clothe.position = ccp(100,100);
[self addChild:clothe];

這是在init初始的一個衣服成員
一開始就設定好在(100,100)的地方
之後我在別的函式想要重新SetPosition
卻發現無法運作 衣服不會移動位置

追查後發現解決方法
程式碼如下
[clothe stopAllActions];
[clothe setPosition:ccp(200,100)];

關鍵就是要先stopAllActions
以上

2012年8月25日 星期六

UIActionSheet Button location Error

之前一直為了UIActionSheet產生的button位置歪掉煩惱
像這張圖這樣

因為是小問題,所以沒有很認真去查
但實際上又是很煩惱的問題
今天終於搞定了
一般來說 我們要加入一個UIActionSheet會用以下程式碼
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                             delegate:self
                    cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
               destructiveButtonTitle:NSLocalizedString(@"Logout", @"logout")
                    otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
關鍵在最後一行
[actionSheet showInView:self.view];

實際上應該使用
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
要以整個window來看才正確 以上

2012年7月31日 星期二

NSMutableArray Sort


今天來分享一下NSMutableArray的排序方式

NSString *sortOrder = @"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz_0123456789";
            
            [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
             {
                 char char1 = [(NSString *)obj1 characterAtIndex: 0];
                 char char2 = [(NSString *)obj2 characterAtIndex: 0];
                 
                 int index1;
                 for (index1 = 0; index1 < sortOrder.length; index1++)
                     if ([sortOrder characterAtIndex: index1] == char1)
                         break;
                 
                 int index2;
                 for (index2 = 0; index2 < sortOrder.length; index2++)
                     if ([sortOrder characterAtIndex: index2] == char2)
                         break;
                 
                 if (index1 < index2)
                     return NSOrderedAscending;
                 else if (index1 > index2)
                     return NSOrderedDescending;
                 else
                     return [(NSString *)obj1 compare: obj2 options: NSCaseInsensitiveSearch];
             }];

這樣Array就會照英文跟數字的方式來排序了

2012年7月30日 星期一

NSLOG without Date

今天來分享NSLOG的小東西
可以不秀出時間跟專案名稱
void IFPrint (NSString *format, ...)
{
    va_list args;
    va_start(args, format);
    
    fputs([[[[NSString alloc] initWithFormat:format arguments:args] autorelease] UTF8String], stdout);
    
    va_end(args);
}

2012年7月18日 星期三

Hold Button Event

這次來分享在ios上如何偵測長壓按鈕的訊息

因為ios本身storyboard沒有提供這個函式
所以用以下的程式碼來宣告一個UILongPressGestureRecognizer
用它來掌控時間到了要呼喚哪個函式
範例中是0.3秒
UILongPressGestureRecognizer * recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];    
        recognizer.minimumPressDuration = 0.3f;  
        [downButton addGestureRecognizer:recognizer];
        [recognizer release];

常見的問題是 會在select時呼叫自己宣告長壓時的函式
但那是錯誤的
要再判斷長壓的狀態才去執行該做的事情
所以以handleLongPress這個函式來判斷
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender
{ 
    if (sender.state == UIGestureRecognizerStateEnded)
    {
  //      NSLog(@"Long press Ended");
    }
    else
    {
        [self moveFastDown];
    }
}

UIGestureRecognizerStateEnded時不作事情
這是當長壓後放開按鈕時會觸發的狀態
所以最常看到的問題就是長壓後會觸發兩次
就是因為要正確的分辨狀態再執行
以上

2012年6月30日 星期六

Change UIActionSheet Style

 首先要記得繼承UIActionSheetDelegate

修改這個UIActionSheet的預設函式
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    // 設定UIActionSheet的backround圖片
    UIImage *theImage = [UIImage imageNamed:@"selectbg.png"];    
    theImage = [theImage stretchableImageWithLeftCapWidth:32 topCapHeight:32];
    CGSize theSize = actionSheet.frame.size;
    // draw the background image and replace layer content  
    UIGraphicsBeginImageContext(theSize);    
    [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];    
    theImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    [[actionSheet layer] setContents:(id)theImage.CGImage];
}
然後調整UIActionSheet彈出後的按鈕樣式
UIActionSheet* Action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"加入" otherButtonTitles:nil, nil];
    
[[[Action valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"select_bt.png"] forState:UIControlStateNormal];
    
[[[busAction valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"select_bt2.png"] forState:UIControlStateNormal];
    
[Action showInView:self.view];

Combine two Image

-(UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second
{
    // build merged size
    CGSize mergedSize = CGSizeMake(Main_Pic_Width * 2 + 10, Main_Pic_Height);
    
    // capture image context ref
    UIGraphicsBeginImageContext(mergedSize);
    
    //Draw images onto the context
    [first drawInRect:CGRectMake(0, 0, Main_Pic_Width, Main_Pic_Height)];
    [second drawInRect:CGRectMake(Main_Pic_Width + 10, 0, Main_Pic_Width, Main_Pic_Height)]; 
    
    // assign context to new UIImage
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // end context
    UIGraphicsEndImageContext();
    
    return newImage;
}