Cov txheej txheem:

Tsim Scroll View Nrog Swift: 9 Kauj Ruam
Tsim Scroll View Nrog Swift: 9 Kauj Ruam

Video: Tsim Scroll View Nrog Swift: 9 Kauj Ruam

Video: Tsim Scroll View Nrog Swift: 9 Kauj Ruam
Video: ib sim cas tsis tsim nyog tsom xyooj 2022 2024, Kaum ib hlis
Anonim
Image
Image

Ob peb lub hlis dhau los, Kuv tsis paub tias muaj kev nrawm thiab Xcode. Hnub no, kuv muaj peev xwm los tsim ib feem me me ntawm daim ntawv thov uas kuv xav tsim. Kuv muaj peev xwm tsim qee yam txias, uas kuv xav qhia rau koj.

Hauv qhov kev qhia no kuv yuav coj koj mus dhau cov txheej txheem ntawm kev tsim kom pom qhov tshwm sim, uas cov neeg siv yuav raug xa rov qab thaum lawv xav sau npe tus account tshiab. Nrog txoj kev, Kuv yuav nco ntsoov muab qee qhov kev qhia theoretical rau koj ntawm yam peb ua, yog li koj tuaj yeem nkag siab qhov nws yog qhov peb tab tom ua.

Ua ntej peb ua qhov ntawd, cia tham txog Swift thiab Xcode yog dab tsi:

1. Swift yog cov lus muaj zog thiab siv tau yooj yim rau macOS, iOS, watchOS thiab tvOS. Kev sau Swift txoj cai yog sib tham sib thiab lom zem, cov ntsiab lus luv luv tsis tau hais tawm, thiab Swift suav nrog cov yam ntxwv tshiab niaj hnub tsim los nyiam. Swift code muaj kev nyab xeeb los ntawm kev tsim, tseem tseem tsim cov software uas ua haujlwm nrawm-nrawm. Nws tau tsim los ua haujlwm nrog Apple's Cocoa thiab Cocoa Touch cov txheej txheem thiab lub cev loj ntawm Lub Hom Phiaj-C uas twb muaj lawm sau rau Apple cov khoom. Nws tau tsim nrog qhov qhib qhov LLVM cov ntaub ntawv sau ua ke thiab tau suav nrog hauv Xcode txij li version 6, tso tawm xyoo 2014. Ntawm Apple platforms, nws siv Lub Hom Phiaj-C runtime library uas tso cai C, Lub Hom Phiaj-C, C ++ thiab Swift code kom khiav hauv ib qhov program.

2. Xcode yog kev txhim kho ib puag ncig ib puag ncig (IDE) rau macOS uas muaj cov cuab yeej tsim kho software tsim los ntawm Apple rau tsim software rau macOS, iOS, watchOS, thiab tvOS.

Kauj ruam 1: Rub tawm Xcode

Ua haujlwm hauv UI
Ua haujlwm hauv UI

Xcode 10 suav nrog txhua yam koj xav tau los tsim cov apps zoo rau txhua lub Apple platform. Tam sim no Xcode thiab Kev Ntsuas zoo nyob hauv qhov Tsaus Hom Tshiab ntawm macOS Mojave. Lub hauv paus tus lej editor tso cai rau koj hloov pauv lossis rov ua dua tus lej yooj yim dua, saib qhov kev hloov pauv hloov pauv nrog cov kab uas cuam tshuam, thiab tau txais cov ntsiab lus sai ntawm cov cai sib txawv. Koj tuaj yeem tsim koj tus kheej cov cuab yeej nrog kev pom kev zoo thiab kev tshuaj xyuas cov ntaub ntawv. Swift suav sau software sai dua, pab koj xa cov ntawv thov nrawm dua, thiab tsim txawm tias me dua. Cov suites ua tiav ntau zaus sai dua, ua haujlwm nrog pab pawg yog yooj yim dua thiab nyab xeeb dua, thiab ntau ntxiv.

