最近、qiitaで自分にはまだ理解ができないけど、理解できる方達がコメントでのキャッチボールが羨ましく、ただぼーっと指を加えています。たまに指摘してもらえる時は、教えてもらっているのでキャチボールにはなってませんが、幸せは感じているものです。
ここでは、よく使うけど、記憶しておけないものを色々つらつら書いていきます。都度更新して最後はいい感じになることを願っています
参考:http://qiita.com/nacika_ins/items/b9ac96bc4e22d974cd00
UITableView
※スタイルの書き方(スタイルは2種類)
0がplain 1がgrouped
let tableview = UITableView(frame: frame, style: UITableViewStyle(rawValue: 0)!)
変数名の宣言なら
let tableview = UITableView(frame: frame, style: .plain)
let tableview = UITableView(frame: frame, style: .grouped)
swiftのプログラムマーク
// MARK: - ここに説明
※コメントと思って//←これ消すとエラーになるのです。
ボタンなどの位置、サイズなどの初期化、形、アクションの設定参考:アプリにボタンを配置するuibuttonの基本的な使い方
var myButton = UIButton()
myButton = UIButton(frame: CGRect(x: 250.0, y: 580.0, width:80.0 , height: 80.0))
myButton.addTarget(self, action: #selector(buttonTap(sender:)), for: .touchUpInside)
myButton.backgroundColor = UIColor.red
myButton.layer.cornerRadius = 40
view.addSubview(myButton)
func buttonTap(sender: UIButton) {
print("ボタンが押された")
}
myButton.addTarget(self, action: #selector(self.buttonTap(_:)), for: .touchUpInside)
func buttonTap(_ sender: AnyObject){
print("ボタンが押された")
}
var SampleButton:UIButton = UIButton()
SampleButton.frame = CGRect(x: 0, y: 0, width:100 , height:100)
SampleButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
SampleButton.backgroundColor = UIColor.pink
SampleButton.setTitle("スタート", for: UIControlState.normal)
SampleButton.addTarget(self, action: #selector(gamePlay), for: .touchUpInside)
self.view.addSubview(SampleButton)
func gamePlay(){
print("ボタンTap")
}
ボタンなどの位置、サイズなどの初期化のみ
var myButton = UIButton()
let rec = CGRect(x: 10.0, y: 10.0 + 100.0, width:100.0 , height: 200.0)
myButton.frame = rec
imgとかをviewにしてaddsubviewにするのコピーペーストするように作成してます。
let img = UIImage(named: "sample.png")
let imgView = UIImageView()
let rec = CGRect(x: 10.0, y: 10.0 + 100.0, width:100.0 , height: 200.0)
imgView.frame = rec
imgView.image = img
self.view.addSubview(imgView)
viewに対してのviewの座標を取得するための検索キーワードワード
→ 座標変換(convertRect とか convertPoint)
上と下は同じことコメントに書いてあったものを引用
let numberWords = ["one", "two", "three"]
for word in numberWords {
print("🐥\(word)")
}
//🐥one
//🐥two
//🐥three
numberWords.forEach {word in
print("🐥\(word)")
}
//🐥one
//🐥two
//🐥three
クロージャー引数も戻り値もないバージョン
var helloClosure: () -> () = {
print("おはようございます")
}
var helloClosure1 = {() -> () in
print("おはようございます1")
}
var helloClosure2 = {() -> Void in
print("おはようございます2")
}
var helloClosure3 = {() in
print("おはようございます3")
}
var helloClosure4 = {
print("おはようございます4")
}
配列と辞書
let array0:[String] = ["a", "b", "c"]
let array1:Array<String> = ["a", "b", "c"]
let Dic0:[String:String] = ["a":"青い", "b":"節"]
let Dic1:Dictionary<String,String> = ["a":"青い", "b":"節"]
セルの上に乗っているボタンの反応をよくするのに困った時に助けてくださった。サイト(テーブルビューには遅延処理が走っているのでなくす必要があるそうです。)
http://seeku.hateblo.jp/entry/2016/02/23/202011
よくわからなくなるif文省略文。慣れると1行で済むから嬉しい。
//trueをfalseに falseをtrueに
sampletextField?.isSecureTextEntry = !(sampletextField?.isSecureTextEntry)!
if文の省略
let Text = (sampletextField?.isSecureTextEntry)! ? "見せない" : "見せる"
辞書→NSData→辞書
参考: https://stackoverflow.com/questions/26376469/nsdictionary-to-nsdata-and-nsdata-to-nsdictionary-in-swift
//🐊辞書作成
var Dic:Dictionary<String,Int> = [:]
Dic["token"] = 1234567
print("👹\(Dic)")
//👹["token": 1234567]
//🐊辞書からNSDataに変換
let Data:NSData = NSKeyedArchiver.archivedData(withRootObject: Dic) as NSData
print("👹\(Data)")
//👹<62706c69 73743030 d40……
//🐊NSDataから辞書に変換
let dictionary:NSDictionary? = NSKeyedUnarchiver.unarchiveObject(with: Data as Data)! as? NSDictionary
//作成した辞書から値を取り出し出力
let Token = dictionary?["token"]
print("👹\(String(describing: Token))")
//👹Optional(1234567)
クロージャー実行
sample {
print("sasasa")
}
//これを実行したところででるログ以下
//a
//sasasa
//b
func sample(Do:(()->())){
print("a")
Do()
print("b")
}
didSetその変数に何かを当てはめた時に実行される処理
var text:String = "こんにちは"{
didSet{
text = text + "⭐︎"
}
}
print(text)
text = "こんにちは"
print(text)
//この場合は以下の出力となる
// こんにちは
//こんにちは⭐︎
textfieldcearbutton変更
参考:https://qiita.com/noppefoxwolf/items/ccbe9bb93edb874064b0
let image0:UIImage = UIImage(named:"1.png")!
textFieldsample.rightButton?.setImage(image0, for: .normal)
extension UITextField{
var rightButton: UIButton? {
return value(forKey: "_clearButton") as? UIButton
}
}
不必要なアニメーションの処理を無くしたい。例えばcell単体の更新noneにしているのにアニメーションしちゃう時
UIView.performWithoutAnimation {
//中にアニメーション無くしたい処理の記述を行う
}
すごくわかりやすいのに又探したりとかしてすぐアクセスできないので今までなんて素敵なwebサイトの一覧をここに記録しときます!
iPhoneアプリ開発虎の巻さんのweb
http://iphone-tora.sakura.ne.jp/index.html
Swift Docsさんのweb
https://sites.google.com/a/gclue.jp/swift-docs/home
swiftサラーマンさんのweb
http://swift-salaryman.com/uibutton.php
コードというより用語などすごくわかりやすく説明してくださっているサイトです。
http://wa3.i-3-i.info/word14133.html
FaceBookログインについて(swif3)すごくわかりやすかったです。
http://blog.personal-factory.com/2017/01/17/facebook-login-with-swift3/