Cov txheej txheem:

Yuav Ua Li Cas Lub Xeem Hauv Xcode Siv Swift: 9 Kauj Ruam
Yuav Ua Li Cas Lub Xeem Hauv Xcode Siv Swift: 9 Kauj Ruam

Video: Yuav Ua Li Cas Lub Xeem Hauv Xcode Siv Swift: 9 Kauj Ruam

Video: Yuav Ua Li Cas Lub Xeem Hauv Xcode Siv Swift: 9 Kauj Ruam
Video: Ib Lub Xeem Sib Yuav Puas Txhaum? 2024, Kaum ib hlis
Anonim
Image
Image

Hauv qhov kev qhia nrawm no, kuv yuav qhia koj yuav tsim lub tshuab xam zauv yooj yim siv Swift hauv Xcode li cas. Cov app no tau tsim los saib yuav luag zoo ib yam rau thawj lub tshuab xam zauv app rau iOS. Koj tuaj yeem ua raws cov lus qhia ib qib zuj zus thiab tsim lub tshuab xam zauv nrog kuv, lossis koj tuaj yeem yooj yim mus rau cov kauj ruam kawg thiab luam thiab muab cov cai tso rau hauv koj tus saib xyuas. Txawm li cas los xij, yog tias koj ua qhov no, nco ntsoov txuas tag nrho koj cov ntsiab lus ntawm zaj dab neeg nrog koj tus saib xyuas.

Kauj Ruam 1: Tsim Txoj Haujlwm

Storyboard Layout
Storyboard Layout

Thawj kauj ruam hauv kev ua peb lub laij lej yog los tsim qhov haujlwm hauv Xcode. Koj tuaj yeem ua qhov no los ntawm txhaj "Tsim qhov tshiab Xcode project" thiab npe nws txhua yam koj xav tau. Kuv lub npe hu ua "Calculator." Cov kauj ruam tom ntej yog xaiv "Daim Ntawv Thov Ib Leeg" rau hom app. Khaws tag nrho lwm cov ntaub ntawv raws li tus nqi pib.

Kauj ruam 2: Storyboard Layout

Kauj Ruam 2 ntawm kev tsim peb lub laij lej xav kom koj tsim tus qauv yooj yim hauv zaj dab neeg. Ua ntej koj pib qhov no, Kuv pom zoo hloov koj lub cuab yeej simulation mus rau iPhone 7 Plus. Pib los ntawm rub lub pob rau ntawm zaj dab neeg thiab hloov nws qhov ntev mus rau 89 x 89. Hloov nws cov xim keeb kwm yav dhau rau mercury ntawm tus cwj pwm tshuaj xyuas thiab nws cov xim xim rau tungsten. Tom ntej no, kho qhov font mus rau Helvetica Light 30. Txuas mus ntxiv thiab muab tshuaj txhuam khawm kom txog thaum koj muaj tag nrho ntawm 20. Kho qhov teeb tsa ntawm cov nyees khawm no kom koj muaj tsib kab thiab plaub kab.

Kauj Ruam 3: Tsim Zaj Dab Neeg thiab Kev Zoo Nkauj

Storyboard Tsim thiab Zoo Nkauj
Storyboard Tsim thiab Zoo Nkauj

Rho tawm khawm thib ob ntawm kab hauv qab thiab nthuav thawj lub pob kom hla qhov chaw no. Hloov qhov chaw tso tus nqi ntawm lub pob no rau xoom. Txuas ntxiv hloov tus lej tus lej thiab cov cim ntawm txhua lub pob kom txog thaum nws zoo ib yam hauv daim duab uas tau hais los saum no. Ntawm tus kws tshuaj xyuas tus cwj pwm, xim tsaus nti yog nyiaj, xim txiv kab ntxwv yog tangerine, thiab xim xim tau hloov mus rau daus ntawm cov txiv kab ntxwv. Tom ntej no, nyem rau ntawm tus saib saib thiab hloov nws cov xim keeb kwm yav dhau rau dub. Ntxiv daim ntawv lo rau saum cov nyees khawm thiab kho nws qhov loj me txawm li cas los koj xav xis nyob. Teem cov ntawv mus rau sab xis thiab hloov daim ntawv lo rau font Helvetica lub teeb 70. Yog tias koj xav tau, koj tuaj yeem ntxiv cov kev txwv rau txhua yam hauv kev txhawm rau ua kom lub app zoo ib yam rau txhua lub cuab yeej.

