Skip to content

Instantly share code, notes, and snippets.

@alfredcc
Created November 30, 2015 08:58
Show Gist options
  • Save alfredcc/5df7dba252721c84e088 to your computer and use it in GitHub Desktop.
Save alfredcc/5df7dba252721c84e088 to your computer and use it in GitHub Desktop.
An ImageViewer
//
// ImageViewerViewController.swift
// GJHP2P
//
// Created by race on 15/11/30.
// Copyright © 2015年 Guijinhui. All rights reserved.
//
import UIKit
class ImageViewerViewController: UIViewController, UIScrollViewDelegate {
let scrollView = UIScrollView()
let imageView = UIImageView()
var image: UIImage?
// MARK: Life-Cycle
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
func setupView() {
view.backgroundColor = UIColor.blackColor()
scrollView.frame = view.bounds
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 3.0
scrollView.zoomScale = 1.0
view.addSubview(scrollView)
if let image = image {
imageView.contentMode = .ScaleAspectFill
imageView.frame = centerFrameFromImage(image)
imageView.image = image
}
scrollView.addSubview(imageView)
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTapRecognizer.numberOfTapsRequired = 2
doubleTapRecognizer.numberOfTouchesRequired = 1
scrollView.addGestureRecognizer(doubleTapRecognizer)
}
// MARK: Gesture Recognizers
func handleDoubleTap(recognizer: UITapGestureRecognizer!) {
let pointInView = recognizer.locationInView(self.imageView)
self.zoomInZoomOut(pointInView)
}
// MARK: ScrollView
func centerFrameFromImage(image: UIImage?) -> CGRect {
if image == nil {
return CGRectZero
}
let scaleFactor = scrollView.frame.size.width / image!.size.width
let newHeight = image!.size.height * scaleFactor
var newImageSize = CGSize(width: scrollView.frame.size.width, height: newHeight)
newImageSize.height = min(scrollView.frame.size.height, newImageSize.height)
let centerFrame = CGRect(x: 0.0, y: scrollView.frame.size.height/2 - newImageSize.height/2, width: newImageSize.width, height: newImageSize.height)
return centerFrame
}
func scrollViewDidZoom(scrollView: UIScrollView) {
self.centerScrollViewContents()
}
func centerScrollViewContents() {
let boundsSize = scrollView.frame
var contentsFrame = self.imageView.frame
if contentsFrame.size.width < boundsSize.width {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0
} else {
contentsFrame.origin.x = 0.0
}
if contentsFrame.size.height < boundsSize.height {
contentsFrame.origin.y = (boundsSize.height - scrollView.scrollIndicatorInsets.top - scrollView.scrollIndicatorInsets.bottom - contentsFrame.size.height) / 2.0
} else {
contentsFrame.origin.y = 0.0
}
self.imageView.frame = contentsFrame
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.imageView
}
func zoomInZoomOut(point: CGPoint!) {
let newZoomScale = self.scrollView.zoomScale > (self.scrollView.maximumZoomScale/2) ? self.scrollView.minimumZoomScale : self.scrollView.maximumZoomScale
let scrollViewSize = self.scrollView.bounds.size
let width = scrollViewSize.width / newZoomScale
let height = scrollViewSize.height / newZoomScale
let x = point.x - (width / 2.0)
let y = point.y - (height / 2.0)
let rectToZoom = CGRect(x: x, y: y, width: width, height: height)
self.scrollView.zoomToRect(rectToZoom, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment