Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yonezawaizumi
Last active January 25, 2017 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yonezawaizumi/b8a8bcdda952f3b205e71d7c51e8dfc1 to your computer and use it in GitHub Desktop.
Save yonezawaizumi/b8a8bcdda952f3b205e71d7c51e8dfc1 to your computer and use it in GitHub Desktop.
画像をダブルタップとピンチイン・ピンチアウトで拡大・縮小する Swift3編 ref: http://qiita.com/yonezawaizumi/items/bd3f53b2f4d80f815357
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet var myImageView: UIImageView!
@IBOutlet var myScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.myScrollView.delegate = self
let doubleTapGesture = UITapGestureRecognizer(target: self, action:#selector(self.doubleTap))
doubleTapGesture.numberOfTapsRequired = 2
self.myImageView.addGestureRecognizer(doubleTapGesture)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
//print("pinch")
return self.myImageView
}
func doubleTap(gesture: UITapGestureRecognizer) -> Void {
//print(self.myScrollView.zoomScale)
if (self.myScrollView.zoomScale < self.myScrollView.maximumZoomScale) {
let newScale = self.myScrollView.zoomScale * 3
let zoomRect = self.zoomRectForScale(scale: newScale, center: gesture.location(in: gesture.view))
self.myScrollView.zoom(to: zoomRect, animated: true)
} else {
self.myScrollView.setZoomScale(1.0, animated: true)
}
}
func zoomRectForScale(scale:CGFloat, center: CGPoint) -> CGRect{
let size = CGSize(
width: self.myScrollView.frame.size.width / scale,
height: self.myScrollView.frame.size.height / scale
)
return CGRect(
origin: CGPoint(
x: center.x - size.width / 2.0,
y: center.y - size.height / 2.0
),
size: size
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment