Skip to content

Commit

Permalink
fix imageView alpha animation and add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
horitaku46 committed Dec 19, 2017
1 parent e7a1bb0 commit 4b6ff3e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 29 deletions.
10 changes: 7 additions & 3 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import UIKit

final class ViewController: UIViewController {

@IBOutlet weak var collectionView: UICollectionView! {
@IBOutlet weak private var collectionView: UICollectionView! {
didSet {
collectionView.register(UINib(nibName: "ImageCell", bundle: nil), forCellWithReuseIdentifier: "ImageCell")
collectionView.delegate = self
collectionView.dataSource = self
}
}

@IBOutlet weak var flowLayout: UICollectionViewFlowLayout! {
@IBOutlet weak private var flowLayout: UICollectionViewFlowLayout! {
didSet {
flowLayout.scrollDirection = .vertical
flowLayout.minimumLineSpacing = 1
Expand Down Expand Up @@ -51,10 +51,14 @@ extension ViewController: UICollectionViewDelegate {
return
}

let slideLeafs: [SlideLeaf] = images.enumerated().map { SlideLeaf(image: $0.1, title: "Image Title \($0.0)", caption: "Index is \($0.0)") }
let slideLeafs: [SlideLeaf] = images.enumerated().map { SlideLeaf(image: $0.1,
title: "Image Title \($0.0)",
caption: "Index is \($0.0)") }

let slideImageViewController = SlideLeafViewController.make(leafs: slideLeafs,
startIndex: indexPath.row,
fromImageView: selectedCell.imageView)

slideImageViewController.delegate = self
present(slideImageViewController, animated: true, completion: nil)
}
Expand Down
11 changes: 6 additions & 5 deletions Serrata.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "Serrata"
s.version = "1.0.1"
s.summary = "Slide image viewer library similar to twitter and LINE"
s.version = "1.0.2"
s.summary = "Slide image viewer library similar to twitter and LINE."

s.description = <<-DESC
You can use it simply by passing the necessary information.
Expand All @@ -19,13 +19,14 @@ Pod::Spec.new do |s|
s.author = { "Takuma Horiuchi" => "horitaku46@gmail.com" }
s.social_media_url = "https://twitter.com/horitaku_"

s.platform = :ios, "10.0"
s.source = { :git => "https://github.com/horitaku46/Serrata.git", :tag => "#{s.version}" }

s.ios.deployment_target = '10.0'

s.source = { :git => "https://github.com/horitaku46/Serrata.git", :tag => "#{s.version}" }

s.source_files = "Serrata/Sources/*.{swift}"
s.resources = "Serrata/**/*.{storyboard,xib,xcassets}"

s.requires_arc = true

s.dependency "Kingfisher"
end
23 changes: 18 additions & 5 deletions Serrata/Sources/SlideLeaf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,33 @@

import UIKit

open class SlideLeaf: NSObject {
public final class SlideLeaf: NSObject {

open var image: UIImage?
open var urlString: String?
public var image: UIImage?
public var urlString: String?

open var title: String
open var caption: String
public var title: String
public var caption: String


/// If either title and caption is empty, detailView is not displayed.
///
/// - Parameters:
/// - image: To read by a slide.
/// - title: Title of the image.
/// - caption: Caption of the image.
public init(image: UIImage?, title: String = "", caption: String = "") {
self.image = image
self.title = title
self.caption = caption
}

/// If either title and caption is empty, detailView is not displayed.
///
/// - Parameters:
/// - urlString: To read by a slide. It is displayed by Kingfisher.
/// - title: Title of the image.
/// - caption: Caption of the image.
public init(urlString: String?, title: String = "", caption: String = "") {
self.urlString = urlString;
self.title = title
Expand Down
27 changes: 18 additions & 9 deletions Serrata/Sources/SlideLeafCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import UIKit
import Kingfisher

public protocol SlideLeafCellDelegate: class {
func slideLeafScrollViewWillBeginDragging(_ scrollView: UIScrollView)
func slideLeafScrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?)
func slideLeafScrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat)
func longPressImageView()
}

Expand Down Expand Up @@ -60,7 +61,7 @@ open class SlideLeafCell: UICollectionViewCell {

open func configure(slideLeaf: SlideLeaf) {
if let image = slideLeaf.image {
setImage(image)
setImage(image, animated: false)

} else if let url = slideLeaf.urlString {
activityIndicatorView.startAnimating()
Expand All @@ -70,7 +71,7 @@ open class SlideLeafCell: UICollectionViewCell {
guard let me = self, let image = image else { return }
me.activityIndicatorView.isHidden = true
me.activityIndicatorView.stopAnimating()
me.setImage(image)
me.setImage(image, animated: true)
}
}
}
Expand All @@ -89,13 +90,17 @@ open class SlideLeafCell: UICollectionViewCell {
scrollView.setZoomScale(1, animated: false)
}

private func setImage(_ image: UIImage) {
private func setImage(_ image: UIImage, animated: Bool) {
imageView.image = image
calcImageViewFrame(image)
scrollView.addSubview(imageView)

UIView.animate(withDuration: 0.2) {
self.imageView.alpha = 1

if animated {
UIView.animate(withDuration: 0.2) {
self.imageView.alpha = 1
}
} else {
imageView.alpha = 1
}
}

Expand Down Expand Up @@ -154,7 +159,11 @@ extension SlideLeafCell: UIScrollViewDelegate {
updateImageViewToCenter()
}

open func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
delegate?.slideLeafScrollViewWillBeginDragging(scrollView)
open func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
delegate?.slideLeafScrollViewWillBeginZooming(scrollView, with: view)
}

open func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
delegate?.slideLeafScrollViewDidEndZooming(scrollView, with: view, atScale: scale)
}
}
28 changes: 21 additions & 7 deletions Serrata/Sources/SlideLeafViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ fileprivate enum SlideLeafConst {

@objc public protocol SlideLeafViewControllerDelegate: class {
func tapImageDetailView(slideLeaf: SlideLeaf, pageIndex: Int)
@objc func longPressImageView(slideLeafViewController: SlideLeafViewController, slideLeaf: SlideLeaf, pageIndex: Int)
@objc func slideLeafViewControllerDismissed(slideLeaf: SlideLeaf, pageIndex: Int)
@objc optional func longPressImageView(slideLeafViewController: SlideLeafViewController, slideLeaf: SlideLeaf, pageIndex: Int)
@objc optional func slideLeafViewControllerDismissed(slideLeaf: SlideLeaf, pageIndex: Int)
}

open class SlideLeafViewController: UIViewController {
open class SlideLeafViewController: UIViewController {

open override var prefersStatusBarHidden: Bool {
return true
Expand Down Expand Up @@ -112,6 +112,14 @@ fileprivate enum SlideLeafConst {
private var selectedCell = SlideLeafCell()
private var isDecideDissmiss = false


/// This method generates SlideLeafViewController.
///
/// - Parameters:
/// - leafs: It is array to display it by a slide.
/// - startIndex: It is for initial indication based on array of leafs.
/// - fromImageView: ImageView of the origin of transition. In the case of nil, CrossDissolve.
/// - Returns: Instance of SlideLeafViewController.
open class func make(leafs: [SlideLeaf], startIndex: Int = 0, fromImageView: UIImageView? = nil) -> SlideLeafViewController {
let viewController = UIStoryboard(name: "SlideLeafViewController", bundle: Bundle(for: SlideLeafViewController.self))
.instantiateViewController(withIdentifier: "SlideLeafViewController") as! SlideLeafViewController
Expand Down Expand Up @@ -185,7 +193,7 @@ fileprivate enum SlideLeafConst {
dismiss(animated: true) {
if self.isDecideDissmiss {
let leaf = self.slideLeafs[self.pageIndex]
self.delegate?.slideLeafViewControllerDismissed(slideLeaf: leaf, pageIndex: self.pageIndex)
self.delegate?.slideLeafViewControllerDismissed!(slideLeaf: leaf, pageIndex: self.pageIndex)
}
}
}
Expand Down Expand Up @@ -271,13 +279,19 @@ extension SlideLeafViewController: UIScrollViewDelegate {

extension SlideLeafViewController: SlideLeafCellDelegate {

open func slideLeafScrollViewWillBeginDragging(_ scrollView: UIScrollView) {
open func slideLeafScrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
imageDetailView.fadeOut()
}

public func slideLeafScrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
if scale == 1 {
imageDetailView.fadeIn()
}
}

open func longPressImageView() {
let leaf = slideLeafs[pageIndex]
delegate?.longPressImageView(slideLeafViewController: self, slideLeaf: leaf, pageIndex: pageIndex)
delegate?.longPressImageView!(slideLeafViewController: self, slideLeaf: leaf, pageIndex: pageIndex)
}
}

Expand All @@ -286,7 +300,7 @@ extension SlideLeafViewController: ImageDetailViewDelegate {
open func tapCloseButton() {
dismiss(animated: true) {
let leaf = self.slideLeafs[self.pageIndex]
self.delegate?.slideLeafViewControllerDismissed(slideLeaf: leaf, pageIndex: self.pageIndex)
self.delegate?.slideLeafViewControllerDismissed!(slideLeaf: leaf, pageIndex: self.pageIndex)
}
}

Expand Down

0 comments on commit 4b6ff3e

Please sign in to comment.