// func touchesBegan(touches: Set<UITouch>, withEvent: UIEvent?) {
// if self.target == nil {
// return
// }
像重寫方法這種 把這個方法寫在另外一個方法里就會沒有重寫
寫代碼的時候注意層級關(guān)系
func addTarget(target:AnyObject?,action: Selector){
self.target = target
self.action = action
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if self.target == nil {
return
}
//判斷self.target是否實現(xiàn)了指定的方法
if self.target!.respondsToSelector(self.action!) {
self.target?.performSelector(self.action!, withObject: self)
}else{
print("按鈕點擊對應(yīng)的方法沒有實現(xiàn)")
}
}
懶加載
lazy var imageArray: [UIImage] = {
var tempArray = [UIImage]()
for item in 1...15 {
let image = UIImage.init(named: "32_\(item).jpg")
tempArray.append(image!)
}
return tempArray
}()
func btnAction(btn:YTButton) {
let detail = DetailViewController()
//傳值
detail.imageArray = self.imageArray
//!!!在詳情頁隱藏底部的標(biāo)簽欄
detail.hidesBottomBarWhenPushed = true
//跳轉(zhuǎn)
self.navigationController?.pushViewController(detail, animated: true)
}
類前面加static 要用類名才能調(diào)用 (類型方法)
let str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let c = str.characters[str.startIndex.advancedBy(section)]
tabBarItems 和 tabBar.Items
//顯示箭頭
cell?.accessoryType = .DisclosureIndicator
//1.圖片上的按鈕被點擊
func deleteAction() {
print("刪除!!!!!")
//將當(dāng)前圖片按鈕從界面上移除
self.removeFromSuperview()
//發(fā)送消息通知視圖控制器刷新界面
NSNotificationCenter.defaultCenter().postNotificationName("reloadView", object: nil)
}
//注冊成為觀察者
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadView", name: "reloadView", object: nil)
return 沒寫在最后 return后面的代碼會執(zhí)行
//定制navigationBar
//1.設(shè)置背景顏色
self.navigationBar.barTintColor = UIColor.blackColor()
//2.設(shè)置中間的文字顏色
//NSForegroundColorAttributeName 文字顏色的key
self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
//3.設(shè)置填充顏色
self.navigationBar.tintColor = UIColor.whiteColor()
//設(shè)置狀態(tài)欄的顏色為白色
UIApplication.sharedApplication().statusBarStyle = .LightContent
// 設(shè)置背景圖片
// 以圖片路徑的方式獲取圖片
bgImgView = UIImageView.init(frame: self.view.bounds)
bgImgView.image = UIImage.init(contentsOfFile: NSBundle.mainBundle().pathForResource(bgImgName, ofType: nil)!)
// 設(shè)置中間segement
let segement = UISegmentedControl.init(items: ["消息","電話"])
segement.frame = CGRectMake(0, 0, 100, 40)
segement.selectedSegmentIndex = 0
// 設(shè)置segement邊框顏色
segement.layer.borderColor = UIColor.whiteColor().CGColor
segement.layer.borderWidth = 1
// 設(shè)置segement背景顏色
// segement.backgroundColor = UIColor.whiteColor()
segement.tintColor = UIColor.whiteColor()
self.navigationItem.titleView = segement
按鈕切圓
//切圓
self.centerBtn.layer.masksToBounds = true
self.centerBtn.layer.cornerRadius = 50
textField的左右視圖
let leftImageView1 = UIImageView.init(frame: CGRectMake(0, 0, 40, 50))
leftImageView1.contentMode = .Center
leftImageView1.image = UIImage.init(named: "user")
userField.leftView = leftImageView1
userField.leftViewMode = .Always
self.bgView.addSubview(userField)
userField.delegate = self
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// self.outHandAnimation()
self.view.endEditing(true)
}
//MARK: - 監(jiān)聽鍵盤的彈起和收起
extension LoginViewController{
func addObserverAction() {
//當(dāng)鍵盤彈起和收起的時候,系統(tǒng)會自動通過消息中心去發(fā)送相應(yīng)的消息。
//UIKeyboardWillShowNotification -> 鍵盤將要彈起對應(yīng)的消息名
//UIKeyboardWillHideNotification -> 鍵盤將要收起對應(yīng)的消息名
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showKeyBoard:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideKeyBoard:", name: UIKeyboardWillHideNotification, object: nil)
}
//鍵盤彈起
func showKeyBoard(nof:NSNotification){
UIView.animateWithDuration(0.4) {
self.bgView.transform = CGAffineTransformMakeTranslation(0, -110)
}
}
//鍵盤收起
func hideKeyBoard(nof:NSNotification) {
UIView.animateWithDuration(0.4) {
self.bgView.transform = CGAffineTransformMakeTranslation(0, 0)
}
}
let story = UIStoryboard.init(name: "Register", bundle: nil)
let register = story.instantiateInitialViewController()
self.navigationController?.pushViewController(register!, animated: true)
訊飛語音開放平臺
//self.imgView = UIImageView.init(image: UIImage.init(data: (data?.picData)!))
self.imgView.image = UIImage.init(data: (data?.picData)!)//將不是YTTabBar的從系統(tǒng)的tabBar上移除
item.removeFromSuperview()
App Transport Security Settings
//獲取最外層的字典
let json = data.result.value
//獲取articles對應(yīng)的數(shù)組
let tarray = json?.objectForKey("articles”)
//耗時操作執(zhí)行完成后將子線程取消
NSThread.currentThread().cancel()
//- 方式3:隱式的創(chuàng)建一個子線程
func creatThread3() {
//1.創(chuàng)建一個后臺線程(子線程),創(chuàng)建好之后會自動啟動
self.performSelectorInBackground("longTimeOperation", withObject: nil)
}
//!!!圖片下載完成后回到主線程去展示圖片
//參數(shù)1:指定的方法
//參數(shù)2:指定的線程
//參數(shù)3:方法中的實參
//參數(shù)4:是否等待指定的方法執(zhí)行完成
//功能:在指定的線程中調(diào)用指定的方法
self.performSelector("showImage:", onThread: NSThread.mainThread(), withObject: image, waitUntilDone: false)
//MARK: - 兩個概念:
//1.任務(wù): 想要執(zhí)行的操作(想要做的事情)
//2.隊列: 用來存儲任務(wù)的容器(先進(jìn)先出)
//GCD的使用步驟:
//1.創(chuàng)建隊列(需要確定隊列的類型)
//2.創(chuàng)建任務(wù),并且將任務(wù)添加到隊列中.(確定任務(wù)的執(zhí)行方式)
//GCD會自動將任務(wù)從隊列中取出,放到對應(yīng)的線程中去執(zhí)行
//MARK: - 四個術(shù)語
//任務(wù)的執(zhí)行方式:
//1.同步:在當(dāng)前線程中執(zhí)行(不會去創(chuàng)建新的線程),需要馬上執(zhí)行任務(wù)
//2.異步:在另外一個線程中執(zhí)行(會創(chuàng)建新的線程)
//隊列的類型:
//1.并發(fā):隊列中的任務(wù)可以同時執(zhí)行(前提是能有多個線程)
//2.串行:隊列中的任務(wù)一個一個順序執(zhí)行
//在swift中使用OC的第三方庫
//1.將第三方庫的文件拖到工程中
//2.創(chuàng)建橋接文件
//a.通過新建文件創(chuàng)建一個.h文件,命名規(guī)范:XXX-Briding-Header
//b.在橋接文件中將需要使用的頭文件通過"#import"包含進(jìn)去
//c.設(shè)置工程文件
SDWebImage的使用 //通過SDWebImage做圖片的異步下載和緩存
//參數(shù)1:圖片的網(wǎng)路路徑對應(yīng)的url
//參數(shù)2:占位圖
//cell.iconImageView.setImageWithURL(NSURL.init(string: model.icon), placeholderImage: UIImage.init(named: "user_default"))
cell.iconImageView.sd_setImageWithURL(NSURL.init(string: model.icon), placeholderImage: UIImage.init(named: "user_default"))
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NSFileManager這個類
do { } catch { } 捕獲異常
caemitterlayer類
判斷一個類的對象判斷,,,可以通過它的屬性判斷,,還可以給它添加其它的屬性用來區(qū)分如tag值
請求網(wǎng)絡(luò)數(shù)據(jù)提問
注意我們創(chuàng)建connection和session會話都是調(diào)用的方法創(chuàng)建的而不是直接初始化
1.NSURLConnetion方式請求數(shù)據(jù)(ios9已經(jīng)被廢除)(只是簡單介紹了下沒有涉及get 。post等傳參數(shù)問題)
直接用這個類的方法而不是用對象的方法?用的方法的名稱是什么?帶了幾個參數(shù)?最后一個參數(shù)中閉包有哪幾個參數(shù)?如何將二進(jìn)制數(shù)據(jù)解析成json數(shù)據(jù),調(diào)用的是那個類的什么方法而不是對象的方法?
2.NSURLSession.sharedSession()這個方法快速創(chuàng)建的是默認(rèn)的會話?還有一個方法創(chuàng)建默認(rèn)會話模式 哪個方法?請求對象任務(wù)的執(zhí)行語句?
創(chuàng)建POST請求對象是用的哪個類?NSMutableURLRequest.init(URL: url!)
//b.將字符串轉(zhuǎn)換成二進(jìn)制
let sendData = sendStr.dataUsingEncoding(NSUTF8StringEncoding)
//3.根據(jù)會話模式創(chuàng)建session(創(chuàng)建默認(rèn)會話模式的session)
//let session = NSURLSession.init(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
//快速創(chuàng)建一個默認(rèn)會話模式的session
let session = NSURLSession.sharedSession()
//!!!圖片下載完成后回到主線程去展示圖片
//參數(shù)1:指定的方法
//參數(shù)2:指定的線程
//參數(shù)3:方法中的實參
//參數(shù)4:是否等待指定的方法執(zhí)行完成
//功能:在指定的線程中調(diào)用指定的方法
self.performSelector("showImage:", onThread: NSThread.mainThread(), withObject: image, waitUntilDone: false)
單例的創(chuàng)建方式
import UIKit
class ValueManager: NSObject {
// 保證當(dāng)前這個類只能創(chuàng)建出一個對象,而且這個對象必須defalutManager去拿到
// 拿到當(dāng)前這個類唯一的對象
static let defalutManager = ValueManager()
// 構(gòu)造方法私有化
private override init() {
}
// 2.聲明一個屬性的類型是需要傳的值的類型
var sendValue = ""
}
//self.imgView = UIImageView.init(image: UIImage.init(data: (data?.picData)!))
self.imgView.image = UIImage.init(data: (data?.picData)!)//將不是YTTabBar的從系統(tǒng)的tabBar上移除
item.removeFromSuperview()
//設(shè)置每個cell之間的最小間距(行間距和列間距)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0 //設(shè)置這個屬性就是直接設(shè)置了每一行cell之間的間距
tableView collectionView 設(shè)置cell,foot,header一般是在創(chuàng)建這三個對象的時候就就改變frame和內(nèi)容
UITableViewCell中有兩個header 1.是textLabel 2.detailTextLabel
//5.
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return ["A","B","C","D","E"]
}
//數(shù)據(jù)源數(shù)組
lazy var dataArray: [NSDictionary] = {
var tempArray = [NSDictionary]()
//1.獲取plist文件的路徑
let path = NSBundle.mainBundle().pathForResource("data2.plist", ofType: nil)
//2.拿到plist文件最外層的數(shù)組
//將plist文件轉(zhuǎn)換成OC的數(shù)組(前提是plist的最外層是數(shù)組)
//參數(shù):plist文件的路徑
let plistArray = NSArray.init(contentsOfFile: path!)
return tempArray
}()
//CGSizeMake(1000,100)是最大值
let nameSize = nameStr.boundingRectWithSize(CGSizeMake(1000,100),options: .USesLineFramentOrigin,attributes:[NSFontAttributeName: UIFont.systemFontOfSize(13)],context:nil).size
//設(shè)置title和image
for (i,value) in self.tabBar.items!.enumerate() {
value.title = titleArray[i]
//item字體相關(guān),字體是一個title一個tile設(shè)置的要用循環(huán)拿到每個title
value.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.lightGrayColor(),NSFontAttributeName: UIFont.systemFontOfSize(15)], forState: .Normal)
在tabBar中設(shè)置title字體
value.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(15), NSForegroundColorAttributeName:UIColor.orangeColor()],forState: .Selected)
}
然而在navigationBar中設(shè)置title是
self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
item.image = UIImage.init(named: imageName[i])?.imageWithRenderingMode(.AlwaysOriginal)
item.selectedImage = UIImage.init(named: imageName[i] + "_1")?.imageWithRenderingMode(.AlwaysOriginal)
//第一次點擊放大,第二次點擊縮小
if self.imageView.frame.size.width == 200 {
self.imageView.frame = self.view.bounds
}else{
self.imageView.frame = CGRectMake(0, 0, 200, 200)
self.imageView.center = self.view.center
}
self.center = (touches.first?.locationInView(self.superview))!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// 停止UIView動畫
self.imagView.layer.removeAllAnimations()
// 讓形變清空
self.imagView.transform = CGAffineTransformIdentity
}
self.tabBar.translucent = false
//2.設(shè)置中間的文字顏色
//NSForegroundColorAttributeName 文字顏色的key
self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
// 需要顯示在Label上的文字
let str = "有的防護(hù)服的發(fā)揮份發(fā)動機(jī)減肥哈將對方尷尬發(fā)動 "
// 計算顯示指定文字所需要的最小空間
// 1.將swift的字符串轉(zhuǎn)換成oc的字符串
let ocStr = str as NSString
// 2.計算字符串大小
// 參數(shù)1:限制顯示當(dāng)前字符串當(dāng)前字符串的最大寬度和最大高度
// 參數(shù)2:設(shè)置渲染方式
// (UsesLineFragmentOrigin)
// 參數(shù)3:確定文字的字體(大小)
// NSFontAttributeName -> 字體對應(yīng)的Key值
// NSForegrouundColorAttributeName -> 文字顏色對應(yīng)的Key值
let strSize = ocStr.boundingRectWithSize(CGSizeMake(200, 100000), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(17)], context: nil).size
print(strSize)
// 創(chuàng)建label顯示文字
let label = UILabel.init(frame: CGRectMake(100, 100, strSize.width, strSize.height))
label.backgroundColor = UIColor.yellowColor()
self.view.addSubview(label)
label.text = str
// 自動換行
label.numberOfLines = 0 //必須要自動換行,不然會一行顯示
}
board = [[PointState]](count: 15, repeatedValue: [PointState](count: 15, repeatedValue: .Space))
//
// TapViewController.swift
// 02-手勢集合
//
// Created by 千鋒1 on 16/9/2.
// Copyright ? 2016年 1000phone. All rights reserved.
//
import UIKit
class TapViewController: YTViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 1.創(chuàng)建點擊手勢對象
// UIGestureRecongnizer是所有手勢的父類
// 參數(shù)1.調(diào)用方法的對象
// 參數(shù)2.方法對應(yīng)的選擇器 -> 這個方法如果帶參只能帶一個參數(shù),參數(shù)的對象就是當(dāng)前創(chuàng)建的點擊手勢對象
// 點擊手勢發(fā)生的時候讓參數(shù)1調(diào)用參數(shù)2中的方法
let tap = UITapGestureRecognizer.init(target: self, action: "tapActon:")
//核心屬性:點擊次數(shù)(默認(rèn)是1->單擊)
tap.numberOfTapsRequired = 2
// 2.將點擊手勢添加到視圖上(要求:添加手勢的視圖必須可以進(jìn)行用戶交互)
self.view.addGestureRecognizer(tap)
// 3.打開圖片的用戶交互,并添加點擊手勢
// 注意:同一個手勢對象,只能被添加到一個視圖
self.imagView.addGestureRecognizer(tap)
}
func tapAction(tap:UITapGestureRecognizer) {
print("點擊")
if self.imagView.frame.size.width == 200 {
self.imagView.frame = self.view.bounds
}
else {
self.imagView.frame = CGRectMake(0, 0, 200, 200)
self.imagView.center = self.view.center
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
// 將數(shù)據(jù)保存到手機(jī)硬盤中 -> 數(shù)據(jù)本地化
// ios中的數(shù)據(jù)本地化
// 1.文件操作(NSFileManager和NSFileHandle)
// 2.plist文件(NSUserDefaults)
// 3.數(shù)據(jù)庫
// 4.歸檔和解歸檔
// ios應(yīng)用程序中的本地路徑 -> 沙盒路徑
// 蘋果手機(jī)安裝的每個應(yīng)用程序都有一個專門的內(nèi)存空間用來存儲當(dāng)前應(yīng)用程序中產(chǎn)生的本地數(shù)據(jù) ,這個內(nèi)容空間就是沙盒。每個應(yīng)用程序?qū)?yīng)的沙盒是獨立的,相互不影響,而且相互不能訪問。
// =============沙盒===============
//!!!面試常問
// 1.拿到沙盒路徑
// 如果應(yīng)用程序是在真機(jī)上,拿到的就是真機(jī)的沙盒目錄,如果是在模擬器上可以拿到模擬器對應(yīng)的沙盒目錄
// a.Documents:存到這個目錄下的數(shù)據(jù)除非講應(yīng)用程序卸載,否則里面的數(shù)據(jù)會自動銷毀
let documentPath = NSHomeDirectory() + "/Douments"
// b.Library:在程序更新的時候,數(shù)據(jù)會自動被刪除
// Caches:專門用來存儲緩存數(shù)據(jù) 在清除緩存的時候就將這個文件夾的內(nèi)容刪除
let cachesPath = NSHomeDirectory() + "/Library/Caches"
// Preferences:(偏好設(shè)置)專門用來存儲設(shè)置性的數(shù)據(jù)
let prefencesPath = NSHomeDirectory() + "/Library/Preferences"
// temp:存到這個目錄下的數(shù)據(jù)在程序結(jié)束后會自動銷毀
let temp = NSHomeDirectory() + "/temp"
print(NSHomeDirectory())
command + shirft+ g
顯示隱藏文件
iTunes在與iPhone同步時,備份所有的Documents和Library文件。
iPhone在重啟時,會丟棄所有的tmp文件。
查看方法:
方法1、可以設(shè)置顯示隱藏文件,然后在Finder下直接打開。設(shè)置查看隱藏文件的方法如下:打開終端,輸入命名
(1)顯示Mac隱藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
(2)隱藏Mac隱藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
(3)輸完單擊Enter鍵,退出終端,重新啟動Finder就可以了 重啟Finder:鼠標(biāo)單擊窗口左上角的蘋果標(biāo)志-->強制退出-->Finder-->
現(xiàn)在能看到資源庫文件夾了。
打開資源庫后找到/Application Support/iPhone Simulator/文件夾。這里面就是模擬器的各個程序的沙盒目錄了。
方法2、這種方法更方便,在Finder上點->前往->前往文件夾,輸入/Users/username/Library/Application Support/iPhone Simulator/ 前往。
username這里寫用戶名。
//1.設(shè)置是否透明
self.tabBar.translucent = false
self.view.window?.rootViewController = YTTabBarController()
let item2 = UIBarButtonItem.init(image: UIImage.init(named: "itemImage")?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: "itemAction:")
//2.隱藏系統(tǒng)自帶的返回按鈕
self.navigationItem.hidesBackButton = true
let space1 = UIBarButtonItem.init(barButtonSystemItem: .FlexibleSpace, target: nil, action: "")
//b.FixedSpace(相當(dāng)于空格,用來設(shè)置每個item之間的間距,間距需要手動設(shè)置)
let space2 = UIBarButtonItem.init(barButtonSystemItem: .FixedSpace, target: nil, action: "")
//設(shè)置間距
space2.width = 20
//將item添加到toolBar上
self.toolbarItems = [space2,item1,space2,item2,space2,item3,space2,item4,space2]
//MARK:-警告框
func showAlert(text: String,action:((UIAlertAction)-> Void)?) {
//創(chuàng)建警告框
let alert = UIAlertController.init(title: nil, message: text, preferredStyle: .Alert)
//添加action
let action = UIAlertAction.init(title: "確定", style: .Default,handler: action)
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
}
閉包的寫法,尾隨閉包的幾種寫法
弄清繼承關(guān)系
弄清子類特有的,就、以及哪些是父類的。