已经用代码:
override func viewDidLoad() { super.viewDidLoad() self.title = bubbleImageView.resMsg.fileItem.name self.imageView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] self.imageView.contentMode = .ScaleAspectFit self.view.addSubview(self.imageView) constrain(imageView) {imageView in //imageView.edges <= imageView.superview!.edges imageView.left == imageView.superview!.left imageView.top == imageView.superview!.top imageView.right == imageView.superview!.right imageView.bottom == imageView.superview!.bottom } } |
显示了图片:
现在需要去实现:
pinch,捏,实现缩放图片
swift imageview pinch to scale
UIImageView pinch zoom swift – Stack Overflow
Scale images with Pinch Gesture in iOS with Swift – iOScreator
Pinch To Zoom – UIImageView with Swift — Vea Software
UIScrollView Tutorial: Getting Started
最后用代码:
// // ImageViewController.swift // JianDao // // Created by licrifan on 16/3/2. // Copyright © 2016年 licrifan. All rights reserved. // import UIKit class ImageViewController: UIViewController, UIScrollViewDelegate { var bubbleImageView:ImageBubbleView var imageView: UIImageView var scrollView:UIScrollView init(bubbleImageView:ImageBubbleView) { self.bubbleImageView = bubbleImageView // self.imageView = bubbleImageView.imageView self.imageView = UIImageView(image: bubbleImageView.imageView.image!) self.scrollView = UIScrollView() super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() self.title = bubbleImageView.resMsg.fileItem.name self.scrollView.addSubview(self.imageView) self.view.addSubview(self.scrollView) self.scrollView.frame = CGRectMake( 0, 0, self.view.frame.width, self.view.frame.height – statusBarFrame.height //20 – HeightNaviBar) //44 let imageSize = self.imageView.image!.size // print("imageSize=\(imageSize)") //(1359.0, 901.0) self.scrollView.contentSize = imageSize self.scrollView.bounces = false self.scrollView.showsHorizontalScrollIndicator = false self.scrollView.showsVerticalScrollIndicator = false self.scrollView.userInteractionEnabled = true self.scrollView.delegate = self self.scrollView.bouncesZoom = false self.scrollView.scrollsToTop = false let scrollViewFrame = scrollView.frame // print("scrollViewFrame=\(scrollViewFrame)") //(0.0, 0.0, 375.0, 667.0) let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width // print("scaleWidth=\(scaleWidth)") //0.275938189845475 let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height // print("scaleHeight=\(scaleHeight)") //0.740288568257492 let minScale = min(scaleWidth, scaleHeight) // print("minScale=\(minScale)") //0.275938189845475 self.scrollView.minimumZoomScale = minScale self.scrollView.maximumZoomScale = 1.0 self.scrollView.zoomScale = minScale centerScrollViewContents() let doubleTap = UITapGestureRecognizer(target: self, action: "doubleTapped:") doubleTap.numberOfTapsRequired = 2 doubleTap.numberOfTouchesRequired = 1 self.scrollView.addGestureRecognizer(doubleTap) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func centerScrollViewContents() { let boundsSize = self.scrollView.bounds.size // print("boundsSize=\(boundsSize)") var contentsFrame = self.imageView.frame // print("contentsFrame=\(contentsFrame)") 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 – contentsFrame.size.height) / 2.0 } else { contentsFrame.origin.y = 0.0 } // print("contentsFrame=\(contentsFrame)") self.imageView.frame = contentsFrame } func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? { return self.imageView } func scrollViewDidZoom(scrollView: UIScrollView) { centerScrollViewContents() } // func doubleTapped(tapGesture: UITapGestureRecognizer) { // print("tapGesture=\(tapGesture)") // if (self.scrollView.zoomScale == self.scrollView.minimumZoomScale) { // let center = tapGesture.locationInView(self.scrollView) // print("center=\(center)") // let size = self.imageView.image!.size // print("size=\(size)") // let zoomRect = CGRectMake(center.x, center.y, (size.width / 2), (size.height / 2)) // print("zoomRect=\(zoomRect)") // self.scrollView.zoomToRect(zoomRect, animated: true) // } else { // self.scrollView.setZoomScale(self.scrollView.minimumZoomScale, animated: true) // } // } func doubleTapped(recognizer: UITapGestureRecognizer) { if (self.scrollView.zoomScale == self.scrollView.maximumZoomScale) { self.scrollView.setZoomScale(self.scrollView.minimumZoomScale, animated: true) } else { let pointInView = recognizer.locationInView(self.imageView) // print("pointInView=\(pointInView)") //(898.155653734425, 425.882443769075) var newZoomScale = scrollView.zoomScale * 1.5 // print("newZoomScale=\(newZoomScale)") //0.986187634979701 newZoomScale = min(newZoomScale, scrollView.maximumZoomScale) // print("newZoomScale=\(newZoomScale)") //0.986187634979701 let scrollViewSize = scrollView.bounds.size // print("scrollViewSize=\(scrollViewSize)") //(375.0, 603.0) let w = scrollViewSize.width / newZoomScale let h = scrollViewSize.height / newZoomScale let x = pointInView.x – (w / 2.0) let y = pointInView.y – (h / 2.0) let rectToZoomTo = CGRectMake(x, y, w, h); // print("rectToZoomTo=\(rectToZoomTo)") //(708.029562766088, 120.159689491989, 380.252181936674, 611.445508554172) scrollView.zoomToRect(rectToZoomTo, animated: true) } } } |
至少实现了基本的功能了:
显示图片,是图片居中,且已当前屏幕缩放适配:
双击,自动放大1.5倍:
可以拖动:
放到最大后,双击会到最开始适配屏幕:
转载请注明:在路上 » [已解决]swift中实现图片的捏pinch去缩放图片