Skip to content

Commit

Permalink
fix bug for zoom position
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyuexit committed Sep 15, 2017
1 parent 245e4f6 commit cc47d34
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions LPAlbum/Others/AlbumManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class AlbumManager {
}

// 根据Asset获取photo
@discardableResult
public class func getPhoto(asset: PHAsset, targetSize: CGSize, option: PHImageRequestOptions? = nil, resultHandler: @escaping ((UIImage?, [AnyHashable: Any]?) -> Void)) -> PHImageRequestID {

let defalutOption = PHImageRequestOptions()
Expand Down
46 changes: 37 additions & 9 deletions LPAlbum/Views/PhotoPreviewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class PhotoPreviewCell: UICollectionViewCell {

var assetModel: AssetModel! {
didSet{

AlbumManager.getPhoto(asset: assetModel.asset, targetSize: bounds.size) { (image, _) in
self.photoView.image = image
let size = self.scrollView.frame.size
let newSize = (image?.size ?? size).constrainedSize(toSize: size)
self.photoView.bounds = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
}
}
}
Expand All @@ -33,14 +35,40 @@ class PhotoPreviewCell: UICollectionViewCell {
scrollView.delaysContentTouches = true
scrollView.canCancelContentTouches = true
scrollView.delegate = self
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false

photoView.contentMode = .scaleAspectFit
photoView.frame = scrollView.bounds
scrollView.addSubview(photoView)
contentView.addSubview(scrollView)

let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapAction))
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTapAction))
doubleTap.numberOfTapsRequired = 2
singleTap.require(toFail: doubleTap)
contentView.addGestureRecognizer(singleTap)
contentView.addGestureRecognizer(doubleTap)

}

func singleTapAction() {

}

func doubleTapAction(ges: UITapGestureRecognizer) {
if scrollView.zoomScale > 1.0 {
scrollView.contentInset = .zero
scrollView.setZoomScale(1.0, animated: true)
}else{
let point = ges.location(in: photoView)
let zoonScale = scrollView.maximumZoomScale
let xSize = bounds.size.width / zoonScale
let ySize = bounds.size.height / zoonScale
scrollView.zoom(to: CGRect(x: point.x - xSize/2, y: point.y - ySize/2, width: xSize, height: ySize), animated: true)
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand All @@ -56,14 +84,14 @@ extension PhotoPreviewCell: UIScrollViewDelegate {
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
// let scrollViewW = scrollView.frame.width
// let scrollViewH = scrollView.frame.height
// let contentSizeW = scrollView.contentSize.width
// let contentSizeH = scrollView.contentSize.height
//
// let offsetX = scrollViewW > contentSizeW ? scrollViewW - contentSizeW * 0.5 : 0
// let offsetY = scrollViewH > contentSizeH ? scrollViewH - contentSizeH * 0.5 : 0
// photoView.center = CGPoint(x: contentSizeW * 0.5 + offsetX, y: contentSizeH * 0.5 + offsetY)
let scrollViewW = scrollView.frame.width
let scrollViewH = scrollView.frame.height
let contentSizeW = scrollView.contentSize.width
let contentSizeH = scrollView.contentSize.height
// 缩小
let offsetX = scrollViewW > contentSizeW ? (scrollViewW - contentSizeW) * 0.5 : 0
let offsetY = scrollViewH > contentSizeH ? (scrollViewH - contentSizeH) * 0.5 : 0
photoView.center = CGPoint(x: contentSizeW * 0.5 + offsetX, y: contentSizeH * 0.5 + offsetY)
}

}
Expand Down

0 comments on commit cc47d34

Please sign in to comment.