Kauj Ruam 4: Sib Txuas thiab Sib Koom Ua Ke

Kev Sib Txuas thiab Kev Sib Koom Ua Ke
Kev Sib Txuas thiab Kev Sib Koom Ua Ke
Kev Sib Txuas thiab Kev Sib Koom Ua Ke
Kev Sib Txuas thiab Kev Sib Koom Ua Ke

Qhib tus kws tshuaj xyuas tus cwj pwm thiab hloov daim ntawv rau txhua tus lej tus lej. Cov ntawv yuav tsum yog 1 ntau tshaj qhov muaj nuj nqis tiag. Piv txwv li, #0 khawm yuav tsum muaj tus lej cim npe ntawm 1, khawm #1 yuav tsum muaj tus lej cim ntawm 2, khawm #2 yuav tsum muaj tus lej cim npe ntawm 3, thiab ntxiv rau. Tom ntej no, nyem tswj, nyem rau ntawm #0 khawm, thiab rub nws mus rau qhov saib xyuas. Ib qho popup yuav tsum tshwm ntawm qhov screen. Hloov qhov kev txuas mus rau 'kev nqis tes ua,' hom rau 'UIButton,' qhov xwm txheej mus rau 'Touch Up Inside,' cov lus sib cav rau 'Sender,' thiab nws lub npe rau 'lej.' Koj tuaj yeem hloov lub npe mus rau txhua yam uas koj xav tau tab sis qhov ntawd txhais tau tias koj yuav tau hloov lub npe dua thaum hu ua haujlwm tom qab ntawm qhov haujlwm. Tom ntej no, tswj hwm, nyem, thiab luag txhua tus lej khawm mus rau qhov haujlwm uas peb nyuam qhuav tsim. Tam sim no, tswj, nyem, thiab rub daim ntawv lo rau hauv qhov haujlwm, tab sis TSIS YOG rau hauv txoj haujlwm. Qhov no txhais tau tias koj tsuas yog nqa daim ntawv lo rau hauv txoj haujlwm raws li qhov sib txawv sib txawv. Nco ntsoov, yog tias koj ib txwm tsis meej pem txog cov cai, Kuv tau tso tag nrho kuv cov cai rau koj siv rau theem kawg ntawm Cov Lus Qhia no.

Kauj Ruam 5: Tsim Kev Hloov

Tsim kom muaj qhov sib txawv
Tsim kom muaj qhov sib txawv

Txhawm rau ua kom peb cov lej lej ua haujlwm, peb yuav tsum txuas lawv tus nqi rau daim ntawv lo hauv peb 'cov lej' ua haujlwm. Koj tuaj yeem ua qhov no los ntawm kev xub tsim qhov sib txawv 'numberOnScreen' thiab ua nws hom ob npaug thiab sib npaug rau 0: var numberOnScreen: Ob npaug = 0; Thiab tsis txhob hnov qab, yog tias cov cai ntawm no tsis meej me ntsis, Kuv tau tso koj tag nrho cov cai ntawm cov kauj ruam kawg rau koj siv rau qhov koj nyiam. Tom ntej no, tsim kom muaj lwm qhov sib txawv 'performingMath' ntawm hom bool thiab ua rau nws cuav: var performingMath = cuav; Tsis tas li, tsim lwm qhov sib txawv hu ua 'previousNumber' ntawm hom ob thiab teeb nws sib npaug rau 0: var yav dhau losNumber: Ob npaug = 0; Qhov sib txawv kawg uas koj yuav tsum tsim yog 'kev ua haujlwm' sib txawv. Teem nws sib npaug rau 0: var ua haujlwm = 0;

