How to scale CGPoints and rescale back in UIView - ios

My app represent a small ReportReview I have a custom CanvasView (UIView) where the user can draw inside.
When the user will finish the draw will be able to will click the Add UIButton and that scribbles will be appended to an object. Each object represent a row in a table.
When the user click any of the rows the colour of the scribbles will be changed on CanvasView to be more visible for user.
When the user finish all the draws can click Save UIButton to serialise and save the report as Data in CoreData.
The user can review the report deserialising it back.
The problem is that the scribbles are not scaled correctly (I think) and the CGPoints on CanvasView are in different locations.
Here is a link with my small project:
https://github.com/tygruletz/SelectScribblesUsingBinaryData
When I serialise I'm using this function:
func serializeDamageItemsTTable(damageItems: [DamageItem], canvas: CanvasView) -> TTable {
var metaDamage: [TMeta] = []
metaDamage.append(TMeta(type: .s, name: "descript")) // DamageItem Name
metaDamage.append(TMeta(type: .z, name: "scribble")) // Coordinates for each scribble recorded on the image for DamageItem
let ttDamageItems = TTable(meta: metaDamage)
print("SERIALIZATION STARTED")
damageItems.forEach { damageItem in
let row = TRow(tMeta: metaDamage)
row.cell.append(TCell(s: damageItem.name)) // DamageItem Name
var scribbleCoord = [UInt8]()
damageItem.scribbles.forEach { scribble in
var firstPointInScribble = true
scribble.forEach { coordinate in
// ------ Scale all CGPoints ---------
let factor: CGFloat = 240 / canvas.bounds.width
let scaledX = UInt8(coordinate.x * factor)
let scaledY = UInt8(coordinate.y * factor)
if (coordinate.x > 0 && coordinate.x < 255) && (coordinate.y > 0 && coordinate.y < 127) {
scribbleCoord.append(scaledX) // Append X coord
if firstPointInScribble {
firstPointInScribble = false
scribbleCoord.append(scaledY | Scribble.yMarkerBitmask) // Append value 1 in front of Y coord + Y coord
} else {
scribbleCoord.append(scaledY)
}
}
}
}
row.cell.append(TCell(data: scribbleCoord)) // Coordinates for each scribble recorded on the image for DamageItem
print("Damage Item Scribble: \(scribbleCoord)")
print("SERIALIZATION ENDED")
do{
try ttDamageItems.add(row: row)
} catch{
print("serializeDamageTTable: Row can't be added: \(error)")
}
}
return ttDamageItems
}
When I deserialise I'm using this:
// Resize back all CGPoints
func scaleAllCGPoints(damageItems: [DamageItem], canvas: CanvasView) -> [[CGPoint]] {
return damageItems.flatMap { damageItem in
damageItem.scribbles.map { scribble in
scribble.map { point in
let factor: CGFloat = 240 / canvas.bounds.width
let scaledX = point.x / factor
let scaledY = point.y / factor
return CGPoint(x: scaledX, y: scaledY)
}
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
// Cell with the image recorded + scribbles on the image
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "reviewCanvasCell", for: indexPath) as! ReviewCanvasCell
// ------ Resize back all CGPoints ---------
let scribbleCGPoints = scaleAllCGPoints(damageItems: reviewDamageItems, canvas: cell.reviewCanvas)
cell.reviewCanvas.currentScribble = scribbleCGPoints
// ------ Unscaled version ---------
cell.reviewImageView.image = UIImage(data: report.imageRecorded ?? Data()) // Load Image recorded from CoreData
cell.reviewCanvas.damageItems = reviewDamageItems // Load Scribbles on the image from CoreData
cell.selectedDamageIndex = tableView.indexPathForSelectedRow?.row
return cell
default: return UITableViewCell()
}
}
Thanks for reading this !

Couple needed changes...
Prevent out-of-bounds coordinates. As the user drags to draw a line, if the coordinates go outside the bounds of the "canvas" view, change the x/y to minimum of 0 (zero), and max of bounds.maxX / bounds.maxY
Change the canvas view height to the resulting height of the imageView's Aspect-Fit ratio (in your specific case, the images will always be in 240:127 ratio).

Related

Align Nodes in tree hierarchy

Note:
I am looking for alternate solution, Since I have tried changing lots of Sprite Kit properties values, and came upto conclusion that it is not possible with those changes. So I ain't posting any code. You can try the mentioned library for more information.
Original Question:
I am using BLBubbleFilters library to create Bubbles.
This library used magnetic property to pull align items to the centre of the screen. And each time the screen opens the positioning of each node is different.
What have I tried so far?
I have tried playing around with the library and its physics attributes(e.g. Magnetic property) to achieve my goal.
The goal is:
Place 'Family' node over and above all nodes, and rest of the nodes below it.
What I thought is to that, each node will have a property for example "priority(Integer)". That is, the Priority 0 nodes will be placed above all nodes, priority 1 nodes below 0 priority nodes and so on.
Since SpriteKit deals with Physics properties, is there is any such property or way to achieve my goal?
See, how the family bubble is at the Top.
I had to do something like that and I did By creating a custom SelectInterestsCollectionViewFlowLayOut and I added the bubbles in the UICollectionViewCells.
The result was this.
Every cell is a white square and inside I have an image view with corner radius 1/2 * height.
The difference is that in my case I had the same bubble size for all my bubbles.
My code is:
enum InterestBubbleType {
case A
case B
case C
case D
case E
func getYPosition(cellSideSize: CGFloat) -> CGFloat {
let spaceFromTopForTwoCellsOnTop: CGFloat = cellSideSize / 2
switch self {
case .A:
return cellSideSize
case .B:
return cellSideSize * 2
case .C:
return spaceFromTopForTwoCellsOnTop
case .D:
return cellSideSize + spaceFromTopForTwoCellsOnTop
case .E:
return 0
}
}
}
class SelectInterestsCollectionViewFlowLayOut: UICollectionViewFlowLayout {
var cache: [UICollectionViewLayoutAttributes] = []
var layoutAttributes: [UICollectionViewLayoutAttributes] = []
var cellSideSize: CGFloat!
var xPosition: CGFloat = 0
var itemPositions: [InterestBubbleType] = [.A,.C,.D,.E,.A,.B,.C,.D,.E,.A,.B,.C,.D,.A,.B,.C,.D,.E,.A,.B]
var positionsToIncreaseXPosition = [0,2,5,7,10,12,14,16]
override func prepare() {
cache = []
super.prepare()
//Prepare constants
self.xPosition = 0
cellSideSize = self.collectionView!.height() / 3
self.configureCasheForTwentyInterests()
}
private func configureCasheForTwentyInterests() {
for i in 0...(self.itemPositions.count - 1) {
let indexPath = IndexPath(item: i, section: 0)
let xPosition = self.xPosition
let frame = CGRect(x: xPosition, y: self.itemPositions[i].getYPosition(cellSideSize: self.cellSideSize), width: self.cellSideSize, height: self.cellSideSize)
let atribute = UICollectionViewLayoutAttributes(forCellWith: indexPath)
atribute.frame = frame
cache.append(atribute)
if self.positionsToIncreaseXPosition.contains(i) {
self.xPosition += self.cellSideSize
}
}
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
self.layoutAttributes = []
for uiCollectionViewLayoutAttribute in self.cache {
self.layoutAttributes.append(uiCollectionViewLayoutAttribute)
}
return layoutAttributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return layoutAttributes[indexPath.row]
}
override var collectionViewContentSize: CGSize {
let totalWidth = cellSideSize * 9
return CGSize(width: totalWidth, height: self.collectionView!.height())
}
}
Now how it works is that in my collection view there are 5 different y positions that a bubble can be inside the collection view and they are A,B,C,D,E and I also keep in mind when you need to move 1 more x position which is 1 * collectionViewWidth size.
You can configure the code to achive your own result.
In my case I had a fixed number of bubbles and a fixed view to show them. The reason I chose to do it with collection view was because if the number change you can just add positions to change x position in positionsToIncreaseXPosition and it will work.

How to darken texture of sprite node

