diff --git a/JRLongPressGestureHandler.xcodeproj/project.pbxproj b/JRLongPressGestureHandler.xcodeproj/project.pbxproj index a013f71..c5f97f6 100644 --- a/JRLongPressGestureHandler.xcodeproj/project.pbxproj +++ b/JRLongPressGestureHandler.xcodeproj/project.pbxproj @@ -101,11 +101,12 @@ 3F917D981CE3FFA4009D8EFE /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0730; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Jeremy Rea"; TargetAttributes = { 3F917DA01CE3FFA4009D8EFE = { CreatedOnToolsVersion = 7.3.1; + LastSwiftMigration = 0800; }; }; }; @@ -162,8 +163,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -211,8 +214,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -232,6 +237,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; @@ -243,6 +249,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_MODULES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -254,6 +261,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -261,6 +269,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_MODULES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -271,6 +280,7 @@ PRODUCT_BUNDLE_IDENTIFIER = ca.jeremyrea.JRLongPressGestureHandler; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -293,6 +303,7 @@ 3F917DAB1CE3FFA4009D8EFE /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/JRLongPressGestureHandler.xcodeproj/xcshareddata/xcschemes/JRLongPressGestureHandler.xcscheme b/JRLongPressGestureHandler.xcodeproj/xcshareddata/xcschemes/JRLongPressGestureHandler.xcscheme index 93d5004..c7373d1 100644 --- a/JRLongPressGestureHandler.xcodeproj/xcshareddata/xcschemes/JRLongPressGestureHandler.xcscheme +++ b/JRLongPressGestureHandler.xcodeproj/xcshareddata/xcschemes/JRLongPressGestureHandler.xcscheme @@ -1,6 +1,6 @@ SchemeUserState - JRLongPressGestureHandler.xcscheme + JRLongPressGestureHandler.xcscheme_^#shared#^_ orderHint 0 diff --git a/JRLongPressGestureHandler/Source/JRLongPressGestureHandler.swift b/JRLongPressGestureHandler/Source/JRLongPressGestureHandler.swift index 951f056..2102cf9 100644 --- a/JRLongPressGestureHandler/Source/JRLongPressGestureHandler.swift +++ b/JRLongPressGestureHandler/Source/JRLongPressGestureHandler.swift @@ -1,84 +1,62 @@ import UIKit public protocol JRLongPressGestureHandlerDelegate: class { - func didEndLongPress(startIndexPath: NSIndexPath, endIndexPath: NSIndexPath) + func didEndLongPress(_ startIndexPath: IndexPath, endIndexPath: IndexPath) } -public class JRLongPressGestureHandler { +open class JRLongPressGestureHandler { enum CellAlpha: CGFloat { - case Hidden = 0.0 - case Visible = 1.0 + case hidden = 0.0 + case visible = 1.0 } - private weak var delegate: JRLongPressGestureHandlerDelegate? - private var snapshot: UIView? - private var sourceIndexPath: NSIndexPath? - private var previousIndexPath: NSIndexPath? - private var backupIndexPath: NSIndexPath? - private var startIndexPath: NSIndexPath! + fileprivate weak var delegate: JRLongPressGestureHandlerDelegate? + fileprivate var snapshot: UIView? + fileprivate var sourceIndexPath: IndexPath? + fileprivate var previousIndexPath: IndexPath? + fileprivate var backupIndexPath: IndexPath? + fileprivate var startIndexPath: IndexPath! - public var transformOnCellSelection: CGAffineTransform! - public var transformOnCellDeposit: CGAffineTransform! - public var durationForSelectionAnimation: NSTimeInterval! - public var durationForDepositAnimation: NSTimeInterval! - public var alphaForCell: CGFloat! + open var transformOnCellSelection: CGAffineTransform! + open var transformOnCellDeposit: CGAffineTransform! + open var durationForSelectionAnimation: TimeInterval! + open var durationForDepositAnimation: TimeInterval! + open var alphaForCell: CGFloat! public init(delegate: JRLongPressGestureHandlerDelegate) { self.delegate = delegate - transformOnCellSelection = CGAffineTransformMakeScale(1.05, 1.05) - transformOnCellDeposit = CGAffineTransformIdentity + transformOnCellSelection = CGAffineTransform(scaleX: 1.05, y: 1.05) + transformOnCellDeposit = CGAffineTransform.identity durationForSelectionAnimation = 0.25 durationForDepositAnimation = 0.25 alphaForCell = 0.98 } - public func longPressGestureRecognized(tableView: UITableView, gesture: UILongPressGestureRecognizer) { + open func longPressGestureRecognized(_ tableView: UITableView, gesture: UILongPressGestureRecognizer) { let gestureState: UIGestureRecognizerState = gesture.state - let location: CGPoint = gesture.locationInView(tableView) - var indexPath: NSIndexPath? = tableView.indexPathForRowAtPoint(location) + let location: CGPoint = gesture.location(in: tableView) + var indexPath: IndexPath? = tableView.indexPathForRow(at: location) if cellIsOutOfBounds(indexPath) { indexPath = self.backupIndexPath - } else if cellWasOutOfBounds(tableView.numberOfRowsInSection(0), indexPath: indexPath!) { + } else if cellWasOutOfBounds(tableView.numberOfRows(inSection: 0), indexPath: indexPath!) { self.backupIndexPath = indexPath } switch (gestureState) { - case UIGestureRecognizerState.Began: + case UIGestureRecognizerState.began: sourceIndexPath = indexPath startIndexPath = sourceIndexPath pickupCellAnimation(tableView, indexPath: indexPath!, location: location) - case UIGestureRecognizerState.Changed: + case UIGestureRecognizerState.changed: var center: CGPoint = snapshot!.center center.y = location.y snapshot?.center = center if cellMoved(sourceIndexPath!, indexPath: indexPath!) { - - print("cell center: ", center.y) - print(tableView.frame.height + tableView.contentOffset.y - 88) - print("--------") - - - // Scrolling - if(center.y > tableView.frame.height + tableView.contentOffset.y - 65 && indexPath!.row + 1 < tableView.numberOfRowsInSection((indexPath?.section)!)) { - indexPath = NSIndexPath(forRow: indexPath!.row + 1, inSection: indexPath!.section) - - UIView.animateWithDuration(0.5, animations: { () -> Void in - tableView.scrollToRowAtIndexPath(indexPath!, atScrollPosition: .Bottom, animated: false) - }) - } else if (center.y - tableView.contentOffset.y < 64 + 65 && indexPath!.row - 1 >= 0) { - // 64 is height of navigation bar + status bar -// indexPath = NSIndexPath(forRow: indexPath!.row - 1, inSection: indexPath!.section) - - UIView.animateWithDuration(0.5, animations: { () -> Void in - tableView.scrollToRowAtIndexPath(indexPath!, atScrollPosition: .Top, animated: false) - }) - } - displaceCellAnimation(tableView, indexPath: indexPath!) } @@ -92,48 +70,48 @@ public class JRLongPressGestureHandler { } } - private func cellIsOutOfBounds(indexPath: NSIndexPath?) -> Bool { + fileprivate func cellIsOutOfBounds(_ indexPath: IndexPath?) -> Bool { return indexPath == nil && backupIndexPath != nil } - private func cellWasOutOfBounds(tableSize: Int, indexPath: NSIndexPath) -> Bool { - return (indexPath.row == 0 || indexPath.row == tableSize - 1) + fileprivate func cellWasOutOfBounds(_ tableSize: Int, indexPath: IndexPath) -> Bool { + return ((indexPath as NSIndexPath).row == 0 || (indexPath as NSIndexPath).row == tableSize - 1) } - private func cellMoved(sourceIndexPath: NSIndexPath, indexPath: NSIndexPath) -> Bool { + fileprivate func cellMoved(_ sourceIndexPath: IndexPath, indexPath: IndexPath) -> Bool { return indexPath != sourceIndexPath } - private func hideCell(tableView: UITableView, indexPath: NSIndexPath) { - let cell = tableView.cellForRowAtIndexPath(indexPath)! - cell.alpha = CellAlpha.Hidden.rawValue + fileprivate func hideCell(_ tableView: UITableView, indexPath: IndexPath) { + let cell = tableView.cellForRow(at: indexPath)! + cell.alpha = CellAlpha.hidden.rawValue } - private func pickupCellAnimation(tableView: UITableView, indexPath: NSIndexPath, location: CGPoint) { - let cell = tableView.cellForRowAtIndexPath(indexPath)! + fileprivate func pickupCellAnimation(_ tableView: UITableView, indexPath: IndexPath, location: CGPoint) { + let cell = tableView.cellForRow(at: indexPath)! var centerPoint = cell.center snapshot = customSnapshotFromView(cell, snapshotCenter: centerPoint) tableView.addSubview(snapshot!) - UIView.animateWithDuration(durationForSelectionAnimation, animations: { () -> Void in + UIView.animate(withDuration: durationForSelectionAnimation, animations: { () -> Void in centerPoint.y = location.y - cell.alpha = CellAlpha.Hidden.rawValue + cell.alpha = CellAlpha.hidden.rawValue self.configureLocalSnapshot(centerPoint, transform: self.transformOnCellSelection, alpha: self.alphaForCell) }) } - private func displaceCellAnimation(tableView: UITableView, indexPath: NSIndexPath) { - tableView.moveRowAtIndexPath(sourceIndexPath!, toIndexPath: indexPath) + fileprivate func displaceCellAnimation(_ tableView: UITableView, indexPath: IndexPath) { + tableView.moveRow(at: sourceIndexPath!, to: indexPath) sourceIndexPath = indexPath hideCell(tableView, indexPath: indexPath) } - private func depositCellAnimation(tableView: UITableView, indexPath: NSIndexPath) { - let cell = tableView.cellForRowAtIndexPath(indexPath)! - cell.alpha = CellAlpha.Hidden.rawValue - UIView.animateWithDuration(durationForDepositAnimation, animations: { () -> Void in + fileprivate func depositCellAnimation(_ tableView: UITableView, indexPath: IndexPath) { + let cell = tableView.cellForRow(at: indexPath)! + cell.alpha = CellAlpha.hidden.rawValue + UIView.animate(withDuration: durationForDepositAnimation, animations: { () -> Void in self.configureLocalSnapshot(cell.center, transform: self.transformOnCellDeposit, alpha: 0.0) - cell.alpha = CellAlpha.Visible.rawValue + cell.alpha = CellAlpha.visible.rawValue }, completion: { (finished) in self.sourceIndexPath = nil @@ -142,26 +120,26 @@ public class JRLongPressGestureHandler { }) } - private func customSnapshotFromView(inputView: UIView, snapshotCenter: CGPoint) -> UIView { + fileprivate func customSnapshotFromView(_ inputView: UIView, snapshotCenter: CGPoint) -> UIView { let image = takeCellSnapshot(inputView) let imageView = createViewFromImage(image) - imageView.alpha = CellAlpha.Hidden.rawValue + imageView.alpha = CellAlpha.hidden.rawValue imageView.center = snapshotCenter return imageView } - private func takeCellSnapshot(inputView: UIView) -> UIImage { + fileprivate func takeCellSnapshot(_ inputView: UIView) -> UIImage { UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0) - inputView.layer.renderInContext(UIGraphicsGetCurrentContext()!) + inputView.layer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() - return image + return image! } - private func createViewFromImage(image: UIImage) -> UIImageView { + fileprivate func createViewFromImage(_ image: UIImage) -> UIImageView { let imageView = UIImageView(image: image) imageView.layer.masksToBounds = false imageView.layer.cornerRadius = 0.0 @@ -172,7 +150,7 @@ public class JRLongPressGestureHandler { return imageView } - private func configureLocalSnapshot(center: CGPoint, transform: CGAffineTransform, alpha: CGFloat) { + fileprivate func configureLocalSnapshot(_ center: CGPoint, transform: CGAffineTransform, alpha: CGFloat) { self.snapshot?.center = center self.snapshot?.transform = transform self.snapshot?.alpha = alpha