ABOUT ME

Today
Yesterday
Total
  • Peek and Pop 코드
    앱등이에게 살충제를 뿌린다./일기는 일기장에 2017. 6. 6. 17:40

    <Like or Like>



    ViewController.swift

    import UIKit
    class ViewController: UIViewController {
    @IBOutlet weak var collectionView: UICollectionView!
    var photos: [UIImage] {
    var photoList: [UIImage] = []
    for index in 0..<9 {
    let image = UIImage(named: "\(index).jpg")
    photoList.append(image!)
    }
    return photoList
    }
    override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    if traitCollection.forceTouchCapability == .available {
    registerForPreviewing(with: self, sourceView: view)
    }
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? DetailViewController {
    destination.image = (sender as? CollectionViewCell)?.imageView.image
    }
    }
    }
    extension ViewController: UIViewControllerPreviewingDelegate {
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
    print("\(#function) called.")
    // showDetailViewController(viewControllerToCommit, sender: self)
    }
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    guard let indexPath = collectionView.indexPathForItem(at: location) else { return nil }
    guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell else { return nil }
    guard let detailVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else { return nil }
    detailVC.image = photos[indexPath.row]
    detailVC.preferredContentSize = CGSize(width: 0, height: 300)
    previewingContext.sourceRect = cell.frame
    return detailVC
    }
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    }
    }
    extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return photos.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as? CollectionViewCell else { return UICollectionViewCell() }
    cell.imageView.image = photos[indexPath.row]
    return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.bounds.size.width / 2.5, height: collectionView.bounds.size.height)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: 0, height: 0)
    }
    }
    class CollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var imageView: UIImageView!
    }

    DetailViewController.swift

    import UIKit
    class DetailViewController: UIViewController {
    var image: UIImage?
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
    super.viewDidLoad()
    imageView.image = image
    }
    override var previewActionItems: [UIPreviewActionItem] {
    let likeAction = UIPreviewAction(title: "Like", style: .default) { (action, viewController) in
    print("Like pressed. action: \(action), viewController: \(viewController)")
    }
    let deleteAction = UIPreviewAction(title: "Like", style: .destructive) { (action, viewController) in
    print("Delete pressed. action: \(action), viewController: \(viewController)")
    }
    return [likeAction, deleteAction]
    }
    }


Designed by Tistory.