Skip to content

Commit

Permalink
Merge pull request #120 from nakajijapan/202107-refactoring
Browse files Browse the repository at this point in the history
Some fixes
  • Loading branch information
nakajijapan committed Jul 28, 2021
2 parents e0701b7 + c0ac4ab commit 7db2d5d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 111 deletions.
57 changes: 16 additions & 41 deletions PhotoSliderDemo/ViewController.swift
Expand Up @@ -74,9 +74,8 @@ class ViewController: UIViewController {
}

var width = view.bounds.width
if #available(iOS 11.0, *) {
width = view.bounds.width - view.safeAreaInsets.left - view.safeAreaInsets.right
}
width = view.bounds.width - view.safeAreaInsets.left - view.safeAreaInsets.right

collectionView.contentOffset = CGPoint(x: CGFloat(currentRow) * width, y: 0.0)
}

Expand Down Expand Up @@ -153,18 +152,10 @@ extension ViewController: UICollectionViewDelegateFlowLayout {

} else {
let height: CGFloat
if #available(iOS 11.0, *) {
height = view.safeAreaLayoutGuide.layoutFrame.height
} else {
height = view.bounds.height
}

height = view.safeAreaLayoutGuide.layoutFrame.height
return CGSize(width: tableView.bounds.width, height: height)

}

}

}

// MARK: - UITableViewDataSource
Expand Down Expand Up @@ -234,14 +225,13 @@ extension ViewController: ZoomingAnimationControllerTransitioning {
let imageView = UIImageView(image: cell.imageView.image)

var frame = cell.imageView.frame
frame.origin.y += UIApplication.shared.statusBarFrame.height
frame.origin.y += view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
// tune in UIImageView
if #available(iOS 11.0, *) {
frame.origin.x = view.safeAreaInsets.left
if view.bounds.width > view.bounds.height {
frame.size.height = view.safeAreaLayoutGuide.layoutFrame.height
}
frame.origin.x = view.safeAreaInsets.left
if view.bounds.width > view.bounds.height {
frame.size.height = view.safeAreaLayoutGuide.layoutFrame.height
}

imageView.frame = frame
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFill
Expand All @@ -258,48 +248,33 @@ extension ViewController: ZoomingAnimationControllerTransitioning {
guard let cell = collectionView.visibleCells.first as? ImageCollectionViewCell else {
return
}
let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height
let statusBarHeight: CGFloat = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
let topPosition: CGFloat = view.safeAreaLayoutGuide.layoutFrame.origin.y
var frame = CGRect.zero

if view.bounds.size.width < view.bounds.height {

if image.size.height < image.size.width {
let width = (sourceImageView.image!.size.width * sourceImageView.bounds.size.width) / sourceImageView.image!.size.height
let x = (width - cell.imageView.bounds.height) * 0.5
frame = CGRect(x: -1.0 * x, y: statusBarHeight, width: width, height: cell.imageView.bounds.height)
frame = CGRect(x: -1.0 * x, y: topPosition, width: width, height: cell.imageView.bounds.height)
} else {
frame = CGRect(x: 0.0, y: statusBarHeight, width: view.bounds.width, height: view.bounds.width)
frame = CGRect(x: 0.0, y: topPosition, width: view.bounds.width, height: view.bounds.width)
}

} else {
let width: CGFloat
let x: CGFloat
if #available(iOS 11.0, *) {
width = view.bounds.width - view.safeAreaInsets.left - view.safeAreaInsets.right
x = view.safeAreaInsets.left
} else {
width = view.bounds.width
x = 0
}
width = view.bounds.width - view.safeAreaInsets.left - view.safeAreaInsets.right
x = view.safeAreaInsets.left

let height: CGFloat
if #available(iOS 11.0, *) {
height = view.safeAreaLayoutGuide.layoutFrame.height
} else {
height = (image.size.height * width) / image.size.width
}
height = view.safeAreaLayoutGuide.layoutFrame.height

let y: CGFloat
if #available(iOS 11.0, *) {
y = (height - view.safeAreaLayoutGuide.layoutFrame.height - statusBarHeight) * 0.5
} else {
y = (height - UIScreen.main.bounds.height - statusBarHeight) * 0.5
}

y = (height - view.safeAreaLayoutGuide.layoutFrame.height - statusBarHeight) * 0.5
frame = CGRect(x: x, y: -1.0 * y, width: width, height: height)
}
sourceImageView.frame = frame

}
}

Expand Down
7 changes: 3 additions & 4 deletions Sources/PhotoSlider/Classes/ImageView.swift
Expand Up @@ -44,9 +44,8 @@ class ImageView: UIView {
scrollView.maximumZoomScale = 3.0
scrollView.delegate = self
scrollView.alwaysBounceVertical = true
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
}
scrollView.contentInsetAdjustmentBehavior = .never

