Skip to content

Commit

Permalink
Fixed a crash caused by KVO observer handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Angyal committed Jun 9, 2016
1 parent 19ac960 commit 4b81a3b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
16 changes: 8 additions & 8 deletions Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@
<action selector="showGalleryImageViewer:" destination="BYZ-38-t0r" eventType="touchUpInside" id="a7r-FT-uUm"/>
</connections>
</button>
<button opaque="NO" tag="5" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" adjustsImageWhenHighlighted="NO" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Fd6-Vz-POJ">
<rect key="frame" x="56" y="447" width="201" height="83"/>
<state key="normal" backgroundImage="5"/>
<connections>
<action selector="showGalleryImageViewer:" destination="BYZ-38-t0r" eventType="touchUpInside" id="1YS-ou-fMt"/>
</connections>
</button>
<button opaque="NO" tag="6" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" adjustsImageWhenHighlighted="NO" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Bne-tn-gLP">
<rect key="frame" x="189" y="393" width="68" height="50"/>
<state key="normal" backgroundImage="6"/>
Expand Down Expand Up @@ -85,13 +78,20 @@
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="WRv-58-cYl">
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" adjustsImageWhenHighlighted="NO" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="WRv-58-cYl">
<rect key="frame" x="20" y="51" width="86" height="48"/>
<state key="normal" backgroundImage="image_small"/>
<connections>
<action selector="showSingleImageViewer:" destination="BYZ-38-t0r" eventType="touchUpInside" id="kdW-lW-YeZ"/>
</connections>
</button>
<button opaque="NO" tag="5" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" adjustsImageWhenHighlighted="NO" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Fd6-Vz-POJ">
<rect key="frame" x="35" y="445" width="201" height="83"/>
<state key="normal" backgroundImage="5"/>
<connections>
<action selector="showGalleryImageViewer:" destination="BYZ-38-t0r" eventType="touchUpInside" id="1YS-ou-fMt"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
Expand Down
61 changes: 28 additions & 33 deletions ImageViewer/Source/ImageViewer/ImageViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ import AVFoundation
public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewControllerTransitioningDelegate {

/// UI
private var scrollView: UIScrollView!
private var overlayView: UIView!
private var closeButton: UIButton!
private var scrollView = UIScrollView()
private var overlayView = UIView()
private var closeButton = UIButton()
private var imageView = UIImageView()

private let displacedView: UIView
private var applicationWindow: UIWindow? {
return UIApplication.sharedApplication().delegate?.window?.flatMap { $0 }
Expand All @@ -71,7 +70,7 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
private let hideCloseButtonDuration = 0.05
private let zoomDuration = 0.2
private let thresholdVelocity: CGFloat = 1000 // Based on UX experiments
private let cutOffVelocity: CGFloat = 1000000 // we need some sufficiently large number, nobody can swipe faster then that
private let cutOffVelocity: CGFloat = 1000000 // we simply need some sufficiently large number, nobody can swipe faster than that
/// TRANSITIONS
private let presentTransition: ImageViewerPresentTransition
private let dismissTransition: ImageViewerDismissTransition
Expand Down Expand Up @@ -101,6 +100,7 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
// MARK: - Deinit

deinit {

scrollView.removeObserver(self, forKeyPath: "contentOffset")
}

Expand All @@ -121,6 +121,11 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
transitioningDelegate = self
modalPresentationStyle = .Custom
extendedLayoutIncludesOpaqueBars = true

overlayView.autoresizingMask = [.None]
configureCloseButton()
configureImageView()
configureScrollView()
}

public required init?(coder aDecoder: NSCoder) {
Expand All @@ -136,6 +141,7 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
closeButton.setImage(closeButtonAssets.normal, forState: UIControlState.Normal)
closeButton.setImage(closeButtonAssets.highlighted, forState: UIControlState.Highlighted)
closeButton.alpha = 0.0
closeButton.addTarget(self, action: #selector(ImageViewer.close(_:)), forControlEvents: .TouchUpInside)
}

private func configureGestureRecognizers() {
Expand All @@ -154,22 +160,37 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo

imageView.frame = parentViewFrameInOurCoordinateSystem
imageView.contentMode = .ScaleAspectFit
view.addSubview(imageView)
imageView.image = screenshotFromView(displacedView)
}

private func configureScrollView() {

scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
scrollView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
scrollView.decelerationRate = 0.5
scrollView.contentInset = UIEdgeInsetsZero
scrollView.contentOffset = CGPointZero
scrollView.contentSize = imageView.frame.size
scrollView.minimumZoomScale = 1
scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
scrollView.delegate = self
}

func createViewHierarchy() {

view.addSubview(overlayView)
view.addSubview(imageView)
view.addSubview(scrollView)
view.addSubview(closeButton)
}

// MARK: - View Lifecycle

public override func viewDidLoad() {
super.viewDidLoad()

createViewHierarchy()
}

public override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
shouldRotate = true
Expand All @@ -196,32 +217,6 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
}
}

public override func loadView() {
super.loadView()

scrollView = UIScrollView(frame: CGRectZero)
overlayView = UIView(frame: CGRectZero)
closeButton = UIButton(frame: CGRectZero)

scrollView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
overlayView.autoresizingMask = [.None]

view.addSubview(overlayView)
view.addSubview(scrollView)
view.addSubview(closeButton)

scrollView.delegate = self
closeButton.addTarget(self, action: #selector(ImageViewer.close(_:)), forControlEvents: .TouchUpInside)
}

public override func viewDidLoad() {
super.viewDidLoad()

configureCloseButton()
configureImageView()
configureScrollView()
}

// MARK: - Transitioning Delegate

public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
Expand Down

0 comments on commit 4b81a3b

Please sign in to comment.