// 注意判斷條件, ide
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {spa
if let cell = sender as? UICollectionViewCell,3d
let indexPath = collectionView?.indexPath(for: cell),it
let managePageViewController = segue.destination as? ManagePageViewController {io
managePageViewController.photos = photosclass
managePageViewController.currentIndex = indexPath.rowdate
}file
}select
}scroll
// UIPageViewController 用法
if let viewController = viewPhotoCommentController(index: currentIndex ?? 0) {
let viewControllers = [viewController]
setViewControllers (
viewControllers,
direction: .forward,
animated: false,
completion: nil
)
}
// 來自storyboard的VC,初始化方法
fileprivate func viewPhotoCommentController(index: Int) -> PhotoCommentViewController? {
if let storyboard = storyboard,
let page = storyboard.instantiateViewController(withIdentifier: "PhotoCommentViewController")
as? PhotoCommentViewController {
page.photoName = photos[index]
page.photoIndex = index
return page
}
return nil
}
// UIPageViewControllerDataSource
extension ManagePageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let viewController = viewController as? PhotoCommentViewController {
guard let index = viewController.photoIndex, index != 0 else {
return nil
}
return viewPhotoCommentController(index: index - 1)
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let viewController = viewController as? PhotoCommentViewController {
guard let index = viewController.photoIndex, index != photos.count - 1 else {
return nil
}
return viewPhotoCommentController(index: index + 1)
}
return nil
}
/// MARK: UIPageControl
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return photos.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return currentIndex ?? 0
}
}
// 初始化Selector
fileprivate extension Selector {
static let keyboardWillShowHandler = #selector(PhotoCommentViewController.keyboardWillShow(notification:))
static let keyboardWillHideHandler = #selector(PhotoCommentViewController.keyboardWillHide(notification:))
static let generalTap = #selector(PhotoCommentViewController.dismissKeyboard)
static let zoomTap = #selector(PhotoCommentViewController.openZoomingController(sender:))
}
// UIScrollViewDelegate
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return myImageView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
updateConstraints(forSize: view.bounds.size)
}