I have the following code where I create a sprite node that displays an animated GIF. I want to create another function that darkens the GIF when called upon. I could still be able to watch the animation, but the content would be visibly darker. I'm not sure how to approach this. Should I individually darken every texture or frame used to create the animation? If so, how do I darken a texture or frame in the first place?
// Extract frames and duration
guard let imageData = try? Data(contentsOf: url as URL) else {
return
}
let source = CGImageSourceCreateWithData(imageData as CFData, nil)
var images = [CGImage]()
let count = CGImageSourceGetCount(source!)
var delays = [Int]()
// Fill arrays
for i in 0..<count {
// Add image
if let image = CGImageSourceCreateImageAtIndex(source!, i, nil) {
images.append(image)
}
// At it's delay in cs
let delaySeconds = UIImage.delayForImageAtIndex(Int(i),
source: source)
delays.append(Int(delaySeconds * 1000.0)) // Seconds to ms
}
// Calculate full duration
let duration: Int = {
var sum = 0
for val: Int in delays {
sum += val
}
return sum
}()
// Get frames
let gcd = SKScene.gcdForArray(delays)
var frames = [SKTexture]()
var frame: SKTexture
var frameCount: Int
for i in 0..<count {
frame = SKTexture(cgImage: images[Int(i)])
frameCount = Int(delays[Int(i)] / gcd)
for _ in 0..<frameCount {
frames.append(frame)
}
}
let gifNode = SKSpriteNode.init(texture: frames[0])
gifNode.position = CGPoint(x: skScene.size.width / 2.0, y: skScene.size.height / 2.0)
gifNode.name = "content"
// Add animation
let gifAnimation = SKAction.animate(with: frames, timePerFrame: ((Double(duration) / 1000.0)) / Double(frames.count))
gifNode.run(SKAction.repeatForever(gifAnimation))
skScene.addChild(gifNode)
I would recommend to use the colorize(with:colorBlendFactor:duration:) method. It is an SKAction that animates changing the color of a whole node. That way you don't have to get into darkening the individual textures or frames, and it also adds a nice transition from a non-dark to a darkened color. Once the action ends, the node will stay darkened until you undarken it, so any changes to the node's texture will also be visible as darkend to the user.
Choose whatever color and colorBlendFactor will work best for you to have the darkened effect you need, e.g. you could set the color to .black and colorBlendFactor to 0.3. To undarken, just set the color to .clear and colorBlendFactor to 0.
Documentation here.
Hope this helps!

Swift 3 - How to remove all Subviews from Superview as well as Array