Kauj Ruam 6: Tus Naj Npawb Ua Haujlwm

Cov Naj Npawb Nuj Nqis
Cov Naj Npawb Nuj Nqis

Tom qab koj tau tsim cov kev hloov pauv uas tsim nyog, koj tuaj yeem npaj mus theej thiab muab cov lej no tso rau hauv koj li 'lej' ua haujlwm:

yog tias ua tauMath == muaj tseeb {

label.text = Txoj hlua (sender.tag-1)

numberOnScreen = Ob npaug (ntawv lo lus!)!

performingMath = tsis tseeb

}

lwm tus {

label.text = ntawv.text! + Txoj hlua (sender.tag-1)

numberOnScreen = Ob npaug (ntawv lo lus!)!

}

Qhov tseem ceeb, tsab cai no qhia qee tus lej ntawm daim ntawv lo thaum lub khawm tsim nyog raug nias. Txawm li cas los xij, peb tseem yuav tsum muaj peev xwm siv tag nrho lwm cov nyees khawm thiab ua lub laij lej ua haujlwm. Peb yuav ua qhov no hauv ob peb kauj ruam tom ntej.

Kauj Ruam 7: Kev Ua Haujlwm Cov Khawm

Kev koom ua ke khawm
Kev koom ua ke khawm
Kev koom ua ke khawm
Kev koom ua ke khawm

Qhib tus kws tshuaj xyuas tus cwj pwm thiab hloov daim ntawv rau txhua qhov khawm sib txawv. Lub pob kom meej yuav tsum muaj tag ntawm 11, khawm faib yuav tsum muaj tag ntawm 12, khawm sib npaug yuav tsum muaj tag ntawm 13, khawm rho tawm yuav tsum muaj tag ntawm 14, khawm ntxiv yuav tsum muaj tag ntawm 15, thiab khawm sib npaug yuav tsum muaj tag lossis 16. Tom ntej no, nias tswj, nyem rau ntawm lub pob kom meej, thiab rub nws mus rau tus saib xyuas. Ib qho popup yuav tsum tshwm ntawm qhov screen. Hloov qhov kev txuas mus rau 'kev nqis tes ua,' hom rau 'UIButton,' qhov xwm txheej mus rau 'Touch Up Inside,' cov lus sib cav rau 'Sender,' thiab nws lub npe rau 'nyees khawm.' Koj tuaj yeem hloov lub npe mus rau txhua yam uas koj xav tau tab sis qhov ntawd txhais tau tias koj yuav tau hloov lub npe dua thaum hu ua haujlwm tom qab ntawm qhov haujlwm. Tom ntej no, tswj hwm, nyem, thiab rub txhua pob sib txawv rau hauv qhov haujlwm uas peb nyuam qhuav tsim.

Kauj Ruam 8: Lwm Yam Ua Ntej Ua Ntej

Lwm Yam Ua Ntej Ua Ntej
Lwm Yam Ua Ntej Ua Ntej

Tom qab koj tau txuas tag nrho cov cim npe sib txawv khawm rau lawv txoj haujlwm tsim nyog, koj tuaj yeem pib ntxig cov cai rau hauv 'khawm' ua haujlwm:

yav dhau losNumber = Ob npaug (ntawv lo lus!)!

yog sender.tag == 12 {// Faib

label.text = "/";

}

yog sender.tag == 13 {// Multiply

label.text = "x";

}

yog tias sender.tag == 14 {// Rho tawm

label.text = "-";

}

yog sender.tag == 15 {// Ntxiv

label.text = "+";

}

ua haujlwm = sender.tag

performingMath = muaj tseeb;

}

