         contentView.frame = contentsFrame
     }
     
+    private func getDistanceToBorder(center center: CGPoint,distanceToBeOffset: CGFloat,insets: UIEdgeInsets) -> CGFloat {
+        let size = bounds.size
+        var	distanceToBorder: CGFloat = size.width
+        
+        //check if the view is close to the left
+        //changing the distance to border and the offset accordingly
+        let leftDistance = center.x - insets.left
+        if leftDistance < distanceToBeOffset && leftDistance < distanceToBorder {
+            distanceToBorder = leftDistance
+        }
+        
+        //same for top
+        let topDistance = center.y - insets.top
+        if topDistance < distanceToBeOffset && topDistance < distanceToBorder {
+            distanceToBorder = topDistance
+        }
+        
+        //same for right
+        let rightDistance = size.width - center.x - insets.right
+        if rightDistance < distanceToBeOffset && rightDistance < distanceToBorder {
+            distanceToBorder = rightDistance
+        }
+        
+        //same for bottom
+        let bottomDistance = size.height - center.y - insets.bottom
+        if bottomDistance < distanceToBeOffset && bottomDistance < distanceToBorder {
+            distanceToBorder = bottomDistance
+        }
+        
+        return distanceToBorder*2
+    }
+    
     private func centerOnIndex(index: Int, zoomScale: CGFloat) {
         guard centerOnEndScroll else { return }
         centerOnEndScroll = false

        
        views.enumerate().forEach { (viewIndex: Int, view: UIView) -> () in
            let center = view.center
            let potentialDistance = distanceBetweenPoint(x1: center.x, y1: center.y, x2: contentViewCenter.x, y2: contentViewCenter.y)
            
            if potentialDistance < distance || !hasItem {
                hasItem = true

        return index
    }
    
    private static func distanceBetweenPoint(point1 point1: CGPoint, point2: CGPoint) ->  CGFloat {
        let distance = Double((point1.x - point2.x) * (point1.x - point2.x) + (point1.y - point2.y) * (point1.y - point2.y))
        let squaredDistance = sqrt(distance)
        return CGFloat(squaredDistance)
    }
