Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated to swift 5 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DragNDrop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -296,6 +297,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -331,6 +333,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand Down
303 changes: 152 additions & 151 deletions DragNDrop/DragNDropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,167 +9,168 @@
import UIKit

class DragNDropViewController: UITableViewController {

var itemsArray : [String]

required init(coder aDecoder: NSCoder) {
itemsArray = [String]()

var itemsArray : [String]
let item1 = "Bananas"
let item2 = "Oranges"
let item3 = "Kale"
let item4 = "Milk"
let item5 = "Yogurt"
let item6 = "Crackers"
let item7 = "Cheese"
let item8 = "Carrots"
let item9 = "Ice Cream"
let item10 = "Olive Oil"

required init(coder aDecoder: NSCoder) {
itemsArray = [String]()

let item1 = "Bananas"
let item2 = "Oranges"
let item3 = "Kale"
let item4 = "Milk"
let item5 = "Yogurt"
let item6 = "Crackers"
let item7 = "Cheese"
let item8 = "Carrots"
let item9 = "Ice Cream"
let item10 = "Olive Oil"

itemsArray.append(item1)
itemsArray.append(item2)
itemsArray.append(item3)
itemsArray.append(item4)
itemsArray.append(item5)
itemsArray.append(item6)
itemsArray.append(item7)
itemsArray.append(item8)
itemsArray.append(item9)
itemsArray.append(item10)

super.init(coder: aDecoder)!
}
itemsArray.append(item1)
itemsArray.append(item2)
itemsArray.append(item3)
itemsArray.append(item4)
itemsArray.append(item5)
itemsArray.append(item6)
itemsArray.append(item7)
itemsArray.append(item8)
itemsArray.append(item9)
itemsArray.append(item10)

override func viewDidLoad() {
super.viewDidLoad()

tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)

let longpress = UILongPressGestureRecognizer(target: self, action: "longPressGestureRecognized:")
tableView.addGestureRecognizer(longpress)
}
super.init(coder: aDecoder)!
}

override func viewDidLoad() {
super.viewDidLoad()

tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)

let longpress = UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureRecognized(gestureRecognizer:)))
tableView.addGestureRecognizer(longpress)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@objc func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {
let longPress = gestureRecognizer as! UILongPressGestureRecognizer
let state = longPress.state
let locationInView = longPress.location(in: tableView)
let indexPath = tableView.indexPathForRow(at: locationInView)

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
struct My {
static var cellSnapshot : UIView? = nil
static var cellIsAnimating : Bool = false
static var cellNeedToShow : Bool = false
}
struct Path {
static var initialIndexPath : IndexPath? = nil
}

func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {
let longPress = gestureRecognizer as! UILongPressGestureRecognizer
let state = longPress.state
let locationInView = longPress.locationInView(tableView)
let indexPath = tableView.indexPathForRowAtPoint(locationInView)

struct My {
static var cellSnapshot : UIView? = nil
static var cellIsAnimating : Bool = false
static var cellNeedToShow : Bool = false
}
struct Path {
static var initialIndexPath : NSIndexPath? = nil
}

switch state {
case UIGestureRecognizerState.Began:
if indexPath != nil {
Path.initialIndexPath = indexPath
let cell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
My.cellSnapshot = snapshotOfCell(cell)

var center = cell.center
My.cellSnapshot!.center = center
My.cellSnapshot!.alpha = 0.0
tableView.addSubview(My.cellSnapshot!)

UIView.animateWithDuration(0.25, animations: { () -> Void in
center.y = locationInView.y
My.cellIsAnimating = true
My.cellSnapshot!.center = center
My.cellSnapshot!.transform = CGAffineTransformMakeScale(1.05, 1.05)
My.cellSnapshot!.alpha = 0.98
cell.alpha = 0.0
}, completion: { (finished) -> Void in
if finished {
My.cellIsAnimating = false
if My.cellNeedToShow {
My.cellNeedToShow = false
UIView.animateWithDuration(0.25, animations: { () -> Void in
cell.alpha = 1
})
} else {
cell.hidden = true
}
}
switch state {
case UIGestureRecognizerState.began:
if indexPath != nil {
Path.initialIndexPath = indexPath!
let cell = tableView.cellForRow(at: indexPath!)!
My.cellSnapshot = snapshotOfCell(inputView: cell)

var center = cell.center
My.cellSnapshot!.center = center
My.cellSnapshot!.alpha = 0.0
tableView.addSubview(My.cellSnapshot!)

UIView.animate(withDuration: 0.25, animations: { () -> Void in
center.y = locationInView.y
My.cellIsAnimating = true
My.cellSnapshot!.center = center
My.cellSnapshot!.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
My.cellSnapshot!.alpha = 0.98
cell.alpha = 0.0
}, completion: { (finished) -> Void in
if finished {
My.cellIsAnimating = false
if My.cellNeedToShow {
My.cellNeedToShow = false
UIView.animate(withDuration: 0.25, animations: { () -> Void in
cell.alpha = 1
})
} else {
cell.isHidden = true
}
}
})
}

case UIGestureRecognizerState.changed:
if My.cellSnapshot != nil {
var center = My.cellSnapshot!.center
center.y = locationInView.y
My.cellSnapshot!.center = center

if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
itemsArray.insert(itemsArray.remove(at: Path.initialIndexPath!.row), at: indexPath!.row)
tableView.moveRow(at: Path.initialIndexPath!, to: indexPath!)
Path.initialIndexPath = indexPath
}
}
default:
if Path.initialIndexPath != nil {
let cell = tableView.cellForRow(at: Path.initialIndexPath!)!
if My.cellIsAnimating {
My.cellNeedToShow = true
} else {
cell.isHidden = false
cell.alpha = 0.0
}

UIView.animate(withDuration: 0.25, animations: { () -> Void in
My.cellSnapshot!.center = cell.center
My.cellSnapshot!.transform = CGAffineTransform.identity
My.cellSnapshot!.alpha = 0.0
cell.alpha = 1.0

case UIGestureRecognizerState.Changed:
if My.cellSnapshot != nil {
var center = My.cellSnapshot!.center
center.y = locationInView.y
My.cellSnapshot!.center = center

if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
itemsArray.insert(itemsArray.removeAtIndex(Path.initialIndexPath!.row), atIndex: indexPath!.row)
tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)
Path.initialIndexPath = indexPath
}
}
default:
if Path.initialIndexPath != nil {
let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell!
if My.cellIsAnimating {
My.cellNeedToShow = true
} else {
cell.hidden = false
cell.alpha = 0.0
}

UIView.animateWithDuration(0.25, animations: { () -> Void in
My.cellSnapshot!.center = cell.center
My.cellSnapshot!.transform = CGAffineTransformIdentity
My.cellSnapshot!.alpha = 0.0
cell.alpha = 1.0

}, completion: { (finished) -> Void in
if finished {
Path.initialIndexPath = nil
My.cellSnapshot!.removeFromSuperview()
My.cellSnapshot = nil
}
})
}, completion: { (finished) -> Void in
if finished {
Path.initialIndexPath = nil
My.cellSnapshot!.removeFromSuperview()
My.cellSnapshot = nil
}
}
})
}
}
}

func snapshotOfCell(inputView: UIView) -> UIView {
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
inputView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

func snapshotOfCell(inputView: UIView) -> UIView {
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
inputView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()

let cellSnapshot : UIView = UIImageView(image: image)
cellSnapshot.layer.masksToBounds = false
cellSnapshot.layer.cornerRadius = 0.0
cellSnapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0)
cellSnapshot.layer.shadowRadius = 5.0
cellSnapshot.layer.shadowOpacity = 0.4
return cellSnapshot
}

// MARK: - Table view data source

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemsArray.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = itemsArray[indexPath.row]
return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
let cellSnapshot : UIView = UIImageView(image: image)
cellSnapshot.layer.masksToBounds = false
cellSnapshot.layer.cornerRadius = 0.0
cellSnapshot.layer.shadowOffset = CGSize(width: -5.0, height: 0.0)
cellSnapshot.layer.shadowRadius = 5.0
cellSnapshot.layer.shadowOpacity = 0.4
return cellSnapshot
}

// MARK: - Table view data source

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemsArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = itemsArray[indexPath.row]
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
}

}
44 changes: 22 additions & 22 deletions DragNDropTests/DragNDropTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ import UIKit
import XCTest

class DragNDropTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testExample() {
// This is an example of a functional test case.
XCTAssert(true, "Pass")
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measure() {
// Put the code you want to measure the time of here.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testExample() {
// This is an example of a functional test case.
XCTAssert(true, "Pass")
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
// Put the code you want to measure the time of here.
}
}

}

}