Xcode 10 suav nrog Swift 4.2, uas suav koj cov software nrawm dua, pab koj xa cov ntawv thov sai dua, thiab tsim txawm tias me me binaries. Piv rau Swift 4.0, qhov hloov tshiab Swift tshiab tuaj yeem tsim cov apps loj ntau dua li ob zaug sai dua.* Ua ke nrog Xcode tshiab tsim cov txheej txheem, koj hloov kho niaj hnub, tsim, thiab sim ua haujlwm nrawm dua. Ua kom tau zoo rau qhov khoos phis tawj ntau-ntxhais Mac khoos phis tawj, Xcode thiab Swift ua rau lub tshuab tsim hluav taws xob nrawm.

Kauj Ruam 2: Cia Peb Pib

Image
Image

Yog li peb yuav ua dab tsi yog mus rau Xcode thiab tsim txoj haujlwm tshiab. Thaum peb nyem qhov haujlwm tshiab peb daim ntawv thov yuav yog ib qho pom app. Rau cov uas tsis paub, ib qho pom app txhais tau tias koj yuav tsum pib txhua yam los ntawm kos thiab tias yuav muaj ib qho kev pom uas peb tuaj yeem ua haujlwm.

Npe koj cov khoom TutorialApp. Yog tias koj yog tus tsim kev paub uas tshaj tawm cov apps hauv App Store koj yuav zaum muaj pab pawg, tab sis yog tias koj yog tus tshiab thiab koj tsis muaj daim ntawv thov tso tawm, koj tuaj yeem hla daim ntawv no. Hauv lub koom haum lub npe, koj tuaj yeem sau lub npe ntawm lub tuam txhab yog tias koj muaj ib qho, hauv kuv qhov xwm txheej kuv tsuas yog khaws MacBook Pro. Tom qab ntawd, Lub Koom Haum Qhia Tus Kheej tau txiav txim siab ua qee yam zoo li tus cim tshwj xeeb ntawm koj txoj haujlwm, yog li ntawd, koj tuaj yeem sau txhua yam koj xav tau. Lus yuav twv yuav raug sai.

Yog li, nias tom ntej thiab cia peb txuag txoj haujlwm ntawm lub desktop yog li nws nkag tau yooj yim.

Txoj haujlwm tshiab suav nrog peb cov ntaub ntawv, AppDelegate.swift, ViewController.swift, thiab lub hnub qub ntawm qhov kev qhia no: Main.storyboard. Under Deployment Info> Device Orientation in the General project settings, set Devices to iPhone. Txij li qhov no yog daim duab-tsuas yog daim ntawv thov, tshem tawm Txoj Kev Toj roob hauv pes Leftand Toj roob hauv pes txoj cai xaiv. Qhib Main.storyboard hauv qhov project navigator los saib nws hauv Interface Buildereditor:

Peb yuav tsis hloov pauv hauv kev teeb tsa thiab peb yuav mus ncaj mus rau lub ntsiab lus tseem ceeb. Txij li thaum peb ua ib qho app saib, peb tsim tau yooj yim ib leeg, dawb paug, saib. Nov yog qee yam peb yuav tsum tau ua haujlwm.

Kauj ruam 3: Ua haujlwm hauv UI

Ua haujlwm hauv UI
Ua haujlwm hauv UI
Ua haujlwm hauv UI
Ua haujlwm hauv UI

Cov lus piav qhia zaj dab neeg raug rau tus saib xyuas yog "xwm txheej", tab sis koj tuaj yeem siv cov lus sib pauv. Ib qho xwm txheej sawv cev rau tus saib saib hauv zaj dab neeg.

Ntawm no koj pom ib tus tswj hwm saib uas muaj qhov pom tsis pom. Cov xub taw qhia rau tus saib kev saib xyuas los ntawm sab laug qhia tias nws yog thawj qhov saib xyuas kom pom tshwm rau zaj dab neeg no. Kev tsim cov txheej txheem hauv cov ntawv sau zaj dab neeg tau ua tiav los ntawm kev rub cov tswj los ntawm Lub Tsev Qiv Ntawv (saib lub kaum sab xis saum toj) rau hauv koj li kev tswj hwm.

Txhawm rau kom tau txais kev xav li cas tus neeg sau zaj dab neeg ua haujlwm, rub qee qhov kev tswj hwm los ntawm Lub Tsev Qiv Ntawv mus rau qhov saib tsis pom kev zoo li pom hauv daim vis dis aus.

Raws li koj rub cov kev tswj hwm hauv, lawv yuav tsum tshwm nyob rau hauv Daim Ntawv Qhia Txheej Txheem ntawm sab laug.

Koj tuaj yeem tsim Tus Neeg Siv Interface uas koj xav tau. Hauv kuv qhov xwm txheej, kuv siv qhov uas koj pom hauv daim duab.

Kauj Ruam 4: Tsim Tus Saib Xyuas Thib Ob thiab Pib Ua Segues (hloov pauv)

Image
Image
Tsim Txoj Kab Rov tav Los Ntawm Nplooj Ntawv
Tsim Txoj Kab Rov tav Los Ntawm Nplooj Ntawv

Yog li, hauv kuv li app, thaum tus neeg siv nias lub pob "Sau Npe Tus Kheej Tshiab", Kuv xav kom nws raug xa rov mus rau nplooj ntawv sau npe. Yog li rau lub hom phiaj ntawd, txhua nplooj ntawv tsuas yog qhov tshwm sim tshiab, tso tawm tshiab. Vim li ntawd, peb yuav tsum tsim tus saib thib ob, uas koj tuaj yeem pom hauv lub tsev qiv ntawv kwv.

Ntaus tus tswj hwm thiab tso nws ib sab ntawm koj thawj zaug saib. Qhov xwm txheej no yuav muaj lub luag haujlwm rau tus saib xyuas npe. Kev xa rov mus rau nplooj ntawv ntawd tuaj yeem ua ob txoj hauv kev:

  1. peb tuaj yeem ua nws tus kheej thaum peb ua qhov txuas txuas los ntawm khawm mus rau lwm qhov kev saib xyuas
  2. peb tuaj yeem ua nws txoj haujlwm

Qhov kuv xaiv ua yog ua kom nws tus kheej. Nws yog qhov yooj yim uas:

  1. Ua ib sab laug nyem rau ntawm koj lub pob (hauv kuv qhov xwm txheej, Sau Npe Tus Kheej Tshiab)
  2. Tuav cov lus txib thiab sab laug nas nyem kom rub nws mus rau qhov chaw tswj hwm sau npe.
  3. Tso nws nyob ntawd thiab xaiv "Present Modally"

Kauj Ruam 5: Tsim Chav Kawm Rau Kev Sau Npe

Yog li, tam sim no peb xav tsim chav tshwj xeeb rau kev nkag mus rau chav tshiab.

