Share
Embed
UICollectionViewでインタラクティブなCellの並び替え
UICollectionViewの並び替えのAPIが、実はiOS 9からあったことに最近気づいた話です。
第31回の #potatotips で発表した内容です。
himaratsu
July 21, 2016
Transcript
27 slides
-
UICollectionView.h // Support for reordering @available(iOS 9.0, *) public func
beginInteractiveMovementForItem(at indexPath: IndexPath) -> Bool // returns NO if reordering was prevented from beginning - otherwise YES @available(iOS 9.0, *) public func updateInteractiveMovementTargetPosition(_ targetPosition: CGPoint) @available(iOS 9.0, *) public func endInteractiveMovement() @available(iOS 9.0, *) public func cancelInteractiveMovement() ࣮͢Δ -
1. UIGestureRecognizer Λઃఆ ࠓճԡ͠Ͱฒͼସ͑։࢝͢ΔͨΊ UILongPressGesture Λͬͨ let longPressGesture = UILongPressGestureRecognizer(target:
self, action: #selector(longPressed)) addGestureRecognizer(longPressGesture) PhotoCell.swift -
2. ҠಈͰ͖ΔCellΛࢦఆ @objc(collectionView:canMoveItemAtIndexPath:) func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath)
-> Bool { return true } ViewController.swift -
3. GestureRecognizerͷΠϕϯτʹ͋Θͤͯ reorderΛݺͿ - 1 PhotoCell.swift func longPressed(gestureRecognizer: UILongPressGestureRecognizer) {
let position = gestureRecognizer.location(in: self) let convertedPosition = convert(position, to: collectionView) switch gestureRecognizer.state { case .began: delegate?.didStartLongPress(position: convertedPosition) case .changed: delegate?.didChangeLongPress(position: convertedPosition) case .ended: delegate?.didEndLongPress(position: convertedPosition) case .cancelled, .failed: delegate?.didCancelLongPress(position: convertedPosition) case .possible: break // do nothing } } -
3. GestureRecognizerͷΠϕϯτʹ͋Θͤͯ reorderΛݺͿ - 1 PhotoCell.swift func longPressed(gestureRecognizer: UILongPressGestureRecognizer) {
let position = gestureRecognizer.location(in: self) let convertedPosition = convert(position, to: collectionView) switch gestureRecognizer.state { case .began: delegate?.didStartLongPress(position: convertedPosition) case .changed: delegate?.didChangeLongPress(position: convertedPosition) case .ended: delegate?.didEndLongPress(position: convertedPosition) case .cancelled, .failed: delegate?.didCancelLongPress(position: convertedPosition) case .possible: break // do nothing } } -
3. GestureRecognizerͷΠϕϯτʹ͋Θͤͯ reorderΛݺͿ - 1 PhotoCell.swift func longPressed(gestureRecognizer: UILongPressGestureRecognizer) {
let position = gestureRecognizer.location(in: self) let convertedPosition = convert(position, to: collectionView) switch gestureRecognizer.state { case .began: delegate?.didStartLongPress(position: convertedPosition) case .changed: delegate?.didChangeLongPress(position: convertedPosition) case .ended: delegate?.didEndLongPress(position: convertedPosition) case .cancelled, .failed: delegate?.didCancelLongPress(position: convertedPosition) case .possible: break // do nothing } } -
3. GestureRecognizerͷΠϕϯτʹ͋Θͤͯ reorderΛݺͿ - 2 func didStartLongPress(position: CGPoint) { if
let indexPath = collectionView.indexPathForItem(at: position) { let success = collectionView.beginInteractiveMovementForItem(at: indexPath) // true } } func didChangeLongPress(position: CGPoint) { collectionView.updateInteractiveMovementTargetPosition(position) } ViewController.swift -
3. GestureRecognizerͷΠϕϯτʹ͋Θͤͯ reorderΛݺͿ - 2 func didStartLongPress(position: CGPoint) { if
let indexPath = collectionView.indexPathForItem(at: position) { let success = collectionView.beginInteractiveMovementForItem(at: indexPath) // true } } func didChangeLongPress(position: CGPoint) { collectionView.updateInteractiveMovementTargetPosition(position) } ViewController.swift -
3. GestureRecognizerͷΠϕϯτʹ͋Θͤͯ reorderΛݺͿ - 2 func didStartLongPress(position: CGPoint) { if
let indexPath = collectionView.indexPathForItem(at: position) { let success = collectionView.beginInteractiveMovementForItem(at: indexPath) // true } } func didChangeLongPress(position: CGPoint) { collectionView.updateInteractiveMovementTargetPosition(position) } ViewController.swift -
ViewController.swift 3. GestureRecognizerͷΠϕϯτʹ͋Θͤͯ reorderΛݺͿ - 3 func didCancelLongPress(position: CGPoint) {
collectionView.endInteractiveMovement() } func didEndLongPress(position: CGPoint) { collectionView.endInteractiveMovement() } -
4. σʔλೖΕସ͑ͷॲཧ func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to
destinationIndexPath: IndexPath) { let targetPhoto = photos[sourceIndexPath.row] photos.remove(at: sourceIndexPath.row) photos.insert(targetPhoto, at: destinationIndexPath.row) } ViewController.swift