はじめに
Swiftを9月から始めました。
勉強したメモを書いていきます。
勉強で使ったもの
Swift - Photo Gallery App (StoryBoard + Actions & Outlets) pt. 1/5
http://youtu.be/ztIBNHOm35E
https://github.com/TDAbboud/PhotosGalleryApp
- メモがわかりにくかったらここを参照してください。
今回の成果物
画像ギャラリーを作るためのはじめの一歩といった感じです。
インタフェースをストリーボードに配置する
ViewControllerを配置する
Navigation Controllerを配置する
CollectionViewをPhotoCellとして定義する
CollectionViewの中にImageViewを配置し、PhotoCellとして定義する
ImageViewとfullscreenViewを関連付ける
Showを選択する
NavigationItemをViewに配置する。
UIImageViewをフルスクリーンで配置
Coding
ViewPhotoクラスを作成
ViewPortクラスをストリーボードで関連付けする
配置したボタンをアクション接続する
追加したコード
ViewPhoto.swift
import UIKit
class ViewPhoto: UIViewController {
@IBAction func btnCancel(sender: AnyObject) {
println("Cancel")
self.navigationController?.popToRootViewControllerAnimated(true)
}
@IBAction func btnTrush(sender: AnyObject) {
println("trush")
}
@IBAction func btnExport(sender: AnyObject) {
println("Export")
}
@IBOutlet weak var ImgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
- ロートコントローラに戻る。
ViewControllerのコードと関連付けを行う。
ViewPhoto.swift
import UIKit
let reuseIdentifier = "PhotoCell"
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet var collectionView: UICollectionView!
@IBAction func btnCamera(sender: AnyObject) {
}
@IBAction func btnPhotoAlbum(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// uicollectionViewDataSource method
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return 1
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
// modify the cell
cell.backgroundColor = UIColor.redColor()
return cell
}
}
今後に向けて
今回はpart1だけのみなので、今後もすすめていきます。