khuong291 khuong291 - 7 months ago 36
Swift Question

How to get center point of cell

I want to show the

AddView
in my Xib File whenever I click on the cell which indexPath.row == 0:

enter image description here

But I don't want it appear like this, I want it to show from the center of the cell which
indexPath.row == 0
(The + image), and start with the width and height = 1, then it animate to be bigger and bigger, and come into the center of the view.

How to do that.

Here is my current code:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 && addViewIsShowed == false {
addViewIsShowed = true
collectionView.alpha = 0.5
addView.delegate = self
addView.center = (collectionView.cellForItemAtIndexPath(indexPath)?.contentView.center)!
addView.frame.size = CGSize(width: 1, height: 1)
addView.contentView.backgroundColor = UIColor.flatYellowColor()
view.insertSubview(addView, aboveSubview: collectionView)
UIView.animateWithDuration(0.8, delay: 0.2, options: [.CurveEaseInOut], animations: {
self.addView.bounds.size.width = 300
self.addView.bounds.size.height = 400
self.addView.center = self.view.center
}, completion: nil)
}
}


UPDATE 1

After doing base on @Zell B guide.

Code:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 && addViewIsShowed == false {
addViewIsShowed = true
collectionView.alpha = 0.5
addView.delegate = self
addView.frame.size = CGSize(width: 1, height: 1)
let cellContentView = collectionView.cellForItemAtIndexPath(indexPath)?.contentView
let rect = cellContentView!.convertRect(cellContentView!.frame, toView: self.view)
addView.center = CGPointMake(rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height / 2)
addView.contentView.backgroundColor = UIColor.flatYellowColor()
view.insertSubview(addView, aboveSubview: collectionView)
UIView.animateWithDuration(2.0, delay: 0.2, options: [.CurveEaseInOut], animations: {
self.addView.bounds.size.width = 120
self.addView.bounds.size.height = 160
self.addView.center = self.view.center
}, completion: nil)
}
}


The AddView still not change the size:

enter image description here

Update 2

After adding
addView.clipsToBounds = true
above of animation. It works.

enter image description here

Any helps would be appreciated. Thanks

Answer

By getting center point of cell directly from its content view you will get its center relative to cell superview, which in your case is collection view. Since UICollectionView is subclass of UIScrollView things will get complicated because of scrollView's contentView and you will hardly get the desired point. To simplify this problem you can easily convert cell's frame relative to view controller's view or relative to application window and then set the converted point to your added view.

let cellContentView = collectionView.cellForItemAtIndexPath(indexPath)?.contentView
let rect = cellContentView!.convertRect(cellContentView!.frame, toView: self.view) // pass toView nil if you want to convert rect relative to window
addView.center = CGPointMake(rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height / 2)