Txhawm rau ua qhov no koj yuav tsum ua cov theem hauv qab no:

  • txoj nyem rau ntawm koj qhov project folder
  • nyem rau ntawm cov ntawv tshiab hu ua cocoa touch class
  • hauv chav kawm sau: Sau npe rau npe
  • Tseem ceeb heev! Nco ntsoov tias cov subclass yuav tsum yog hom UIViewController
  • lus yuav tsum ceev.
  • nyem tom ntej thiab txuag koj chav kawm cocoa sab hauv hauv lub hauv paus tseem ceeb ntawm koj txoj haujlwm.
  • Nyem rau ntawm zaj dab neeg tseem ceeb thiab mus rau tus saib tshiab
  • nyem rau ntawm lub pob daj uas tau muab tso rau saum nws
  • nyob rau hauv txoj cai mus rau tus kws tshuaj xyuas hauv chav kawm thiab ua kom siv mus rau npe VC (Costum chav kawm, chav kawm = RegisterVC

Kauj Ruam 6: Tsim Kab Rov Los Los Ntawm Nplooj Ntawv

Hauv iOS, scroll saib tau siv los saib cov ntsiab lus uas tsis haum rau ntawm lub vijtsam. Scroll views muaj ob lub hom phiaj tseem ceeb:

Txhawm rau kom cov neeg siv rub thaj tsam ntawm cov ntsiab lus lawv xav tso tawm, kom cov neeg siv nthuav dav rau lossis tawm ntawm cov ntsiab lus tso tawm uas siv lub tes taw. Ib qho kev tswj hwm ib txwm siv hauv iOS apps - UITableView - yog chav kawm qis ntawm UIScrollView thiab muab txoj hauv kev zoo los saib cov ntsiab lus uas loj dua lub vijtsam.

Dab tsi siv cov nplooj ntawv qis hauv kab rov tav?

Zoo, yog tias kuv tau tsim 6 nplooj ntawv sib txawv, nws yuav txhais tau tias kuv yuav tsum tsim chav kawm tshwj xeeb rau txhua tus ntawm lawv thiab nws tsis yooj yim kom dhau cov ntaub ntawv los ntawm ib chav mus rau lwm chav. Thaum piv txwv li kuv ntaus kuv tus email thiab nyem rau tom ntej, yog tias kuv muaj tus saib pom sib txawv, kuv yuav tawm ntawm thawj nplooj ntawv ntawm Tus Saib Xyuas thiab tom qab ntawv thib ob yuav nthuav tawm. Hauv qhov no, cov ntaub ntawv ntawm thawj tus saib xyuas, yuav tsum raug xa mus rau tom ntej thiab tom qab ntawd rov mus rau tus saib thib peb thiab lwm yam. Thaum kuv muaj tag nrho cov saib saib kuv xav tau, Kuv yuav tau sau tag nrho cov ntaub ntawv los ntawm txhua qhov cov nplooj ntawv thiab xa lawv mus rau server. Yog li, qhov no yuav nyuaj heev.

Yog li, txav mus rau kev tsim cov tswj hwm saib no, hauv kuv qhov xwm txheej, kuv muaj 5 nplooj ntawv uas kuv xav tsim:

  1. Email
  2. Lub Npe Tag Nrho
  3. Lo lus zais
  4. Hnub yug
  5. Tub los ntxhais

Qhov no txhais tau tias, tus saib kev saib xyuas uas peb yuav tsim, yuav tsum yog 5 npaug loj dua qhov peb tau ua ua ntej.

Xaiv koj tus tswj hwm saib thiab mus rau sab saud, kaum sab xis thiab nyem rau ntawm tus cim kav thiab hloov kho Simulated Loj. Koj yuav xaiv Freeform txhawm rau kho qhov dav thiab qhov siab. Qhov dav dav ntawm lub vijtsam uas tsim nyog rau iphone 8 yog 375, yog li yog tias kuv muab 375*5 = 1875. Thiab ntawm no koj mus, koj muaj qhov txuas ntxiv saib.

Ib yam nkaus, koj ua raws cov txheej txheem tib yam rau txhua lub xov tooj sib txawv thiab qhov loj me ntawm lub vijtsam.

Txhawm rau ua kom cov ntawv pom ua haujlwm, peb xav tau cov khoom scroll. Scroll View muab cov txheej txheem los tso saib cov ntsiab lus uas loj dua qhov loj ntawm daim ntawv thov lub qhov rais. Nyem rau ntawm qhov khoom no, rub nws thiab tso nws rau ntawm lub ces kaum sab saud ntawm tus saib thiab xyuas kom tseeb tias X thiab Y nyob ntawm xoom txoj haujlwm thiab ncab raws li qhov koj saib.

Scroll View tsuas yog tso cai rau peb nqes mus, tsis muaj dab tsi ntxiv. Tom qab ntawd peb yuav tsum ntxiv cov ntsiab lus pom, uas yuav khaws cia lwm qhov kev pom. Koj tuaj yeem pom UIView - nws sawv cev rau thaj tsam ib puag ncig uas nws kos thiab tau txais cov xwm txheej - hauv cov tsev qiv ntawv khoom. Ib qho yooj yim, nyem thiab rub nws mus rau hauv kab ntawv saib thiab ib zaug ntxiv, ncab nws kom haum.

Xaiv cov ntawv pom los ntawm lub vaj huam sib luag sab laug thiab peb yuav hu rau kab ntawv 0, 0, 0, 0 thiab ntxiv kev txwv. Ua tib yam rau cov ntsiab lus pom.

Kauj Ruam 7: Tsim Tus Neeg Siv Kev Sib Tham rau Sub-nplooj ntawv ntawm Kab Rov Tav Los

Image
Image
Siv Kev Tsim Tus Qauv hauv Xcode
Siv Kev Tsim Tus Qauv hauv Xcode

Hauv theem no, koj yuav tsum tsim UI ntawm koj nplooj ntawv qis. Qhov kuv xaiv ua, yog ua tus qauv hauv Sketch thiab tom qab ntawd tsim nws hauv Xcode.

Kauj Ruam 8: Siv Tus Qauv Tsim hauv Xcode

Siv Kev Tsim Tus Qauv hauv Xcode
Siv Kev Tsim Tus Qauv hauv Xcode
Siv Kev Tsim Tus Qauv hauv Xcode
Siv Kev Tsim Tus Qauv hauv Xcode

Cov kauj ruam tom ntej yog siv qhov kev tsim qauv no hauv Xcode. Txhawm rau ua li ntawd, koj yuav tsum tsim kev sib txuas txuas rau txhua nplooj ntawv hauv qab thiab tsim lwm qhov rau "niam saib", lub ntsiab lus, ib qho txuas txuas rau tag nrho cov saib xyuas.

Cov ntsiab lus hauv zaj dab neeg txuas nrog rau qhov chaws. Nws yog ib qho tseem ceeb kom nkag siab txog kev sib raug zoo uas zaj dab neeg muaj rau tus lej koj sau.

Hauv zaj dab neeg, ib qho xwm txheej sawv cev rau ib qho screen ntawm cov ntsiab lus thiab feem ntau yog ib tus saib xyuas. Saib cov tswj hwm siv koj li app tus cwj pwm thiab tswj cov ntsiab lus ib leeg saib nrog nws cov txheej txheem ntawm kev saib ua ntej. Lawv ua haujlwm sib txuas cov ntaub ntawv ntawm lub app cov ntaub ntawv tus qauv, uas suav cov app cov ntaub ntawv, thiab cov kev pom uas tso tawm cov ntaub ntawv ntawd, tswj lub neej nyob ib puag ncig ntawm lawv cov ntsiab lus pom, tswj kev hloov pauv thaum lub cuab yeej tig, txhais cov lus qhia hauv koj li app, thiab siv tus cwj pwm los teb rau cov neeg siv tswv yim. Txhua tus saib cov khoom siv hauv iOS yog hom UIViewController lossis ib qho ntawm nws cov chav kawm.

Koj txhais tus cwj pwm ntawm koj saib cov tswj hwm hauv cov cai los ntawm kev tsim thiab siv kev cai saib xyuas cov pawg subclasses. Tom qab ntawd koj tuaj yeem tsim kev sib txuas ntawm cov chav kawm thiab cov xwm txheej hauv koj zaj dab neeg kom tau txais tus cwj pwm uas koj tau hais tseg hauv tus lej thiab tus neeg siv cuam tshuam uas koj tau hais hauv koj zaj dab neeg.

Xcode twb tau tsim ib chav kawm uas koj tau saib ua ntej, ViewController.swift, thiab txuas nws mus rau qhov xwm txheej koj tab tom ua haujlwm hauv koj zaj dab neeg. Thaum koj ntxiv ntau qhov xwm txheej, koj yuav ua qhov kev sib txuas no koj tus kheej hauv Tus Kws Tshuaj Ntsuam Xyuas Tus Kheej. Tus kws tshuaj xyuas tus kheej cia koj hloov cov khoom ntawm ib yam khoom hauv koj zaj dab neeg cuam tshuam nrog tus yam ntxwv ntawd tus kheej, xws li cov khoom hauv chav kawm twg.

Tsim Cov Hluav Taws Xob rau UI ElementsOutlets muab txoj hauv kev los siv cov khoom sib cuam tshuam-cov khoom koj tau ntxiv rau koj zaj dab neeg-los ntawm cov ntaub ntawv cov cai. Txhawm rau tsim lub qhov hluav taws xob, Tswj-rub los ntawm cov khoom tshwj xeeb hauv koj zaj dab neeg mus rau saib cov ntaub ntawv tswj. Kev ua haujlwm no tsim cov khoom ntiag tug rau cov khoom hauv koj cov ntaub ntawv saib xyuas, uas tso cai rau koj nkag mus thiab tswj cov khoom ntawd los ntawm cov cai ntawm runtime

  1. Qhib koj zaj dab neeg, Main.storyboard.
  2. Nyem rau Assistant khawm hauv Xcode toolbar ze rau saum kaum sab xis ntawm Xcode kom qhib tus pabcuam pabcuam. Yog tias koj xav tau chaw ntau ntxiv los ua haujlwm, tsoo qhov project navigator thiab thaj chaw siv hluav taws xob los ntawm txhaj rau Navigator thiab Utilities khawm hauv Xcode toolbar.
  3. Koj tseem tuaj yeem ua kom pom cov qauv qhia tawm.

Hauv cov ntawv xaiv tus khoos phis tawj, uas tshwm nyob rau sab saum toj ntawm tus pab tus kws kho mob, hloov tus pab cuam khoos phis tawj los ntawm Saib Ua Ntej mus Tsis Siv Neeg> ViewController.swift.

Nyem rau sub-page thiab rub mus rau chav kawm tsim nyog hauv cov cai.

Kauj Ruam 9: Ua ke Kev Yooj Yim

Image
Image
Ua ke Kev Ywj Pheej
Ua ke Kev Ywj Pheej

ZOO TSHAJ PLAWS

Ib qho yoj tes taw tshwm sim thaum tus neeg siv txav ib lossis ntau tus ntiv tes hla lub vijtsam hauv kab rov tav tshwj xeeb lossis ntsug. Siv UISwipeGestureRecognizer chav kawm txhawm rau txheeb xyuas cov lus piav tes.

Kev siv ntawm tes los so

Kauj Ruam 1: Ntxiv los so Gesture (s) hauv viewDidLoad () txoj kev

override func viewDidLoad () {super.viewDidLoad ()

cia swipeLeft = UISwipeGestureRecognizer (lub hom phiaj: tus kheej, ua: #selector (handleGesture)) swipeLeft.direction =.left self.view.addGestureRecognizer (swipeLeft)

cia swipeRight = UISwipeGestureRecognizer (lub hom phiaj: tus kheej, kev nqis tes ua: #selector (handleGesture)) swipeRight.direction =.right self.view.addGestureRecognizer (swipeRight)

cia swipeUp = UISwipeGestureRecognizer (lub hom phiaj: tus kheej, ua: #selector (handleGesture)) swipeUp.direction =.up self.view.addGestureRecognizer (swipeUp)

cia swipeDown = UISwipeGestureRecognizer (lub hom phiaj: tus kheej, kev nqis tes ua: #selector (handleGesture)) swipeDown.direction =.down self.view.addGestureRecognizer (swipeDown)}

Kauj Ruam 2: Txheeb xyuas qhov piav tes piav taw hauv handleGesture () txoj kev func handleGesture (piav tes piav taw: UISwipeGestureRecognizer) -> Void {yog gesture.direction == UISwipeGestureRecognizerDirection.right {print ("Los so Txoj Cai")} ntxiv yog gesture.direction == UISwipeGestureRecognizer. sab laug {luam ("Los so")} lwm qhov yog gesture.direction == UISwipeGestureRecognizerDirection.up {print ("Los so")} lwm qhov yog gesture.direction == UISwipeGestureRecognizerDirection.down {print ("Los so")}}

Hauv kuv app, kuv xav siv swipeRight, tab sis poob dawb los siv qhov uas tsim nyog dua rau koj daim ntawv thov.

Tam sim no, cia siv qhov no rau hauv peb cov cai.

Peb mus rau tus registerVC.swift uas peb tau tsim ua ntej thiab sau cov cai raws li koj pom hauv daim duab.

QHIA NTAWV NTAWV CODE

cia tam sim no_x tau txais txoj haujlwm tam sim no ntawm ScrollView (txoj haujlwm kab rov tav) cia qhov screen_width tau txais qhov dav ntawm lub vijtsam, txiav qhov loj me no cia new_x los ntawm txoj haujlwm tam sim no ntawm scrollview, kuv rov los ntawm qhov dav ntawm qhov screen yog current_x> 0 kom txog thaum nws ntau dua 0 - 0 yog nplooj 1.

Thiab peb tau ua tiav!

Ua haujlwm zoo!

Pom zoo: