Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UICollectionViewでインタラクティブなCellの並び替え

 UICollectionViewでインタラクティブなCellの並び替え

UICollectionViewの並び替えのAPIが、実はiOS 9からあったことに最近気づいた話です。
第31回の #potatotips で発表した内容です。

himaratsu

July 21, 2016
Tweet

More Decks by himaratsu

Other Decks in Technology

Transcript

  1. 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() ࣮૷͢Δ
  2. 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. 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 } }
  4. 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 } }
  5. 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
  6. 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
  7. 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
  8. ViewController.swift 3. GestureRecognizerͷΠϕϯτʹ͋Θͤͯ reorderΛݺͿ - 3 func didCancelLongPress(position: CGPoint) {

    collectionView.endInteractiveMovement() } func didEndLongPress(position: CGPoint) { collectionView.endInteractiveMovement() }
  9. 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