概述
自學(xué) iOS 和 Swift 也有一段時(shí)間了偷线,最早嘗試寫(xiě)Demo時(shí)都是向著相對(duì)較完整的App方向進(jìn)行的,至此這樣“相對(duì)完整的App”也只完成了三個(gè)胃珍,但是到頭來(lái)學(xué)到的最大干貨只有 CoreData 和 JSON解析耍休,大部分時(shí)間用在不斷重復(fù)的布局設(shè)置等方面,甚至有時(shí)為了畫(huà)一些圖標(biāo)都要對(duì)著PS扣一晚上硬猫。所以對(duì)自己的學(xué)習(xí)安排做了一些改變,半個(gè)月前開(kāi)始盡量保證每天寫(xiě)一個(gè)簡(jiǎn)單的Demo改执,當(dāng)然這也是受到一些大佬們的啟發(fā)啸蜜,在網(wǎng)上有不少類(lèi)似于《自學(xué) iOS - 三十天三十個(gè) Swift 項(xiàng)目 》這樣的文章。
這次練習(xí)計(jì)劃的目的完全是為了掌握一些新的知識(shí)和技巧辈挂,省去了”為了讓Demo看上去更完整“而浪費(fèi)的時(shí)間盔性。
所有的Demo我都已經(jīng)開(kāi)源——GitHub鏈接 ,我沒(méi)有把這些練習(xí)放在一個(gè)工程里面所以在下面的每一個(gè)Demo的記錄中都會(huì)單獨(dú)附上鏈接呢岗。
因?yàn)槭亲詫W(xué)所以代碼規(guī)范方面一直有一些問(wèn)題,但是做練習(xí)的同時(shí)也需要不斷參考其他大佬們的代碼蛹尝,所以整個(gè)學(xué)習(xí)計(jì)劃其實(shí)也是見(jiàn)證自己的代碼逐漸規(guī)范化的一個(gè)過(guò)程后豫。Demo我會(huì)堅(jiān)持寫(xiě),寫(xiě)這篇文章的目的一方面是希望給有緣看到這篇文章的初學(xué)者們一些幫助突那,另一方面堅(jiān)持更新也是對(duì)自己學(xué)習(xí)過(guò)程的監(jiān)督挫酿。
Demo
1.自定義導(dǎo)航欄透明漸變
使用的是自定義導(dǎo)航欄,設(shè)置系統(tǒng)導(dǎo)航欄為透明的方法網(wǎng)上有很多但是在iOS 11中透明的導(dǎo)航欄默認(rèn)為白色還不知道怎么解決愕难,所以先用自定義的代替了早龟。透明漸變的功能在scrollViewDidScroll 這個(gè)方法中實(shí)現(xiàn)惫霸,根據(jù)滾動(dòng)偏移量計(jì)算百分比數(shù)值并將其作為導(dǎo)航欄透明度。
2.MapKit簡(jiǎn)單使用
MapKit的簡(jiǎn)單使用葱弟,import前需要進(jìn)行下面的設(shè)置:
添加地點(diǎn)時(shí)我是使用了CoreData壹店,如果只是單純的練習(xí)MapKit是沒(méi)必要這樣做的所以也算是對(duì)之前的知識(shí)做了復(fù)習(xí)。左劃刪除則是iOS 11里的新方法芝加,代替以前的editActionsForRowAtIndexPath 方法:
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let actionDel = UIContextualAction(style: .destructive, title: "刪除") { (action, view, finished) in
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
context.delete(self.fc.object(at: indexPath))
appDelegate.saveContext()
finished(true)
}
actionDel.backgroundColor = UIColor.red
return UISwipeActionsConfiguration(actions: [actionDel])
}
3.圖片無(wú)限輪播
使用scrollView實(shí)現(xiàn)圖片無(wú)限輪播的功能硅卢,在scrollView中添加左、中藏杖、右三個(gè)imageView将塑。使用下面的方法設(shè)置三個(gè)imageView的image:
func setImg() {
if index == 0 {
leftImg?.image = UIImage(named: imgs.last!)
midImg?.image = UIImage(named: imgs.first!)
rightImg?.image = UIImage(named: imgs[1])
} else if index == imgs.count - 1 {
leftImg?.image = UIImage(named: imgs[index - 1])
midImg?.image = UIImage(named: imgs.last!)
rightImg?.image = UIImage(named: imgs.first!)
} else {
leftImg?.image = UIImage(named: imgs[index - 1])
midImg?.image = UIImage(named: imgs[index])
rightImg?.image = UIImage(named: imgs[index + 1])
}
}
自動(dòng)滾動(dòng)則是通過(guò)設(shè)置定時(shí)器實(shí)現(xiàn)。
4.兩個(gè)tableView聯(lián)動(dòng)
這次的練習(xí)是參考了一位大佬的文章【Swift聯(lián)動(dòng)】:兩個(gè)TableView之間的聯(lián)動(dòng)蝌麸,TableView與CollectionView之間的聯(lián)動(dòng)
在眾多電商類(lèi)App中都有用到聯(lián)動(dòng)tableView的形式点寥。
5.新聞點(diǎn)擊展開(kāi)預(yù)覽
tableView自動(dòng)行高的使用,設(shè)置行高為 UITableViewAutomaticDimension 来吩,默認(rèn)設(shè)置新聞內(nèi)容label的行高為2敢辩,點(diǎn)擊后變?yōu)?(即全部顯示)
需要注意的是使用storyBoard進(jìn)行約束時(shí),必須將 tableViewCell 的 ContentView 從上到下填滿误褪,否則無(wú)法計(jì)算出當(dāng)前行高责鳍。
6.AVKit簡(jiǎn)單使用
系統(tǒng)自帶播放器的簡(jiǎn)單使用,點(diǎn)擊cell中的播放按鈕調(diào)用下面的方法:
@objc func playAction(_ sender: UIButton) {
let path = Bundle.main.path(forResource: "清醒夢(mèng)", ofType: "mp3")
videoPlayer = AVPlayer(url: NSURL(fileURLWithPath: path!) as URL)
playVideoController.player = videoPlayer
self.present(playVideoController, animated: true) {
self.playVideoController.player?.play()
}
}
這里因?yàn)槲译娔X里沒(méi)有現(xiàn)成的視頻文件所以偷懶用音頻文件代替了...
7.自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
這個(gè)練習(xí)感覺(jué)是至今為止寫(xiě)過(guò)最實(shí)用的一個(gè)??兽间,這次練習(xí)是分成兩次做的历葛。
- 自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà),創(chuàng)建跳轉(zhuǎn)和返回的兩個(gè)動(dòng)畫(huà)類(lèi)并分別實(shí)現(xiàn)UIViewControllerAnimatedTransitioning協(xié)議嘀略,具體的動(dòng)畫(huà)寫(xiě)在animateTransition()方法里恤溶,在fromView(也就是present方法的view)中實(shí)現(xiàn)UIViewControllerTransitioningDelegate協(xié)議。這里我是實(shí)現(xiàn)了類(lèi)似于添加導(dǎo)航欄后系統(tǒng)自帶的push動(dòng)畫(huà)帜羊。
- 手勢(shì)交互咒程,同樣是模仿系統(tǒng)push動(dòng)畫(huà)的拖動(dòng)返回手勢(shì),在toView中添加pan手勢(shì)讼育,創(chuàng)建UIPercentDrivenInteractiveTransition屬性并實(shí)現(xiàn)手勢(shì)百分比更新帐姻,感謝大佬的文章關(guān)于自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà),我都告訴你奶段。:
var secondViewController = SecondViewController()
@objc func panAction(panGesture: UIPanGestureRecognizer) {
let panPercent = panGesture.translation(in: self.view).x / self.view.frame.width
switch panGesture.state {
case .began:
self.percentDrivenTransition = UIPercentDrivenInteractiveTransition()
self.dismiss(animated: true, completion: nil)
case .changed:
self.percentDrivenTransition?.update(panPercent)
case .ended, .cancelled:
if panPercent > 0.5 {
self.percentDrivenTransition?.finish()
} else {
self.percentDrivenTransition?.cancel()
}
self.percentDrivenTransition = nil
default:
break
}
}
8.tabBar簡(jiǎn)單使用
tabBar的簡(jiǎn)單使用饥瓷,主要是storyBoard上面的用法,前兩個(gè)view都是之前的練習(xí)復(fù)制過(guò)來(lái)的痹籍,第三個(gè)view則是簡(jiǎn)單copy了一下微信的個(gè)人信息界面......
(大紅色的導(dǎo)航欄可以說(shuō)是很蠢了)
9.WKWebView簡(jiǎn)單使用
使用WKWebView進(jìn)行網(wǎng)頁(yè)訪問(wèn):
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
self.view.addSubview(webView)
if let exUrl = URL(string: "https://www.baidu.com") {
let request = URLRequest(url: exUrl)
webView.load(request)
}
注意呢铆,蘋(píng)果現(xiàn)在要求App內(nèi)訪問(wèn)的網(wǎng)絡(luò)必須使用HTTPS協(xié)議,但是很多網(wǎng)頁(yè)是使用HTTP協(xié)議的蹲缠,所以需要在 Info.plist 文件內(nèi)做添加圖中的內(nèi)容:
同時(shí)復(fù)習(xí)了使用storyboard中的segue進(jìn)行傳值棺克。
(tableView和網(wǎng)頁(yè)訪問(wèn)的內(nèi)容悠垛,可以無(wú)視...)
10.拖動(dòng)手勢(shì)&圖片放大
這個(gè)練習(xí)是在 自定義動(dòng)畫(huà)第二部分-交互手勢(shì) 之前進(jìn)行的,重點(diǎn)有兩個(gè):
- 練習(xí)pan拖拽手勢(shì)的使用娜谊,根據(jù)拖拽偏移量計(jì)算一些相關(guān)數(shù)值以實(shí)現(xiàn)實(shí)時(shí)的動(dòng)畫(huà)效果确买,同時(shí)也復(fù)習(xí)了動(dòng)畫(huà)的簡(jiǎn)單使用
- 將imageView嵌套在ScrollView中,以此實(shí)現(xiàn)圖片放大的效果
有兩個(gè)小知識(shí)點(diǎn)需要注意:
- 實(shí)現(xiàn)父視圖半透明而子試圖不透明的效果因俐,可以通過(guò)設(shè)置
view.backgroundColor = UIColor.black.withAlphaComponent(0.9)
來(lái)實(shí)現(xiàn) - 如果要對(duì)UIImageView添加點(diǎn)擊拇惋、拖拽等手勢(shì),需要先設(shè)置
imageView.isUserInteractionEnabled = true
11.仿聊天界面
這個(gè)練習(xí)耗費(fèi)的時(shí)間比較多抹剩。
- 最開(kāi)始的時(shí)候我對(duì)發(fā)送和接受氣泡的處理是寫(xiě)在同一個(gè)cell里撑帖,判斷是發(fā)送消息還是接受消息后將另外一組氣泡圖片和label隱藏,但是仍然需要寫(xiě)兩個(gè)cell(時(shí)間cell 和 消息cell)澳眷,所以我沒(méi)有使用storyboard而是想純代碼布局胡嘿。因?yàn)樯婕暗搅俗詣?dòng)行高需要做約束,所以使用了SnapKit钳踊,但是由于個(gè)人能力有限不知道哪里出了錯(cuò)衷敌,失敗了...不能設(shè)置自動(dòng)行高。所以簡(jiǎn)單了解了xib的使用后用xib設(shè)置tableViewCell拓瞪,同時(shí)對(duì)發(fā)送接收的處理?yè)Q了方法——發(fā)送和接收設(shè)置兩個(gè)tableViewCell缴罗,判斷后返回相應(yīng)的cell,達(dá)到了目的效果祭埂。
- 信息發(fā)送欄隨鍵盤(pán)的彈出和隱藏而改變高度面氓,在viewDidLoad中設(shè)置鍵盤(pán)彈出和隱藏的通知并在方法中獲取到鍵盤(pán)高度,使用動(dòng)畫(huà)改變發(fā)送欄的高度蛆橡。
12.仿QQ個(gè)人資料頁(yè)下拉背景圖放大效果
實(shí)現(xiàn)了類(lèi)似于QQ個(gè)人資料頁(yè)下拉背景圖放大的效果舌界,依然是在scrollViewDidScroll這個(gè)方法里做文章,當(dāng)y方向偏移量<= 0 時(shí)泰演,讓圖片的高度等于偏移量也就是將圖片的高度固定不動(dòng)呻拌,計(jì)算偏移量和屏幕寬度的比值,增加imageView的寬和高睦焕。同時(shí)為了整體效果我把第一個(gè)練習(xí)里的透明漸變導(dǎo)航欄也加進(jìn)來(lái)了藐握。
13.tableView中嵌入collectionView
實(shí)現(xiàn)tableViewCell中嵌入collectionView,點(diǎn)擊collectionView中的圖片垃喊,將值傳給tableView后單獨(dú)刷新第一行的數(shù)據(jù)實(shí)現(xiàn)改變圖片效果猾普。同樣是在電商類(lèi)App中較常見(jiàn)。
單獨(dú)刷新指定的
某個(gè)cell或某個(gè)section:
let index = IndexPath.init(row: 0, section: 0)
tableView.reloadRows(at: [index], with: .fade)
14.仿登錄界面但實(shí)質(zhì)是關(guān)鍵幀動(dòng)畫(huà)練習(xí)
- 昨天本來(lái)是想做個(gè)模仿Twitter啟動(dòng)動(dòng)畫(huà)的練習(xí)缔御,看了一些成品的Demo后發(fā)現(xiàn),關(guān)鍵幀動(dòng)畫(huà)真的妇蛀,無(wú)敵好玩8弧s猿伞!所以花了點(diǎn)時(shí)間看了看有關(guān)關(guān)鍵幀動(dòng)畫(huà)的東西眷茁。簡(jiǎn)單寫(xiě)了個(gè)動(dòng)畫(huà)但是時(shí)間太晚了沒(méi)有上傳炕泳。
所以今天連同昨天的練習(xí)一起,寫(xiě)了一個(gè)動(dòng)畫(huà)效果勉強(qiáng)還算好看(大概吧)的模擬登錄界面 - duration 設(shè)置動(dòng)畫(huà)時(shí)間上祈,在 values 中設(shè)置關(guān)鍵幀培遵,keytimes 中設(shè)置對(duì)應(yīng)的時(shí)間百分比。timingFunctions中設(shè)置對(duì)應(yīng)的速度控制登刺。因?yàn)樵O(shè)置了layer.cornerRadius切割成圓角籽腕,所以為了保持按鈕等組件的圓角不變?cè)谑褂藐P(guān)鍵幀動(dòng)畫(huà)的同時(shí)也混用了基礎(chǔ)動(dòng)畫(huà)。
- 在ListViewController中纸俭,模仿大佬的demo做了一個(gè)tableViewCell背景顏色梯度變化的效果皇耗,并且設(shè)置visibleCells將cell藏在屏幕底部,使用Spring動(dòng)畫(huà)實(shí)現(xiàn)彈性效果揍很。
15.本地推送
使用UserNotifications實(shí)現(xiàn)本地推送通知郎楼,代碼均寫(xiě)在AppDelegate中。參考自大神故胤道長(zhǎng)的 Swift30Projects
16.實(shí)現(xiàn)用戶更改頭像操作
調(diào)用系統(tǒng)的相機(jī)和相冊(cè)窒悔,模擬應(yīng)用中用戶更改頭像的操作呜袁。使用 UIImagePickerControllerDelegate 和 UINavigationControllerDelegate 這兩個(gè)代理,并調(diào)用完成圖片選擇的代理方法實(shí)現(xiàn)頭像更改:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imgView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismiss(animated: true, completion: nil)
}
-
注意:調(diào)用相冊(cè)和相機(jī)需要獲得訪問(wèn)權(quán)限简珠,在 Info.plist 文件中設(shè)置:
16-2.jpg
17.tableView設(shè)置索引
給tableView添加索引功能阶界,使用其代理方法 sectionForSectionIndexTitle:
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
var indexSection : Int = 0
for i in sections {
if i == title {
return indexSection
}
indexSection += 1
}
return 0
}
18.搜索欄使用
搜索欄的使用,定義一個(gè)UISearchController北救,使用UISearchResultsUpdating并實(shí)現(xiàn)其代理方法:
func updateSearchResults(for searchController: UISearchController) {
if var text = searchController.searchBar.text {
text = text.trimmingCharacters(in: .whitespaces)
searchFilter(text: text)
marvelTableView.reloadData()
}
}
使用數(shù)組的filter函數(shù)對(duì)原始數(shù)據(jù)進(jìn)行篩選以實(shí)現(xiàn)搜索功能:
func searchFilter(text: String) {
self.searchResults = movies.filter({ (movie) -> Bool in
return movie.name.localizedCaseInsensitiveContains(text)
})
}
19.仿微博界面 實(shí)現(xiàn)字?jǐn)?shù)限制功能
- 模仿發(fā)微博界面荐操,實(shí)現(xiàn)發(fā)微博時(shí)字?jǐn)?shù)限制為140字的功能,超出后右下角字?jǐn)?shù)統(tǒng)計(jì)label會(huì)變?yōu)殚偕?/li>
- 使用通知來(lái)實(shí)現(xiàn)字?jǐn)?shù)限制功能珍策。在viewDidLoad中添加通知:
NotificationCenter.default.addObserver(self, selector: #selector(textViewNotificationAction(notification:)), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
該通知調(diào)用方法 textViewNotificationAction :
@objc func textViewNotificationAction(notification: Notification) {
let limit: Int = 140
let text = self.textView.text as NSString
if text.length >= limit {
let str = text.substring(to: limit)
self.textView.text = str
self.limitLabel.text = "\(limit)"
self.limitLabel.textColor = UIColor.orange
} else {
self.limitLabel.textColor = UIColor.darkGray
self.limitLabel.text = "\(text.length)"
}
self.weiboDetail = String(text)
}
- 同時(shí)復(fù)習(xí)使用通知監(jiān)聽(tīng)鍵盤(pán)的彈出和隱藏托启,相關(guān)方法參考自簡(jiǎn)書(shū) swift實(shí)現(xiàn)ios類(lèi)似微信輸入框跟隨鍵盤(pán)彈出的效果
20.幾個(gè)自己不常用的控件練習(xí)合集
幾個(gè)以前沒(méi)用怎么用過(guò)的控件的練習(xí)合集,包括選擇器UISegmentedControl攘宙、滑塊UISlider屯耸、數(shù)字UIStepper、開(kāi)關(guān)UISwitch蹭劈。
21.tableView編輯疗绣、cell移動(dòng)、左右側(cè)滑
- 右劃置頂:
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let actionTop = UIContextualAction(style: .normal, title: "置頂") { (action, view, finished) in
let first = IndexPath(row: 0, section: 0)
tableView.moveRow(at: indexPath, to: first)
finished(true)
}
actionTop.backgroundColor = UIColor.orange
return UISwipeActionsConfiguration(actions: [actionTop])
}
- 左劃刪除:
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let actionDel = UIContextualAction(style: .destructive, title: "刪除") { (action, view, finished) in
self.ints.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
finished(true)
}
actionDel.backgroundColor = UIColor.red
return UISwipeActionsConfiguration(actions: [actionDel])
}
- 編輯:
需要先設(shè)置實(shí)現(xiàn)代理方法canEditRowAt铺韧,返回值為true多矮,練習(xí)中因?yàn)樾枰苿?dòng)cell所以也需要實(shí)現(xiàn)canMoveRowAt,并實(shí)現(xiàn)moveRowAt方法:
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let fromRow = (sourceIndexPath as NSIndexPath).row
let toRow = (destinationIndexPath as NSIndexPath).row
let int = ints[fromRow]
ints.remove(at: fromRow)
ints.insert(int, at: toRow)
}
22.使用截圖完成動(dòng)畫(huà)
使用截屏實(shí)現(xiàn)一些動(dòng)畫(huà)效果:
let snapshotView = selectedCell?.snapshotView(afterScreenUpdates: false)!
比如在demo中,CollectionView中的每一個(gè)Cell都有一個(gè)ImageView塔逃,動(dòng)畫(huà)開(kāi)始前使用上面的代碼給點(diǎn)擊的cell截圖讯壶,將其frame設(shè)置與原cell一致,并將原cell隱藏湾盗。接下來(lái)的位移及大小動(dòng)畫(huà)則均是以該截圖為操作對(duì)象伏蚊。
這種方法在自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)時(shí)更實(shí)用。
23.UIPageViewController的使用
- 練習(xí)中 PageViewController 的子視圖是動(dòng)態(tài)的格粪,重復(fù)調(diào)用 WebViewController 每一頁(yè)只是改變了 WKWebView 加載的url躏吊。在 PageViewController 中需要實(shí)現(xiàn)UIPageViewControllerDataSource的兩個(gè)方法,返回當(dāng)前頁(yè)VC的前一個(gè)和后一個(gè)VC:
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = (viewController as! WebViewController).index
index -= 1
return setViewController(index: index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = (viewController as! WebViewController).index
index += 1
return setViewController(index: index)
}
在 setViewController 方法中為 WebViewController 設(shè)置相應(yīng)的url和索引并返回
func setViewController(index: Int) -> WebViewController? {
if case 0..<urls.count = index {
let main = UIStoryboard.init(name: "Main", bundle: Bundle.main)
if let webVC = main.instantiateViewController(withIdentifier: "webView") as? WebViewController {
webVC.url = urls[index]
webVC.index = index
return webVC
}
}
return nil
}
- 練習(xí)中使用的url均選自簡(jiǎn)書(shū)-7日熱門(mén)帐萎。簡(jiǎn)書(shū)網(wǎng)址使用HTTP協(xié)議所以需要在 Info.plist 文件中設(shè)置 App Transport Security Settings - Allow Arbitrary Loads 為 YES
24.使用UIPickerView模擬老虎機(jī)
- 使用pickerView模擬“老虎機(jī)”比伏,模仿概述中提及的大佬的文章中的第14項(xiàng)。
- 使用 arc4random() 方法生成隨機(jī)數(shù)吓肋,在代理方法viewForRow中返回一個(gè)UILabel凳怨,以隨機(jī)數(shù)作為下標(biāo)選擇emojiArray中的Emoji表情并分別賦值給給每一行的label.text:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel = UILabel()
pickerLabel.font = UIFont(name: "Apple Color Emoji", size: 70)
pickerLabel.textAlignment = .center
switch component {
case 0:
pickerLabel.text = emojiArray[Int(arrayOne[row])]
case 1:
pickerLabel.text = emojiArray[Int(arrayTwo[row])]
case 2:
pickerLabel.text = emojiArray[Int(arrayThree[row])]
default:
break
}
return pickerLabel
}
25.簡(jiǎn)單的畫(huà)板功能
- 思路參考自:swift3.0 CoreGraphics繪圖-實(shí)現(xiàn)畫(huà)板
- 無(wú)視我的丑字 (其實(shí)用筆的話我的字還是可以的。是鬼。肤舞。(自我感覺(jué)良好))
- 創(chuàng)建一個(gè)DrawView類(lèi),重寫(xiě)draw方法:
override func draw(_ rect: CGRect) {
super.draw(rect)
let context = UIGraphicsGetCurrentContext()
context?.setLineCap(.round)
context?.setLineJoin(.round)
if allLines.count > 0 {
for i in 0..<allLines.count {
let linePoints = allLines[i]
if linePoints.count > 0 {
context?.beginPath()
context?.move(to: linePoints[0])
for j in 0..<linePoints.count {
context?.addLine(to: linePoints[j])
}
context?.setLineWidth(self.lineWidth)
context?.setStrokeColor(strokeColors[i])
context?.strokePath()
}
}
}
if currentPoints.count > 0 {
context?.beginPath()
context?.setLineWidth(self.lineWidth)
context?.setStrokeColor(self.strokeColor.cgColor)
context?.move(to: currentPoints[0])
for i in 0..<currentPoints.count {
context?.addLine(to: currentPoints[i])
}
context?.strokePath()
}
}
26.下拉刷新UIRefreshControl基本使用
- 下拉刷新控件UIRefreshControl的基本使用均蜜。
- 設(shè)置兩個(gè)保存Emoji表情的數(shù)組:
var emojiArray = ["","","","","","","","??","",""]
let refreshEmojiArray = ["","","","",""]
其中第一個(gè)數(shù)組是tableView默認(rèn)顯示的數(shù)據(jù)李剖,第二個(gè)則是提供給刷新方法使用:
@objc func refreshAction() {
let arcCount = UInt32(self.refreshEmojiArray.count)
let arc = Int(arc4random() % arcCount)
self.emojiArray.insert(refreshEmojiArray[arc], at: 0)
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}
使用arc4random()方法生成隨機(jī)數(shù)作為數(shù)組2的下標(biāo),并將對(duì)應(yīng)的字符串添加到數(shù)組1的最開(kāi)始位置(方便觀察刷新效果)囤耳。
- 用虛擬機(jī)做拖拽的動(dòng)作真的有點(diǎn)不自在無(wú)論是鼠標(biāo)還是觸控板的三只拖拽...所以演示圖片里好多次沒(méi)有拖好...
27.通過(guò)簡(jiǎn)單的單詞翻譯功能Demo練習(xí)JSON解析
- 使用第三方庫(kù):SwiftyJSON進(jìn)行JSON解析篙顺,接口來(lái)自扇貝單詞
- 主要目的還是,練習(xí)JSON充择,所以一些會(huì)導(dǎo)致程序崩潰的情況都沒(méi)有做處理 比如搜索欄的單詞必須正確不能有空格(好吧我承認(rèn)其實(shí)是因?yàn)橄胪祽辛说旅怠!W德蟆#?/li>
- 使用CAGradientLayer做了一個(gè)漸變的背景色宰僧,顏色提取自iPhone一張內(nèi)置壁紙:
func gradientColors() -> CAGradientLayer {
let topColor = UIColor(named: "topColor")!
let bottomColor = UIColor(named: "bottomColor")!
let gradientColors = [topColor.cgColor, bottomColor.cgColor]
let gradientLocations: [NSNumber] = [0.0, 1.0]
let gradientLayer = CAGradientLayer()
gradientLayer.colors = gradientColors
gradientLayer.locations = gradientLocations
return gradientLayer
}
28.計(jì)算最大公約數(shù)和最小公倍數(shù)
老實(shí)說(shuō),這不算是個(gè)練習(xí)观挎。至于為啥寫(xiě)了這么個(gè)東西琴儿,是因?yàn)榍皟商炜佳械氖矣眩瑢I(yè)課考C語(yǔ)言嘁捷,他做的題里有計(jì)算這倆東西的題造成,讓我拿Swift寫(xiě)一下。然后昨天晚上等球賽的時(shí)候雄嚣,把在playground上寫(xiě)給他演示的代碼寫(xiě)成了這個(gè)工程态秧。。。
29.tableView分組折疊/展開(kāi)效果
- 實(shí)現(xiàn)tableView分組折疊/展開(kāi)的效果。
- 參考項(xiàng)目的鏈接我找不到了科吭。。。
- 創(chuàng)建TableViewHeaderViewDelegate協(xié)議逆巍,給headerView添加tap點(diǎn)擊手勢(shì)并實(shí)現(xiàn)協(xié)議方法 tableViewHeaderClick
func tableViewHeaderClick(_ headerView: TableViewHeaderView, section: Int) {
let show = itemData[section].isShow
itemData[section].isShow = !show!
let index = IndexSet(integer: section)
self.tableView.reloadSections(index, with: .fade)
}
- 其實(shí)demo中只有一個(gè)headerView寫(xiě)協(xié)議是有點(diǎn)多余了的但是畢竟Swift核心是面向協(xié)議,平時(shí)多用協(xié)議對(duì)以后的學(xué)習(xí)也有好處
30.Swift 自定義UICollectionViewLayout
- 自定義UICollectionViewLayout 和 UICollectionViewFlowLayout封锉,demo中的兩個(gè)效果的教程分別來(lái)自UICollectionView自定義布局(二) 和 Swift之用CollectionViewFlowLayout實(shí)現(xiàn)LOL皮膚選擇動(dòng)畫(huà)绵跷。
- 三個(gè)重要方法:prepare(基本數(shù)據(jù))、 shouldInvalidateLayout(允許更新位置)成福、 layoutAttributesForElements(返回所有UICollectionViewLayoutAttributes)
寫(xiě)在最后
由于偶爾偷懶碾局,最后用了40天左右的時(shí)間寫(xiě)了這30個(gè)小練習(xí),雖然難度都不大但是我通過(guò)這30個(gè)練習(xí)確實(shí)學(xué)到了不少東西奴艾。下一步打算開(kāi)通開(kāi)發(fā)者賬號(hào)净当,試著以上架App Store為目的寫(xiě)一個(gè)完整的App。