I currently have a double for-loop that creates an X by Y grid of UIView CGRect squares. The loop also adds each UIView/Square of the grid to a 2D array allowing me to access each Cell of the grid and alter color/positioning by the index values of the loop.
The loop seems to work fine and displays the Cells/Squares perfectly, however after a while I want to remove all of the squares and also empty the array (but not entirely delete) to make room for a new next grid (which may be of a different size). I created a function to remove all the views from the superview.
This is how I am creating each "Cell" of the grid and placing each into the 2D array:
let xStart = Int(size.width/2/2) - ((gridSizeX * gridScale)/2)
let yStart = Int(size.height/2/2) - ((gridSizeY * gridScale)/2)
let cell : UIView!
cell = UIView(frame: CGRect(x: xStart + (xPos * gridScale), y:yStart + (yPos * gridScale), width:gridScale, height:gridScale))
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgColor
cell.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0)
cell.tag = 100
self.view?.addSubview(cell)
gridArray[xPos][yPos] = cell
The 2D array is being created on application load like so:
gridArray = Array(repeating: Array(repeating: nil, count: gridSizeY), count: gridSizeX)
I have tried to search for a solution to remove the UIViews from the superview however all answers from other Questions don't seem to work for me. I have tried to add cell.tag = 100 to each UIView and then remove all UIViews with the Tag Value of 100 like:
for subview in (self.view?.subviews)! {
if (subview.tag == 100) {
subview.removeFromSuperview()
}
}
However no luck. I have also tried to use this method:
self.view?.subviews.forEach({
$0.removeConstraints($0.constraints)
$0.removeFromSuperview()
})
The main differences I notice in my code compared to other peoples answers is that I have "?" and "!" at places in the code. I researched about what they mean and understood most of it however I still do not know how to fix my code because if it, and feel as though that is the issue. All I know is that the attempts to remove the UIViews from the superview does not work, and without the "?" and "!"s the code doesn't run at all.
How about to create tag for each cell you are using for example:
//Init value for for the tag.
var n = 0
func prepareCell() -> UIView {
let xStart = Int(size.width/2/2) - ((gridSizeX * gridScale)/2)
let yStart = Int(size.height/2/2) - ((gridSizeY * gridScale)/2)
let cell : UIView!
cell = UIView(frame: CGRect(x: xStart + (xPos * gridScale), y:yStart + (yPos * gridScale), width:gridScale, height:gridScale))
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgColor
cell.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0)
cell.tag = n
//Each cell should have new value
n += 1
return cell
}
And now remove required views.
func removeViews() {
for z in 0...n {
if let viewWithTag = self.view.viewWithTag(z) {
viewWithTag.removeFromSuperview()
}
else {
print("tag not found")
}
}
}
Example that is working in the playground:
var n = 0
let mainView = UIView()
func createView() -> UIView {
let view = UIView()
view.tag = n
n += 1
return view
}
for i in 0...16 {
mainView.addSubview(createView())
}
func removeViews() {
for z in 0...n {
if let viewWithTag = mainView.viewWithTag(z) {
viewWithTag.removeFromSuperview()
print("removed")
}
else {
print("tag not found")
}
}
}
You might be overlooking a much, much simpler way to do this...
You have built a 2D Array containing references to the "cells" as you populated your view. So, just use that Array to remove them.
// when you're ready to remove them
for subArray in gridArray {
for cell in subArray {
cell.removeFromSuperview()
}
}
// clear out the Array
gridArray = Array<Array<UIView>>()

Swiping left to create a new collectionViewCell

I'm trying to implement a smoother way for a use to add new collectionViewCells for myCollectionView (which only shows one cell at a time). I want it to be something like when the user swipes left and if the user is on the last cell myCollectionView inserts a new cell when the user is swiping so that the user swipes left "into" the cell. And also I only allow the user to scroll one cell at a time.
EDIT:
So I think it is a bit hard to describe it in words so here is a gif to show what I mean
So in the past few weeks I have been trying to implement this in a number of different ways, and the one that I have found the most success with is by using the scrollViewWillEndDragging delegate method and I have implemented it like this:
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Getting the size of the cells
let flowLayout = myCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
let cellWidth = flowLayout.itemSize.width
let cellPadding = 10.0 as! CGFloat
// Calculating which page "card" we should be on
let currentOffset = scrollView.contentOffset.x - cellWidth/2
print("currentOffset is \(currentOffset)")
let cardWidth = cellWidth + cellPadding
var page = Int(round((currentOffset)/(cardWidth) + 1))
print("current page number is: \(page)")
if (velocity.x < 0) {
page -= 1
}
if (velocity.x > 0) {
page += 1
}
print("Updated page number is: \(page)")
print("Previous page number is: \(self.previousPage)")
// Only allowing the user to scroll for one page!
if(page > self.previousPage) {
page = self.previousPage + 1
self.previousPage = page
}
else if (page == self.previousPage) {
page = self.previousPage
}
else {
page = self.previousPage - 1
self.previousPage = page
}
print("new page number is: " + String(page))
print("addedCards.count + 1 is: " + String(addedCards.count + 1))
if (page == addedCards.count) {
print("reloading data")
// Update data source
addedCards.append("card")
// Method 1
cardCollectionView.reloadData()
// Method 2
// let newIndexPath = NSIndexPath(forItem: addedCards.count - 1, inSection: 0)
// cardCollectionView.insertItemsAtIndexPaths([newIndexPath])
}
// print("Centering on new cell")
// Center the cardCollectionView on the new page
let newOffset = CGFloat(page * Int((cellWidth + cellPadding)))
print("newOffset is: \(newOffset)")
targetContentOffset.memory.x = newOffset
}
Although I think I have nearly got the desired result there are still some concerns and also bugs that I have found.
My main concern is the fact that instead of inserting a single cell at the end of myCollectionView I'm reloading the whole table. The reason that I do this is because if I didn't then myCollectionView.contentOffset wouldn't be changed and as a result when a new cell is created, myCollectionView isn't centered on the newly created cell.
1. If the user scrolls very slowly and then stops, the new cell gets created but then myCollectionView gets stuck in between two cells it doesn't center in on the newly created cell.
2. When myCollectionView is in between the second last cell and the last cell as a result of 1., the next time the user swipes right, instead of creating one single cell, two cells are created.
I've also used different ways to implement this behaviour such as using scrollViewDidScroll, and various other but to no avail. Can anyone point me in the right direction as I am kind of lost.
Here is a link to download my project if you want to see the interaction:
My Example Project
These the old methods if you're interested:
To do this I have tried 2 ways,
The first being:
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint,
targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// page is the current cell that the user is on
// addedCards is the array that the data source works with
if (page == addedCards.count + 1) {
let placeholderFlashCardProxy = FlashCardProxy(phrase: nil, pronunciation: nil, definition: nil)
addedCards.append(placeholderFlashCardProxy)
let newIndexPath = NSIndexPath(forItem: addedCards.count, inSection: 0)
cardCollectionView.insertItemsAtIndexPaths([newIndexPath])
cardCollectionView.reloadData()
}
}
The problem with using this method is that:
Sometimes I will get a crash as a result of: NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0.
When a new collectionCell is added it will sometimes display the input that the user has written from the previous cell (this might have sometimes to do with the dequeuing and re-use of cell, although again, I'm not sure and I would be grateful if someone answered it)
The inserting isn't smooth, I want the user to be able to swipe left at the last cell and "into" a new cell. As in if I am currently on the last cell, swiping left would put me automatically at the new cell, because right now when I swipe left a new cell is created by it doesn't center on the newly created cell
The second method that I am using is:
let swipeLeftGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipedLeftOnCell:")
swipeLeftGestureRecognizer.direction = .Left
myCollectionView.addGestureRecognizer(swipeLeftGestureRecognizer)
swipeLeftGestureRecognizer.delegate = self
Although the swipe gesture very rarely responds, but if I use a tap gesture, myCollectionView always responds which is very weird (I know again this is a question on its own)
My question is which is the better way to implement what I have described above? And if none are good what should I work with to create the desired results, I've been trying to do this for two days now and I was wondering if someone could point me in the right direction. Thanks!
I hope this helps out in some way :)
UPDATE
I updated the code to fix the issue scrolling in either direction.
The updated gist can be found here
New Updated Gist
Old Gist
First I'm gonna define some model to for a Card
class Card {
var someCardData : String?
}
Next, create a collection view cell, with a card view inside that we will apply the transform to
class CollectionViewCell : UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(cardView)
self.addSubview(cardlabel)
}
override func prepareForReuse() {
super.prepareForReuse()
cardView.alpha = 1.0
cardView.layer.transform = CATransform3DIdentity
}
override func layoutSubviews() {
super.layoutSubviews()
cardView.frame = CGRectMake(contentPadding,
contentPadding,
contentView.bounds.width - (contentPadding * 2.0),
contentView.bounds.height - (contentPadding * 2.0))
cardlabel.frame = cardView.frame
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
lazy var cardView : UIView = {
[unowned self] in
var view = UIView(frame: CGRectZero)
view.backgroundColor = UIColor.whiteColor()
return view
}()
lazy var cardlabel : UILabel = {
[unowned self] in
var label = UILabel(frame: CGRectZero)
label.backgroundColor = UIColor.whiteColor()
label.textAlignment = .Center
return label
}()
}
Next setup the view controller with a collection view. As you will see there is a CustomCollectionView class, which I will define near the end.
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var cards = [Card(), Card()]
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.frame = CGRectMake(0, 0, self.view.bounds.width, tableViewHeight)
collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, contentPadding)
}
lazy var collectionView : CollectionView = {
[unowned self] in
// MARK: Custom Flow Layout defined below
var layout = CustomCollectionViewFlowLayout()
layout.contentDelegate = self
var collectionView = CollectionView(frame: CGRectZero, collectionViewLayout : layout)
collectionView.clipsToBounds = true
collectionView.showsVerticalScrollIndicator = false
collectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView.delegate = self
collectionView.dataSource = self
return collectionView
}()
// MARK: UICollectionViewDelegate, UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cards.count
}
func collectionView(collectionView : UICollectionView, layout collectionViewLayout:UICollectionViewLayout, sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize {
return CGSizeMake(collectionView.bounds.width, tableViewHeight)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cellIdentifier = "CollectionViewCell"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell
cell.contentView.backgroundColor = UIColor.blueColor()
// UPDATE If the cell is not the initial index, and is equal the to animating index
// Prepare it's initial state
if flowLayout.animatingIndex == indexPath.row && indexPath.row != 0{
cell.cardView.alpha = 0.0
cell.cardView.layer.transform = CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0)
}
return cell
}
}
UPDATED - Now For the really tricky part. I'm gonna define the CustomCollectionViewFlowLayout. The protocol callback returns the next insert index calculated by the flow layout
protocol CollectionViewFlowLayoutDelegate : class {
func flowLayout(flowLayout : CustomCollectionViewFlowLayout, insertIndex index : NSIndexPath)
}
/**
* Custom FlowLayout
* Tracks the currently visible index and updates the proposed content offset
*/
class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
weak var contentDelegate: CollectionViewFlowLayoutDelegate?
// Tracks the card to be animated
// TODO: - Adjusted if cards are deleted by one if cards are deleted
private var animatingIndex : Int = 0
// Tracks thje currently visible index
private var visibleIndex : Int = 0 {
didSet {
if visibleIndex > oldValue {
if visibleIndex > animatingIndex {
// Only increment the animating index forward
animatingIndex = visibleIndex
}
if visibleIndex + 1 > self.collectionView!.numberOfItemsInSection(0) - 1 {
let currentEntryIndex = NSIndexPath(forRow: visibleIndex + 1, inSection: 0)
contentDelegate?.flowLayout(self, insertIndex: currentEntryIndex)
}
} else if visibleIndex < oldValue && animatingIndex == oldValue {
// if we start panning to the left, and the animating index is the old value
// let set the animating index to the last card.
animatingIndex = oldValue + 1
}
}
}
override init() {
super.init()
self.minimumInteritemSpacing = 0.0
self.minimumLineSpacing = 0.0
self.scrollDirection = .Horizontal
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// The width offset threshold percentage from 0 - 1
let thresholdOffsetPrecentage : CGFloat = 0.5
// This is the flick velocity threshold
let velocityThreshold : CGFloat = 0.4
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
let leftThreshold = CGFloat(collectionView!.bounds.size.width) * ((CGFloat(visibleIndex) - 0.5))
let rightThreshold = CGFloat(collectionView!.bounds.size.width) * ((CGFloat(visibleIndex) + 0.5))
let currentHorizontalOffset = collectionView!.contentOffset.x
// If you either traverse far enought in either direction,
// or flicked the scrollview over the horizontal velocity in either direction,
// adjust the visible index accordingly
if currentHorizontalOffset < leftThreshold || velocity.x < -velocityThreshold {
visibleIndex = max(0 , (visibleIndex - 1))
} else if currentHorizontalOffset > rightThreshold || velocity.x > velocityThreshold {
visibleIndex += 1
}
var _proposedContentOffset = proposedContentOffset
_proposedContentOffset.x = CGFloat(collectionView!.bounds.width) * CGFloat(visibleIndex)
return _proposedContentOffset
}
}
And define the delegate methods in your view controller to insert a new card when the delegate tell it that it needs a new index
extension ViewController : CollectionViewFlowLayoutDelegate {
func flowLayout(flowLayout : CustomCollectionViewFlowLayout, insertIndex index : NSIndexPath) {
cards.append(Card())
collectionView.performBatchUpdates({
self.collectionView.insertItemsAtIndexPaths([index])
}) { (complete) in
}
}
And below is the custom Collection view that applies the animation while scrolling accordingly :)
class CollectionView : UICollectionView {
override var contentOffset: CGPoint {
didSet {
if self.tracking {
// When you are tracking the CustomCollectionViewFlowLayout does not update it's visible index until you let go
// So you should be adjusting the second to last cell on the screen
self.adjustTransitionForOffset(NSIndexPath(forRow: self.numberOfItemsInSection(0) - 1, inSection: 0))
} else {
// Once the CollectionView is not tracking, the CustomCollectionViewFlowLayout calls
// targetContentOffsetForProposedContentOffset(_:withScrollingVelocity:), and updates the visible index
// by adding 1, thus we need to continue the trasition on the second the last cell
self.adjustTransitionForOffset(NSIndexPath(forRow: self.numberOfItemsInSection(0) - 2, inSection: 0))
}
}
}
/**
This method applies the transform accordingly to the cell at a specified index
- parameter atIndex: index of the cell to adjust
*/
func adjustTransitionForOffset(atIndex : NSIndexPath) {
if let lastCell = self.cellForItemAtIndexPath(atIndex) as? CollectionViewCell {
let progress = 1.0 - (lastCell.frame.minX - self.contentOffset.x) / lastCell.frame.width
lastCell.cardView.alpha = progress
lastCell.cardView.layer.transform = CATransform3DScale(CATransform3DIdentity, progress, progress, 0.0)
}
}
}
I think u missed one point here in
let newIndexPath = NSIndexPath(forItem: addedCards.count, inSection: 0)
the indexPath should be
NSIndexPath(forItem : addedCards.count - 1, inSection : 0) not addedCards.count
Thats why you were getting the error
NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0

