@@ -11,9 +11,9 @@ import UIKit
11
11
class ThirdViewController : UIViewController {
12
12
13
13
@IBOutlet weak var collectionView : UICollectionView !
14
- private var numbers : [ Int ] = [ ]
14
+ fileprivate var numbers : [ Int ] = [ ]
15
15
16
- private var longPressGesture : UILongPressGestureRecognizer !
16
+ fileprivate var longPressGesture : UILongPressGestureRecognizer !
17
17
18
18
override func viewDidLoad( ) {
19
19
super. viewDidLoad ( )
@@ -23,22 +23,22 @@ class ThirdViewController: UIViewController {
23
23
numbers. append ( height)
24
24
}
25
25
26
- longPressGesture = UILongPressGestureRecognizer ( target: self , action: " handleLongGesture: " )
26
+ longPressGesture = UILongPressGestureRecognizer ( target: self , action: #selector ( ThirdViewController . handleLongGesture ( _ : ) ) )
27
27
self . collectionView. addGestureRecognizer ( longPressGesture)
28
28
}
29
29
30
- func handleLongGesture( gesture: UILongPressGestureRecognizer ) {
30
+ func handleLongGesture( _ gesture: UILongPressGestureRecognizer ) {
31
31
32
32
switch ( gesture. state) {
33
33
34
- case UIGestureRecognizerState . Began :
35
- guard let selectedIndexPath = self . collectionView. indexPathForItemAtPoint ( gesture. locationInView ( self . collectionView) ) else {
34
+ case UIGestureRecognizerState . began :
35
+ guard let selectedIndexPath = self . collectionView. indexPathForItem ( at : gesture. location ( in : self . collectionView) ) else {
36
36
break
37
37
}
38
- collectionView. beginInteractiveMovementForItemAtIndexPath ( selectedIndexPath)
39
- case UIGestureRecognizerState . Changed :
40
- collectionView. updateInteractiveMovementTargetPosition ( gesture. locationInView ( gesture. view!) )
41
- case UIGestureRecognizerState . Ended :
38
+ collectionView. beginInteractiveMovementForItem ( at : selectedIndexPath)
39
+ case UIGestureRecognizerState . changed :
40
+ collectionView. updateInteractiveMovementTargetPosition ( gesture. location ( in : gesture. view!) )
41
+ case UIGestureRecognizerState . ended :
42
42
collectionView. endInteractiveMovement ( )
43
43
default :
44
44
collectionView. cancelInteractiveMovement ( )
@@ -49,42 +49,42 @@ class ThirdViewController: UIViewController {
49
49
50
50
extension ThirdViewController : CHTCollectionViewDelegateWaterfallLayout {
51
51
52
- func collectionView ( collectionView: UICollectionView , layout collectionViewLayout: UICollectionViewLayout ,
53
- sizeForItemAtIndexPath indexPath: NSIndexPath ) -> CGSize {
52
+ func collectionView ( _ collectionView: UICollectionView , layout collectionViewLayout: UICollectionViewLayout ,
53
+ sizeForItemAtIndexPath indexPath: IndexPath ) -> CGSize {
54
54
return CGSize ( width: Int ( ( view. bounds. width - 40 ) / 3 ) , height: numbers [ indexPath. item] )
55
55
}
56
56
}
57
57
58
58
extension ThirdViewController : UICollectionViewDataSource {
59
59
60
- func collectionView( collectionView: UICollectionView , numberOfItemsInSection section: Int ) -> Int {
60
+ func collectionView( _ collectionView: UICollectionView , numberOfItemsInSection section: Int ) -> Int {
61
61
return numbers. count
62
62
}
63
63
64
- func collectionView( collectionView: UICollectionView , cellForItemAtIndexPath indexPath: NSIndexPath ) -> UICollectionViewCell {
64
+ func collectionView( _ collectionView: UICollectionView , cellForItemAt indexPath: IndexPath ) -> UICollectionViewCell {
65
65
66
- let cell = collectionView. dequeueReusableCellWithReuseIdentifier ( " Cell " , forIndexPath : indexPath) as! TextCollectionViewCell
66
+ let cell = collectionView. dequeueReusableCell ( withReuseIdentifier : " Cell " , for : indexPath) as! TextCollectionViewCell
67
67
cell. textLabel. text = " \( numbers [ indexPath. item] ) "
68
68
69
69
return cell
70
70
}
71
71
72
- func collectionView( collectionView: UICollectionView , moveItemAtIndexPath sourceIndexPath: NSIndexPath , toIndexPath destinationIndexPath: NSIndexPath ) {
72
+ func collectionView( _ collectionView: UICollectionView , moveItemAt sourceIndexPath: IndexPath , to destinationIndexPath: IndexPath ) {
73
73
74
- let temp = numbers. removeAtIndex ( sourceIndexPath. item)
75
- numbers. insert ( temp, atIndex : destinationIndexPath. item)
74
+ let temp = numbers. remove ( at : sourceIndexPath. item)
75
+ numbers. insert ( temp, at : destinationIndexPath. item)
76
76
}
77
77
78
78
}
79
79
80
80
//MARK: one little trick
81
81
extension CHTCollectionViewWaterfallLayout {
82
82
83
- internal override func invalidationContextForInteractivelyMovingItems ( targetIndexPaths: [ NSIndexPath ] , withTargetPosition targetPosition: CGPoint , previousIndexPaths: [ NSIndexPath ] , previousPosition: CGPoint ) -> UICollectionViewLayoutInvalidationContext {
83
+ internal override func invalidationContext ( forInteractivelyMovingItems targetIndexPaths: [ IndexPath ] , withTargetPosition targetPosition: CGPoint , previousIndexPaths: [ IndexPath ] , previousPosition: CGPoint ) -> UICollectionViewLayoutInvalidationContext {
84
84
85
- let context = super. invalidationContextForInteractivelyMovingItems ( targetIndexPaths, withTargetPosition: targetPosition, previousIndexPaths: previousIndexPaths, previousPosition: previousPosition)
85
+ let context = super. invalidationContext ( forInteractivelyMovingItems : targetIndexPaths, withTargetPosition: targetPosition, previousIndexPaths: previousIndexPaths, previousPosition: previousPosition)
86
86
87
- self . delegate? . collectionView!( self . collectionView!, moveItemAtIndexPath : previousIndexPaths [ 0 ] , toIndexPath : targetIndexPaths [ 0 ] )
87
+ self . delegate? . collectionView!( self . collectionView!, moveItemAt : previousIndexPaths [ 0 ] , to : targetIndexPaths [ 0 ] )
88
88
89
89
return context
90
90
}
0 commit comments