Transcript iPhone
iPhone
開發工具
• Xcode
– 負責管理專案,程式開發、編譯及除錯
• Interface Builder
– 負責UI界面及事件的處理
• 流程:
– Xcode 開啟專案→IB設計UI界面→回Xcode編譯
、執行、測試
開發工具 (續)
• API (Framework)
– MAC上相對的API 就是Carbon及Cocoa
• 程式語言
– Carbon 提供 C/C++ 使用者使用
– Cocoa 可以提供了Objective-C/ObjectiveC++/Java 語言
iPhone 軟體開發
• MAC平台
• Objective-C & Cocoa 程式語言
– iPhone SDK (蘋果的官方網站下載)
http://developer.apple.com/iphone
– 作業系統需搭配 SDK版本
• 實體iPhone機器測試
– 一台iPhone
– 購買iPhone Developer Program(年費99美金)
(一個帳號可以指定100台機子供測試)
Xcode的操作
工具列
檔案與分類
編輯區
Interface Builder
相對於Xcode是撰寫程式碼,Interface
Builder(簡稱IB)則是產生畫面的工具。
IB含有互動GUI版面配置工具,能協助UIView內容
建立互動控制元件。
畫面工具-Interface Builder
雙按MainWindow.xib啟動Interface Builder的畫
面,開啟後有一大一小的視窗。
小視窗是XIB本身,其內容圖示是XIB的所有元件
大視窗則是Window或是View出現的畫面
雙擊 MainWindow.xib
開啟Interface Builder
Interface Builder主要元件
MainWindow.xib專案含有的五個元件:
檔案擁有者(File's Owner)
應用程式委派物件
First Responder物件
視圖控制程序
視窗
mainWindows.xib
UIApplicationMain會產
生一個UIApplication
Type
object
Name
File's Owner
First Responder
App Delegate
代表這個XIB的類別,通常我們把
讓文字輸入元件出現
它設定為UIApplication或
UIApplication
鍵盤時,必須讓這個元
UIViewController用來代表應用程
件取得First Response
式或視窗元件控制器
由UIApplicationMain直接跳
入AppDelegate.m的
代表該畫面中取得焦點(Focus)的
applicationDidFinishLaunchin
UIResponder
元素。
g
UIApplicationDelegate
由此可知為何Interface Builder
View Controller
UIViewController
可以Link Target/Action(例如:
按下Button,顯示出字)等等。
window
說明
UIWindow
指定AppDelegate Class為
application delegate。
指定ViewController為Object View
Controller。
視窗骨架
Inspector和Library
檢測器(Inspector)與元件庫(Library)這二個視
窗是IB進行製作時所需的重要工具。
Inspector可以對專案裡的界面元件進行檢視和客
製化處理。
使用Inspector來觀看物件如何被連結,以及它們所
連結的是哪些類別。
Library會提供一整組可重複使用的Cocoa Touch物
件。
你可以把物件從Library視窗拖曳到應用程式裡。
選擇「Tools」(工具)→「Library(CommandShift-L)」(元件庫)。
雙擊 Tools 開啟Library
選擇「Tools」(工具)→「Inspector(CommandShift-I)」(檢測器) 。
雙擊 Tools 開啟Inspector
檢測視窗
觀測連結物件
與類別
MainWindow專案
元件庫
元件可拖曳至
此區塊
呈現畫面
Inspector
屬性檢測(Attribute Inspector)
連結檢測 (Connections Inspector)
把物件與畫面之間串聯起來
視窗大小檢測 (Window Size Inspector)
用來設定元件的屬性
此元件的視窗大小
類別檢測 (Identity Inspector)
這個元件所代表的類別
連結檢測
屬性檢測
視窗大小檢測
類別檢測
模擬器與實機
某些硬體在模擬器上無法被模擬出來
– 加速度計 (Accelerometer)
– GPS定位
目前模擬器無法模擬。
要使用以上二種功能
需上傳iPhone實機上執行測試。
在實機上測試步驟
• 需先進入 apple 官網的iPhone DevCenter網頁
–
–
–
–
–
–
–
登入Program Protal
建立憑證
指定開發裝置
指定App IDs
產生Provision Profile
放入Xcode執行軟體
指定在實機上執行測試
詳細步驟官網有影片可參考
iPhone定位程式
• 加入CoreLocation Frameworks
– #import <CoreLoctaion/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
必須引入兩個主要標頭檔
• 透過CLLocationManager類別使用定位程式
– locmanager = [[CLLocationManager alloc] init];
建立CLLocationManager類別
– [locmanager setDelegate:self];
將委派指定到主要視圖控制程序
iPhone定位程式
• 透過CLLocationManager類別使用定位程式
– [locmanager setDesiredAccuracy :
kCLLocationAccuracyBest];
指定委派對象為本身,設定使用最精確的方法
– [locmanager startUpdatingLocation];
允許iPhone軟體取得使用者座標,定位完成後就
會觸發「didUpdateToLocation」事件。
使用者目前的經緯度
• 當定位完成,可用「didUpdateToLocation」取得
使用者座標
– (void)locationManager:(CLLocationManager *)manager didU
pdateToLocation:(CLLocation *)newLocation fromLocation:(
CLLocation *)oldLocation
{
CLLocationCoordinate2D loc = [newLocation coordinate];
tflatitude.text = [NSString stringWithFormat: @"%f",
loc.latitude];
tflongitude.text= [NSString stringWithFormat: @"%f",
loc.longitude];
}
電子地圖顯示位置
• 將取得的經緯度傳送給Goolge Map電子地圖顯示
位置
– if (stopenmap.on)
{
NSString *mapUrl = [NSString stringWithFormat:
@"http://maps.google.com/maps?q=%f,%f",
loc.latitude, loc.longitude];
NSURL *url = [NSURL URLWithString:mapUrl];
[[UIApplication sharedApplication] openURL:url];
}
手機執行畫面
iPhone加速度感測器
• iPhone提供三個內建感測器,偵測iPhone三個方向
垂直軸線的加速度運動情況。
• 三垂直線方向分別代表:左/右(X)、上/下(Y)、及
前/後(Z)。
• 數值代表:iPhone所受的力道強度。
(包括地心引力和使用者移動量)
• 加速度的數值範圍誤差是介於-1.0到1.0之間,xyz
方向的力向量。
iPhone移動方向
x>0 : iPhone向右移动
x<0 : iPhone向左移动
y>0 : iPhone向上移动
y<0 : iPhone向下移动
z>0 : iPhone向正面移动
z<0 : iPhone向背面移动
圖片取自Apple iPhone
OS 3.0 Library
Accelerometer
• Iphone加速度可檢測3個軸的加速度。
• 取得加速度類別
• UIAccelerometer *accelerometer =
[ UIAccelerometer shareAccelerometer];
• 需以秒指定更新間隔(指定間隔為每秒60次)
accelerometer.delegate = self;
accelerometer.updateInterval = 1.0f/60.0f;
取得內部加速度的數據
-(void)accelerometer:(UIAccelerometer *)
accelerometer didAccelerate:(UIAcceleration *
)acceleration {
float xx = [acceleration x];
float yy = [acceleration y];
float zz = [acceleration z];
}
檢測搖動程式
-(void)accelerometer:(UIAccelerometer
*)accelerometer didAccelerate:(UIAcceleration
*)acceleration {
if(fabsf(acceleration.x)> 1.0 || fabsf(acceleration.y) >
1.0|| fabsf(acceleration.z) >1.5){
shakeCount++;
//2秒內偵測到4次則判定為shake搖晃手機
if(shakeCount>4){
shakeCount=0;
//畫面顯示搖動文字
mylabel.text=@"搖動";}
}}
Note:(大於1.5 :輕微搖動,大於2.0:劇列搖動)
手機執行畫面