Skip to content

Instantly share code, notes, and snippets.

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.badge, .sound, .alert]) { (granted, error) in
//granted = yes, if app is authorized for all of the requested interaction types
//granted = no, if one or more interaction type is disallowed
}
class CustomCollectionViewCell: UICollectionViewCell
{
//Gradient to add in the cell
private lazy var gradient: CAGradientLayer = {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.darkGray.cgColor, UIColor.orange.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
gradientLayer.frame = self.bounds
return gradientLayer
@IBDesignable class DesignableView: UIView
{
@IBInspectable var gradientColor1: UIColor = UIColor.white {
didSet{
self.setGradient()
}
}
@IBInspectable var gradientColor2: UIColor = UIColor.white {
didSet{
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.purple.cgColor, UIColor.yellow.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
gradientLayer.frame = view.bounds
let items = coordinator.items
var dIndexPath = destinationIndexPath
if dIndexPath.row >= collectionView.numberOfItems(inSection: 0)
{
dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
}
var sourceIndexPaths = [IndexPath]()
var destinationIndexPaths = [IndexPath]()
for item in items
{
func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters?
{
let previewParameters = UIDragPreviewParameters()
previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 25, y: 25, width: 120, height: 120))
return previewParameters
}
let items = coordinator.items
if items.count == 1, let item = items.first, let sourceIndexPath = item.sourceIndexPath
{
var dIndexPath = destinationIndexPath
if dIndexPath.row >= collectionView.numberOfItems(inSection: 0)
{
dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
}
collectionView.performBatchUpdates({
self.items.remove(at: sourceIndexPath.row)
collectionView.performBatchUpdates({
var indexPaths = [IndexPath]()
for (index, item) in coordinator.items.enumerated()
{
//Destination index path for each item is calculated separately using the destinationIndexPath fetched from the coordinator
let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
self.items.insert(item.dragItem.localObject as! String, at: indexPath.row)
indexPaths.append(indexPath)
}
collectionView.insertItems(at: indexPaths)
func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator)
{
let destinationIndexPath: IndexPath
if let indexPath = coordinator.destinationIndexPath
{
destinationIndexPath = indexPath
}
else
{
// Get last index path of collection view.
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
{
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
{
let item = self.items[sourceIndexPath.row]
self.items.remove(at: sourceIndexPath.row)
self.items.insert(item, at: destinationIndexPath.row)