Skip to content

Instantly share code, notes, and snippets.

@gitbricho
Last active April 20, 2016 07:24
Show Gist options
  • Save gitbricho/bf8b3db37c19e32f7c35 to your computer and use it in GitHub Desktop.
Save gitbricho/bf8b3db37c19e32f7c35 to your computer and use it in GitHub Desktop.
OSX:コレクションビュー
import Cocoa
/**
* コレクションビューに表示するアイテム.
*/
class CollectionViewItem: NSCollectionViewItem {
//MARK:プロパティ
var movieTicket: MovieTicket? {
return (representedObject as? MovieTicket)
}
}
import Cocoa
/**
* コレクションビューに表示するアイテム.
*/
class CollectionViewItem: NSCollectionViewItem {
//MARK:プロパティ
var movieTicket: MovieTicket? {
return (representedObject as? MovieTicket)
}
override var selected: Bool {
didSet {
(self.view as! ItemView).selected = selected
}
}
override var highlightState: NSCollectionViewItemHighlightState {
didSet {
(self.view as! ItemView).highlightState = highlightState
}
}
// MARK: NSResponder
//マウスのクリックイベントに応答
override func mouseDown(theEvent: NSEvent) {
if theEvent.clickCount == 2 {
print("Double click \(movieTicket!.title)")
} else {
super.mouseDown(theEvent)
print("click \(movieTicket!.title)")
}
}
}
import Cocoa
class ItemView: NSView {
}
import Cocoa
class ItemView: NSView {
// MARK:プロパティ
let selectedColor = NSColor(red: 0.7,
green: 0.84, blue: 0.94, alpha: 1.0).CGColor
let unSelectedColor = NSColor(red: 0.99,
green: 0.99, blue: 0.99, alpha: 1.0).CGColor
var selected: Bool = false {
didSet {
if selected != oldValue {
needsDisplay = true
}
}
}
var highlightState: NSCollectionViewItemHighlightState = .None {
didSet {
if highlightState != oldValue {
needsDisplay = true
}
}
}
// MARK: NSView
override var wantsUpdateLayer: Bool {
return true
}
override func updateLayer() {
if selected {
self.layer?.cornerRadius = 5
layer!.backgroundColor = selectedColor
} else {
self.layer?.cornerRadius = 0
layer!.backgroundColor = unSelectedColor
}
}
// MARK:イニシャライザ
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
wantsLayer = true
layer?.masksToBounds = true
}
required init?(coder: NSCoder) {
super.init(coder: coder)
wantsLayer = true
layer?.masksToBounds = true
}
}
import Cocoa
/**
* 映画の前売り券情報.
*/
class MovieTicket: NSObject {
var title: String
var image: NSImage?
init(title:String, image: NSImage) {
self.title = title
self.image = image
}
}
import Cocoa
/**
* コレクションビューを含むビューコントローラ.
*/
class ViewController: NSViewController {
@IBOutlet weak var collectionView: NSCollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
import Cocoa
class ViewController: NSViewController ,
NSCollectionViewDataSource {
let imageNames = [
"48時間",
"300",
"E.T",
"ア・フュー・グッドメン",
"アビス",
"ある夜の出来事",
"オーメン",
"キッド",
"キャリー",
"ゴースト・ニューヨークの幻",
"ザ・フライ",
"シェーン",
"ジュラシック・パーク",
"ジョニーは戦場へ行った"
]
@IBOutlet weak var collectionView: NSCollectionView!
override func viewDidLoad() {
super.viewDidLoad()
//このクラスをコレクションビューのデータソースに設定
collectionView.dataSource = self
}
// MARK:データソース
func collectionView(collectionView: NSCollectionView,
numberOfItemsInSection section: Int) -> Int {
return imageNames.count
}
func collectionView(collectionView: NSCollectionView,
itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath)
-> NSCollectionViewItem {
let item = collectionView.makeItemWithIdentifier(
"CollectionViewItem", forIndexPath: indexPath)
item.representedObject = MovieTicket(
title: imageNames[indexPath.item],
image: NSImage(named: imageNames[indexPath.item])!)
return item
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment