2012年12月23日 星期日

iOS 6 畫面旋轉小技巧

目前為了要相容 iOS 5 和 6 的旋轉,在原本的旋轉方法下,還要加入以下方法。

viewController.mm
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//僅支援直式
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSUInteger)supportedInterfaceOrientations
{
//僅支援直式
return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
return YES;
}


以為解決了,結果在 AppDelegate.m 下用

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootviewcontroller = navigationController;

這下可好,完成不理之前的設定(僅支援直式),變成任意旋轉了@_@|||
查了查才發現 self.viewController 是 UIViewController,而被加入 UINavigationController ,旋轉設定並無法影響,解決方法有兩種:
1. 改 UIViewController 為 UINavigationController 的子類別
2. 直接在 AppDelegate.m 覆寫 supportedInterfaceOrientations 方法,如下所示:

@implementation UINavigationController (iOS6OrientationFix)

-(NSUInteger) supportedInterfaceOrientations
{
//被加入的 viewController 為最上層,故只取 topViewController
return [self.topViewController supportedInterfaceOrientations];
}

@end

參考來源:
http://stackoverflow.com/questions/12410031/ios-6-rotations-supportedinterfaceorientations-doesnt-work
http://stackoverflow.com/questions/12504464/ios-6-uitabbarcontroller-supported-orientation-with-current-uinavigation-control


好了,打完收工!

2012年9月3日 星期一

[AR] 想訂購一個窗戶嗎?

國外一間公司(VELUX)運用 AR 來展示商品的應用,主打商品是"窗戶",讓使用者可直接在家中透過相機即可將各式各樣的窗戶選擇來搭配,並可進一步分享至 facebook 與好友討論,最後當然可以打開 google map 查詢販售地點,直接打電話去訂購等等...


[AR] 導入 qr-code 在廣告上的應用

一個運用 qr-code 及 ar 來展示廣告及連結商品購買的影片,有 ios 及 android 版本。


2012年8月31日 星期五

[iOS] 發送 Local Notification 方法小記

最近剛研究完 Apple Push Notification Service (APNS) 來發送訊息給 user device。
有空再來整理整個設定流程,今天同事討論到也有直接本機發送的方式,上網找了一下寫個函式記錄以方便使用。

- (void)pushLocalNotification:(NSDate *)date
{
    //直接抓取目前時間後1分鐘

    //NSDate *after = [NSDate dateWithTimeIntervalSinceNow:(1 * 60)];

    //若有舊訊息則把它刪除
    NSArray *oldNotificatoins = [application scheduledLocalNotifications];
    if([oldNotificatoins count] > 0)
        [application cancelAllLocalNotifications];

    //建立一個訊息來發送
    UILocalNotification *alarm = [[[UILocalNotification alloc] init] autorelease];
    if(alarm)
    {
        alarm.fireDate = date;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"beep-beep.caf";
        alarm.alertBody = @"test local notification message!";
        [application scheduleLocalNotification:alarm];
    }

}

2012年8月30日 星期四

[iOS] 將輸入框 (UITextField) 收起的方法

每次使用 UITextField 來讓使用者輸入文字,但在收起來這件事總是不少麻煩。
記錄一下幾個收起的方法:

1.實作 UITextFieldDelegate 的函式在按下確定 (Return、Done) 後收起

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}


2.手指點擊畫面後收起

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackgroundTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];

- (void)handleBackgroundTap:(UITapGestureRecognizer *)sender
{
    [textField resignFirstResponder];
}

2012年8月28日 星期二

[iOS] 內建 UIAlertView 顯示輸入框的方法


常常使用 UIAlertView 來顯示一些告知使用者訊息的功能,但在登入或需要使用者輸入一些欄位時(ex:密碼)可用以下的方法來呈現:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
UITextField *textField = [alertView textFieldAtIndex:0];
[alertView show];

紅色字的部份可參考以下的列舉使用。

typedef enum {
    UIAlertViewStyleDefault = 0,
    UIAlertViewStyleSecureTextInput,
    UIAlertViewStylePlainTextInput,
    UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle;