|
| 1 | +# SDECollectionViewAlbumTransition |
| 2 | +UICollectionViewController Transition like open and close an album. |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Installtion |
| 8 | + |
| 9 | +Drag files in "Classes" folder into your project. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +- In your storyboard, drag a object to your navigation controller, and set its custom class to "SDENavigationControllerDelegate" |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +- Set this object to be your navigation controller's delegate. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Or, you want to set it in the code. |
| 22 | +- At the last, add a line code in your UICollectionView's delegate: |
| 23 | + |
| 24 | + override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { |
| 25 | + self.selectedIndexPath = indexPath |
| 26 | + ... |
| 27 | + } |
| 28 | + |
| 29 | +**Now your project supports to pinch to pop.** |
| 30 | + |
| 31 | + |
| 32 | +## Requirements |
| 33 | + |
| 34 | +- iOS 7.0+ |
| 35 | + |
| 36 | +Thanks for [@CezaryKopacz](https://github.com/CezaryKopacz/CKWaveCollectionViewTransition), [@ColinEberhardt](https://github.com/ColinEberhardt/VCTransitionsLibrary). I learn a lot from their repo. |
| 37 | +And thanks for [ Vincent Ngo](http://www.raywenderlich.com/94565/how-to-create-an-ios-book-open-animation-part-1) very much, I find the key to resolve the problem of pinching to push. |
| 38 | +There is a little problem with time delta algorithm, I will update if I find the way to improvement. |
| 39 | + |
| 40 | +## Pinch to push? |
| 41 | + |
| 42 | +Pinching to push is a little complex. |
| 43 | + |
| 44 | +If you want to support pinch to push, switch to the branch "Pinch-Push-Pop-Transition", drag files in "Classes" folder in this branch into your project, |
| 45 | +there is a little difference between "Pinch-Push-Pop-Transition" branch with other branches. |
| 46 | + |
| 47 | +Add the below properties to your UICollectionViewController child class: |
| 48 | + |
| 49 | + var transitionDelegate: SDENavigationControllerDelegate? |
| 50 | + var pinchGestureRecognizer: UIPinchGestureRecognizer?{ |
| 51 | + didSet(newValue){ |
| 52 | + collectionView?.addGestureRecognizer(pinchGestureRecognizer!) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + override viewDidLoad(){ |
| 57 | + ... |
| 58 | + pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: "handlePinch:") |
| 59 | + } |
| 60 | + |
| 61 | + deinit{ |
| 62 | + if pinchGestureRecognizer != nil{ |
| 63 | + collectionView?.removeGestureRecognizer(pinchGestureRecognizer!) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + //MARK: Pinch Push and Pop |
| 68 | + func getIndexPathForGesture(gesture: UIPinchGestureRecognizer) -> NSIndexPath?{ |
| 69 | + let location0 = gesture.locationOfTouch(0, inView: gesture.view) |
| 70 | + let location1 = gesture.locationOfTouch(1, inView: gesture.view) |
| 71 | + let middleLocation = CGPointMake((location0.x + location1.x)/2, (location0.y + location1.y)/2) |
| 72 | + let indexPath = collectionView?.indexPathForItemAtPoint(middleLocation) |
| 73 | + return indexPath |
| 74 | + } |
| 75 | + |
| 76 | + func handlePinch(gesture: UIPinchGestureRecognizer){ |
| 77 | + switch gesture.state{ |
| 78 | + case .Began: |
| 79 | + if gesture.scale >= 1.0{ |
| 80 | + guard let indexPath = getIndexPathForGesture(gesture) else{ |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + self.selectedIndexPath = indexPath |
| 85 | + let layoutAttributes = collectionView!.layoutAttributesForItemAtIndexPath(indexPath) |
| 86 | + let areaRect = collectionView!.convertRect(layoutAttributes!.frame, toView: collectionView?.superview) |
| 87 | + |
| 88 | + if let toVC = ...{ |
| 89 | + toVC.coverRectInSuperview = areaRect |
| 90 | + |
| 91 | + transitionDelegate = navigationController?.delegate as? SDENavigationControllerDelegate |
| 92 | + transitionDelegate?.interactive = true |
| 93 | + navigationController?.pushViewController(toVC, animated: true) |
| 94 | + } |
| 95 | + |
| 96 | + }else{ |
| 97 | + //after view controller is poped, UIViewController.navigationController is nil. So you need to keep it somewhere before pop |
| 98 | + transitionDelegate = self.navigationController?.delegate as? SDENavigationControllerDelegate |
| 99 | + transitionDelegate?.interactive = true |
| 100 | + self.navigationController?.popViewControllerAnimated(true) |
| 101 | + } |
| 102 | + |
| 103 | + case .Changed: |
| 104 | + guard transitionDelegate != nil else{ |
| 105 | + return |
| 106 | + } |
| 107 | + guard let interactionController = transitionDelegate?.interactionController else{ |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + var progress = gesture.scale |
| 112 | + if transitionDelegate!.isPush{ |
| 113 | + progress = gesture.scale - 1.0 >= 0.9 ? 0.9 : gesture.scale - 1.0 |
| 114 | + }else{ |
| 115 | + progress = 1.0 - gesture.scale |
| 116 | + } |
| 117 | + |
| 118 | + interactionController.updateInteractiveTransition(progress) |
| 119 | + case .Ended, .Cancelled: |
| 120 | + guard transitionDelegate != nil else{ |
| 121 | + return |
| 122 | + } |
| 123 | + guard let interactionController = transitionDelegate?.interactionController else{ |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + var progress = gesture.scale |
| 128 | + if transitionDelegate!.isPush{ |
| 129 | + progress = gesture.scale - 1.0 >= 0.9 ? 0.9 : gesture.scale - 1.0 |
| 130 | + }else{ |
| 131 | + progress = 1.0 - gesture.scale |
| 132 | + } |
| 133 | + |
| 134 | + if progress >= 0.4{ |
| 135 | + interactionController.finishInteractiveTransition() |
| 136 | + }else{ |
| 137 | + interactionController.cancelInteractiveTransition() |
| 138 | + } |
| 139 | + transitionDelegate?.interactive = false |
| 140 | + default: |
| 141 | + guard transitionDelegate != nil else{ |
| 142 | + return |
| 143 | + } |
| 144 | + transitionDelegate?.interactive = false |
| 145 | + } |
| 146 | + } |
0 commit comments