lwm yog sender.tag == 16 {

yog ua haujlwm == 12 {// Faib

label.text = Txoj hlua (yav dhau losNumber / numberOnScreen)

}

lwm tus yog tias ua haujlwm == 13 {// Multiply

label.text = Txoj hlua (yav dhau losNumber * numberOnScreen)

}

lwm yog ua haujlwm == 14 {// Rho tawm

label.text = Txoj hlua (yav dhau losNumber - numberOnScreen)

}

lwm yog kev ua haujlwm == 15 {// Ntxiv

label.text = Txoj hlua (yav dhau losNumber + numberOnScreen)

}

}

lwm yog sender.tag == 11 {

ntawv.text = ""

yav dhau losNumber = 0;

numberOnScreen = 0;

kev ua haujlwm = 0;

}

Qhov tseem ceeb, daim ntawv no qhia txog ib qho ntawm lwm cov nyees khawm thaum nws raug nias thiab ua tiav los laij cov lus teb kawg thiab nthuav tawm nws ntawm daim ntawv lo.

Kauj ruam 9: Tag Nrho Cov Cai

Txoj Cai Tag Nrho
Txoj Cai Tag Nrho

Yog tias koj tsis xav mus dhau thiab tsim lub tshuab xam zauv ib kauj ruam nrog kuv, tom qab ntawd koj tuaj yeem yooj yim ntxiv cov ntsiab lus rau hauv koj zaj dab neeg thiab luam thiab muab cov lej tag nrho tso rau hauv koj tus saib xyuas. Nov yog cov cai:

ntshuam UIKit

chav ViewController: UIViewController {

var numberOnScreen: Ob npaug = 0;

var yav dhau losNumber: Ob npaug = 0;

var performingMath = cuav;

var ua = 0;

@IBAction func tooj (_ tus xa: UIButton) {

yog tias ua tauMath == muaj tseeb {

label.text = Txoj hlua (sender.tag-1)

numberOnScreen = Ob npaug (ntawv lo lus!)!

performingMath = tsis tseeb

}

lwm tus {

label.text = ntawv.text! + Txoj hlua (sender.tag-1)

numberOnScreen = Ob npaug (ntawv lo lus!)!

}

}

@IBOutlet tsis muaj zog var daim ntawv lo: UILabel!

@IBAction func nyees khawm (_ tus xa: UIButton) {

yog label.text! = "" && sender.tag! = 11 && sender.tag! = 16 {

yav dhau losNumber = Ob npaug (ntawv lo lus!)!

yog sender.tag == 12 {// Faib

label.text = "/";

}

yog sender.tag == 13 {// Multiply

label.text = "x";

}

yog tias sender.tag == 14 {// Rho tawm

label.text = "-";

}

yog sender.tag == 15 {// Ntxiv

label.text = "+";

}

ua haujlwm = sender.tag

performingMath = muaj tseeb;

}

lwm yog sender.tag == 16 {

yog ua haujlwm == 12 {// Faib

label.text = Txoj hlua (yav dhau losNumber / numberOnScreen)

}

lwm tus yog tias ua haujlwm == 13 {// Multiply

label.text = Txoj hlua (yav dhau losNumber * numberOnScreen)

}

lwm yog ua haujlwm == 14 {// Rho tawm

label.text = Txoj hlua (yav dhau losNumber - numberOnScreen)

}

lwm yog kev ua haujlwm == 15 {// Ntxiv

label.text = Txoj hlua (yav dhau losNumber + numberOnScreen)

}

}

lwm yog sender.tag == 11 {

ntawv.text = ""

yav dhau losNumber = 0;

numberOnScreen = 0;

kev ua haujlwm = 0;

}

}

override func viewDidLoad () {

super.viewDidLoad ()

// Ua ib qho kev teeb tsa ntxiv tom qab thauj khoom pom, feem ntau los ntawm tus nib.

}

override func didReceiveMemoryWarning () {

super.didReceiveMemoryWarning ()

// Muab pov tseg ntawm cov peev txheej uas tuaj yeem rov tsim dua.

}

}

Pom zoo: