「Ios/swift/画像ビューアー」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==画像ビューアーサンプル== import UIKit import SDWebImage class ImageDetailViewController: UIViewController, UIScrollViewDelegate { @IBOutlet weak var...」) |
(→画像ビューアーサンプル) |
||
(同じ利用者による、間の5版が非表示) | |||
行1: | 行1: | ||
==画像ビューアーサンプル== | ==画像ビューアーサンプル== | ||
+ | 画像urlを指定したサンプル | ||
import UIKit | import UIKit | ||
import SDWebImage | import SDWebImage | ||
class ImageDetailViewController: UIViewController, UIScrollViewDelegate { | class ImageDetailViewController: UIViewController, UIScrollViewDelegate { | ||
− | @IBOutlet weak var | + | @IBOutlet weak var _scrollView: UIScrollView! |
− | @IBOutlet weak var | + | @IBOutlet weak var _imageView: UIImageView! |
+ | private var _zoomScaleWidth:CGFloat = CGFloat() | ||
internal var imgpath: String! = "https://pbs.twimg.com/media/CnHuxaAWAAI0r3y.jpg" | internal var imgpath: String! = "https://pbs.twimg.com/media/CnHuxaAWAAI0r3y.jpg" | ||
override func viewDidLoad() { | override func viewDidLoad() { | ||
super.viewDidLoad() | super.viewDidLoad() | ||
− | + | navigationController?.setNavigationBarHidden(false, animated: false) | |
if imgpath.containsString("http://") || imgpath.containsString("https://") { | if imgpath.containsString("http://") || imgpath.containsString("https://") { | ||
let imageURL = NSURL(string: imgpath) | let imageURL = NSURL(string: imgpath) | ||
− | + | _imageView.sd_setImageWithURL(imageURL) | |
} else { | } else { | ||
− | + | _imageView.image = UIImage(named:imgpath) | |
} | } | ||
// 初期scale | // 初期scale | ||
let myBoundSize: CGSize = UIScreen.mainScreen().bounds.size | let myBoundSize: CGSize = UIScreen.mainScreen().bounds.size | ||
− | + | _zoomScaleWidth = CGFloat(_imageView.bounds.size.width) / CGFloat(myBoundSize.width) | |
− | + | self._scrollView.delegate = self | |
− | + | self._scrollView.minimumZoomScale = 0.1 | |
− | + | self._scrollView.maximumZoomScale = 8 | |
− | + | self._scrollView.scrollEnabled = true | |
− | + | self._scrollView.showsHorizontalScrollIndicator = true | |
− | self. | + | self._scrollView.showsVerticalScrollIndicator = true |
− | self. | + | |
− | self. | + | |
− | self. | + | |
− | self. | + | |
− | self. | + | |
let doubleTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self | let doubleTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self | ||
, action:#selector(self.doubleTap(_:))) | , action:#selector(self.doubleTap(_:))) | ||
doubleTapGesture.numberOfTapsRequired = 2 | doubleTapGesture.numberOfTapsRequired = 2 | ||
− | self. | + | self._imageView.userInteractionEnabled = true |
− | self. | + | self._imageView.addGestureRecognizer(doubleTapGesture) |
− | self. | + | self._scrollView.setZoomScale(_zoomScaleWidth, animated: false) |
+ | updateScrollInset() | ||
} | } | ||
override func didReceiveMemoryWarning() { | override func didReceiveMemoryWarning() { | ||
行42: | 行40: | ||
// ピンチイン・ピンチアウト | // ピンチイン・ピンチアウト | ||
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? { | func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? { | ||
− | return self. | + | return self._imageView |
} | } | ||
// ダブルタップ | // ダブルタップ | ||
func doubleTap(gesture: UITapGestureRecognizer) -> Void { | func doubleTap(gesture: UITapGestureRecognizer) -> Void { | ||
− | + | if ( self._scrollView.zoomScale < self._scrollView.maximumZoomScale ) { | |
− | if ( self. | + | let newScale:CGFloat = self._scrollView.zoomScale * 3.7 |
− | let newScale:CGFloat = self. | + | |
let zoomRect:CGRect = self.zoomRectForScale(newScale, center: gesture.locationInView(gesture.view)) | let zoomRect:CGRect = self.zoomRectForScale(newScale, center: gesture.locationInView(gesture.view)) | ||
− | self. | + | self._scrollView.zoomToRect(zoomRect, animated: true) |
} else { | } else { | ||
− | self. | + | self._scrollView.setZoomScale(_zoomScaleWidth, animated: true) |
} | } | ||
} | } | ||
行58: | 行55: | ||
func zoomRectForScale(scale:CGFloat, center: CGPoint) -> CGRect{ | func zoomRectForScale(scale:CGFloat, center: CGPoint) -> CGRect{ | ||
var zoomRect: CGRect = CGRect() | var zoomRect: CGRect = CGRect() | ||
− | zoomRect.size.height = self. | + | zoomRect.size.height = self._scrollView.frame.size.height / scale |
− | zoomRect.size.width = self. | + | zoomRect.size.width = self._scrollView.frame.size.width / scale |
zoomRect.origin.x = center.x - zoomRect.size.width / 2.0 | zoomRect.origin.x = center.x - zoomRect.size.width / 2.0 | ||
zoomRect.origin.y = center.y - zoomRect.size.height / 2.0 | zoomRect.origin.y = center.y - zoomRect.size.height / 2.0 | ||
return zoomRect | return zoomRect | ||
+ | } | ||
+ | func scrollViewDidZoom(scrollView: UIScrollView) { | ||
+ | updateScrollInset() | ||
+ | } | ||
+ | private func updateScrollInset() { | ||
+ | // imageViewの大きさからcontentInsetを再計算 | ||
+ | // なお、0を下回らないようにする | ||
+ | _scrollView.contentInset = UIEdgeInsetsMake( | ||
+ | max((_scrollView.frame.height - _imageView.frame.height)/2, 0), | ||
+ | max((_scrollView.frame.width - _imageView.frame.width)/2, 0), | ||
+ | 0, | ||
+ | 0 | ||
+ | ); | ||
} | } | ||
} | } | ||
− | + | ==参考== | |
+ | http://qiita.com/hanoopy/items/7a2c582cd9758e7a3076 | ||
+ | |||
+ | http://qiita.com/wmoai/items/52b1901e62d28dae9f91 |
2016年8月1日 (月) 12:05時点における最新版
画像ビューアーサンプル
画像urlを指定したサンプル
import UIKit import SDWebImage class ImageDetailViewController: UIViewController, UIScrollViewDelegate { @IBOutlet weak var _scrollView: UIScrollView! @IBOutlet weak var _imageView: UIImageView! private var _zoomScaleWidth:CGFloat = CGFloat() internal var imgpath: String! = "https://pbs.twimg.com/media/CnHuxaAWAAI0r3y.jpg" override func viewDidLoad() { super.viewDidLoad() navigationController?.setNavigationBarHidden(false, animated: false) if imgpath.containsString("http://") || imgpath.containsString("https://") { let imageURL = NSURL(string: imgpath) _imageView.sd_setImageWithURL(imageURL) } else { _imageView.image = UIImage(named:imgpath) } // 初期scale let myBoundSize: CGSize = UIScreen.mainScreen().bounds.size _zoomScaleWidth = CGFloat(_imageView.bounds.size.width) / CGFloat(myBoundSize.width) self._scrollView.delegate = self self._scrollView.minimumZoomScale = 0.1 self._scrollView.maximumZoomScale = 8 self._scrollView.scrollEnabled = true self._scrollView.showsHorizontalScrollIndicator = true self._scrollView.showsVerticalScrollIndicator = true let doubleTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self , action:#selector(self.doubleTap(_:))) doubleTapGesture.numberOfTapsRequired = 2 self._imageView.userInteractionEnabled = true self._imageView.addGestureRecognizer(doubleTapGesture) self._scrollView.setZoomScale(_zoomScaleWidth, animated: false) updateScrollInset() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } // ピンチイン・ピンチアウト func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? { return self._imageView } // ダブルタップ func doubleTap(gesture: UITapGestureRecognizer) -> Void { if ( self._scrollView.zoomScale < self._scrollView.maximumZoomScale ) { let newScale:CGFloat = self._scrollView.zoomScale * 3.7 let zoomRect:CGRect = self.zoomRectForScale(newScale, center: gesture.locationInView(gesture.view)) self._scrollView.zoomToRect(zoomRect, animated: true) } else { self._scrollView.setZoomScale(_zoomScaleWidth, animated: true) } } // 領域 func zoomRectForScale(scale:CGFloat, center: CGPoint) -> CGRect{ var zoomRect: CGRect = CGRect() zoomRect.size.height = self._scrollView.frame.size.height / scale zoomRect.size.width = self._scrollView.frame.size.width / scale zoomRect.origin.x = center.x - zoomRect.size.width / 2.0 zoomRect.origin.y = center.y - zoomRect.size.height / 2.0 return zoomRect } func scrollViewDidZoom(scrollView: UIScrollView) { updateScrollInset() } private func updateScrollInset() { // imageViewの大きさからcontentInsetを再計算 // なお、0を下回らないようにする _scrollView.contentInset = UIEdgeInsetsMake( max((_scrollView.frame.height - _imageView.frame.height)/2, 0), max((_scrollView.frame.width - _imageView.frame.width)/2, 0), 0, 0 ); } }