UICollectionView - Drag Cells outside the UICollectionView to specific UIViews

I have a UICollectionView with alot of UICollectionViewCells inside it.
Searched alot, but couldn't find anything near this issue.
My goal is to drag one of the Pink squares (UICollectionViewCell) to the Gray view (UIView).
You can think of it like dragging a folder on your desktop to your trash can except that I dont want the Pink square to be deleted. Or dragging a icon from your dock (it snaps back).
When the pink area hits the gray area it should just go back to its original position and the same when dropping out anywhere else using some kind of easy statement.
Can anyone help me out or redirect me to some references or samples?
Much appreciated, if you would like more info. Let me know.
If i managed to understand you correctly, i did something very similar once before, i found its much better to use a fake image of the cell, instead of the cell itself.
So for example, cell number 2 is starting to be dragged, you immediately call "SetHidden=YES", and draw at the exact same place a fake one with a simple draw method:
UIGraphicsBeginImageContext(self.cell.bounds.size);
[self.cell.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This one you can move as you like, and when it should go back, you do it the same way.
Hope this helps you..
this worked for me in swift 3:
class DragAndDropGestureManager: NSObject {
var originalPosition: CGPoint?
var draggedView: UIView?
var viewSize: CGSize?
var originalZIndex: CGFloat = 0
#objc func handleDrag(_ recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .began:
if let view = recognizer.view {
draggedView = view
originalPosition = view.frame.origin
viewSize = view.frame.size
originalZIndex = view.layer.zPosition
view.layer.zPosition = 1
}
case .changed:
if let view = recognizer.view {
let currentPosition = recognizer.location(in: view.superview!)
let newPosition = CGPoint(x: currentPosition.x - (viewSize!.width / 2.0), y: currentPosition.y - (viewSize!.height / 2.0))
view.frame.origin = newPosition
} else {
self.finishDrag()
}
case .ended:
finishDrag()
default:
print("drag default \(recognizer.state.rawValue)")
}
}
func finishDrag() {
self.draggedView?.frame.origin = originalPosition!
self.draggedView?.layer.zPosition = originalZIndex
originalZIndex = 0
originalPosition = nil
draggedView = nil
}
}
i added a gesture recognizer in the function public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
like this:
if self.dragManager != nil && cell.gestureRecognizers == nil {
cell.addGestureRecognizer(UIPanGestureRecognizer(target: dragManager!, action: #selector(DragAndDropGestureManager.handleDrag)))
}
the if, is for not adding gesutre recognition more than once because the cell are being reused
don't forget to add collectioView.clipToBounds = false otherwise the dragged cell will be clipped when trying to move out of the collection view's bounds

Resources