// image
imageView = UIImageView(frame: CGRect.zero)
imageView.contentMode = .scaleAspectFit
Expand Down Expand Up @@ -126,7 +125,7 @@ class ImageView: UIView {
progressView.heightAnchor.constraint(equalToConstant: 40.0),
progressView.widthAnchor.constraint(equalToConstant: 40.0),
progressView.centerXAnchor.constraint(lessThanOrEqualTo: centerXAnchor, constant: 1.0),
progressView.centerYAnchor.constraint(lessThanOrEqualTo: centerYAnchor, constant: 1.0),
progressView.centerYAnchor.constraint(lessThanOrEqualTo: centerYAnchor, constant: 1.0)
].forEach { $0.isActive = true }
}

Expand Down
90 changes: 24 additions & 66 deletions Sources/PhotoSlider/Classes/ViewController.swift
Expand Up @@ -41,10 +41,7 @@ public class ViewController: UIViewController {
height: self.view.bounds.height * 3.0
)

if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
}

scrollView.contentInsetAdjustmentBehavior = .never
return scrollView
}()

Expand All @@ -64,7 +61,7 @@ public class ViewController: UIViewController {
if let captionHeight = captionHeight {
var y = view.bounds.height - captionHeight - 32.0
var height = captionHeight + 32.0
if #available(iOS 11.0, *), let window = UIApplication.shared.keyWindow {
if let window = UIApplication.shared.keyWindow {
let bottomSafeInsetSpacing = window.safeAreaInsets.bottom
height += bottomSafeInsetSpacing
y -= bottomSafeInsetSpacing
Expand Down Expand Up @@ -309,61 +306,32 @@ fileprivate extension ViewController {

func layoutCloseButton() {
closeButton.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
[
closeButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0),
closeButton.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0.0),
closeButton.heightAnchor.constraint(equalToConstant: 52.0),
closeButton.widthAnchor.constraint(equalToConstant: 52.0),
].forEach { $0.isActive = true }
} else {
[
closeButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0),
closeButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0.0),
closeButton.heightAnchor.constraint(equalToConstant: 52.0),
closeButton.widthAnchor.constraint(equalToConstant: 52.0),
].forEach { $0.isActive = true }

}
[
closeButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0),
closeButton.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0.0),
closeButton.heightAnchor.constraint(equalToConstant: 52.0),
closeButton.widthAnchor.constraint(equalToConstant: 52.0)
].forEach { $0.isActive = true }
}

func layoutShareButton() {
shareButton.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
[
shareButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0),
shareButton.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0.0),
shareButton.heightAnchor.constraint(equalToConstant: 52.0),
shareButton.widthAnchor.constraint(equalToConstant: 52.0),
].forEach { $0.isActive = true }
} else {
[
shareButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0),
shareButton.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0.0),
shareButton.heightAnchor.constraint(equalToConstant: 52.0),
shareButton.widthAnchor.constraint(equalToConstant: 52.0),
].forEach { $0.isActive = true }

}
[
shareButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0),
shareButton.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0.0),
shareButton.heightAnchor.constraint(equalToConstant: 52.0),
shareButton.widthAnchor.constraint(equalToConstant: 52.0)
].forEach { $0.isActive = true }
}

func layoutPageControl() {
pageControl.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
[
pageControl.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0.0),
pageControl.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
pageControl.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0.0),
pageControl.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0.0),
].forEach { $0.isActive = true }
} else {
[
pageControl.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0),
pageControl.centerXAnchor.constraint(equalTo: view.centerXAnchor),
pageControl.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0.0),
pageControl.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0.0),
].forEach { $0.isActive = true }
}
[
pageControl.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0.0),
pageControl.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
pageControl.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0.0),
pageControl.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0.0)
].forEach { $0.isActive = true }
}

func layoutCaptionLabel() {
Expand All @@ -379,19 +347,10 @@ fileprivate extension ViewController {
}

captionLabel.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
[
captionLabel.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -32.0),
captionLabel.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: -16.0),
captionLabel.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 16.0),
].forEach { $0.isActive = true }
} else {
[
captionLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -32.0),
captionLabel.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -16.0),
captionLabel.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16.0),
].forEach { $0.isActive = true }
}
[ captionLabel.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -32.0),
captionLabel.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: -16.0),
captionLabel.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 16.0)
].forEach { $0.isActive = true }

if let captionHeight = captionHeight {
captionLabel.heightAnchor.constraint(equalToConstant: captionHeight).isActive = true
Expand All @@ -406,7 +365,6 @@ fileprivate extension ViewController {
captionBackgroundView.topAnchor.constraint(equalTo: captionLabel.topAnchor, constant: -16.0)
].forEach { $0.isActive = true }
}

}

// MARK: - UIScrollViewDelegate
Expand Down

0 comments on commit 7db2d5d

Please sign in to comment.