AYohannes
AYohannes

Reputation: 625

Cannot assign value of type 'CGPoint' to type 'CGFloat'

First I am trying to get the the layout And calculating the width of the cell including the spacing.After that I am trying to calculating the index of currently showing the Item.The end result i want the scrollview to count and end drugging whenever I stop scrolling But it gives me an error on the calculation. "Cannot assign value of type 'CGPoint' to type 'CGFloat'" no clue how to fix this. Help needed

here is the func code

extension HomeViewController : UIScrollViewDelegate {
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint,targetContentOffset targetContentOffSet: UnsafeMutablePointer<CGPoint>) {
    let  layOut = self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
    let cellWidthIncludingSpacing = layOut.itemSize.width + layOut.minimumLineSpacing
    var offSet = targetContentOffSet.pointee.x
     let index = (offSet.x + scrollView.contentInset.left) / cellWidthIncludingSpacing //error Value of type 'CGFloat' has no member 'x'
    let roundedIndex = round(visibleWidth)
    offSet = CGPoint(x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left,y: -scrollView.contentInset.top) //"Cannot assign value of type 'CGPoint' to type 'CGFloat'"
    targetContentOffSet.pointee = offSet


}
}

Upvotes: 0

Views: 2930

Answers (1)

rob mayoff
rob mayoff

Reputation: 385610

Look at this line of your code:

var offSet = targetContentOffSet.pointee.x

What type does Swift deduce for offSet? You can option-click it in Xcode to find out, but we can see that it must be the type of CGPoint.x, which is CGFloat.

From the remaining lines of your function, I guess you want it to be type CGPoint, so change it to this:

var offSet = targetContentOffset.pointee

Upvotes: 2

Related Questions