Skip to content

Add save way to access superview in dismissal transition #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ private extension UIView.ContentMode {
return "bottomLeft"
case .bottomRight:
return "bottomRight"
@unknown default:
return ""
}
}
}
2 changes: 2 additions & 0 deletions ImageViewer/AnimatableImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private extension AnimatableImageView {
case .bottomRight:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.bottomRight(forSize: image.size, insideSize: bounds.size)
@unknown default:
return
}
}
}
2 changes: 2 additions & 0 deletions ImageViewer/ImageViewerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import UIKit

public typealias ImageCompletion = (UIImage?) -> Void
public typealias ImageBlock = (@escaping ImageCompletion) -> Void
public typealias ScrollViewBlock = (UIScrollView) -> Void

public final class ImageViewerConfiguration {
public var image: UIImage?
public var imageView: UIImageView?
public var imageBlock: ImageBlock?
public var setupScrollViewBlock: ScrollViewBlock?

public typealias ConfigurationClosure = (ImageViewerConfiguration) -> ()

Expand Down
21 changes: 16 additions & 5 deletions ImageViewer/ImageViewerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import UIKit
import AVFoundation

public final class ImageViewerController: UIViewController {
@IBOutlet fileprivate var scrollView: UIScrollView!
@IBOutlet fileprivate var imageView: UIImageView!
@IBOutlet fileprivate var activityIndicator: UIActivityIndicatorView!

@IBOutlet fileprivate weak var scrollView: UIScrollView!
@IBOutlet fileprivate weak var imageView: UIImageView!
@IBOutlet fileprivate weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet fileprivate weak var shareButton: UIButton!

fileprivate var transitionHandler: ImageViewerTransitioningHandler?
fileprivate let configuration: ImageViewerConfiguration?

Expand All @@ -28,7 +29,7 @@ public final class ImageViewerController: UIViewController {

override public func viewDidLoad() {
super.viewDidLoad()
imageView.image = configuration?.imageView?.image ?? configuration?.image
imageView.image = configuration?.image ?? configuration?.imageView?.image

setupScrollView()
setupGestureRecognizers()
Expand Down Expand Up @@ -62,6 +63,8 @@ private extension ImageViewerController {
scrollView.decelerationRate = UIScrollView.DecelerationRate.fast
scrollView.alwaysBounceVertical = true
scrollView.alwaysBounceHorizontal = true

configuration?.setupScrollViewBlock?(scrollView)
}

func setupGestureRecognizers() {
Expand Down Expand Up @@ -97,6 +100,14 @@ private extension ImageViewerController {
@IBAction func closeButtonPressed() {
dismiss(animated: true)
}

@IBAction func share() {
guard let image = imageView.image else { return }
let items = [image]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
ac.popoverPresentationController?.sourceView = shareButton
present(ac, animated: true)
}

@objc func imageViewDoubleTapped(recognizer: UITapGestureRecognizer) {
func zoomRectForScale(scale: CGFloat, center: CGPoint) -> CGRect {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "Share.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Share@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "Share@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 21 additions & 7 deletions ImageViewer/Resources/ImageViewerController.xib
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14868" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14824"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -15,6 +12,7 @@
<outlet property="activityIndicator" destination="oPl-2f-Knx" id="GOD-eG-o96"/>
<outlet property="imageView" destination="rHA-5m-kkS" id="V86-k1-pPH"/>
<outlet property="scrollView" destination="cQD-2Q-pvo" id="M6k-QU-OVY"/>
<outlet property="shareButton" destination="IIq-8X-ob9" id="4IX-cf-QnD"/>
<outlet property="view" destination="iN0-l3-epB" id="bEt-1B-b5l"/>
</connections>
</placeholder>
Expand Down Expand Up @@ -45,7 +43,7 @@
<rect key="frame" x="169" y="315" width="37" height="37"/>
</activityIndicatorView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Wsy-fe-raz">
<rect key="frame" x="0.0" y="20" width="44" height="44"/>
<rect key="frame" x="0.0" y="0.0" width="44" height="44"/>
<constraints>
<constraint firstAttribute="height" constant="44" id="bXU-O2-lam"/>
<constraint firstAttribute="width" constant="44" id="xTg-TJ-eL3"/>
Expand All @@ -57,24 +55,40 @@
<action selector="closeButtonPressed" destination="-1" eventType="touchUpInside" id="jwg-86-OJF"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" adjustsImageSizeForAccessibilityContentSizeCategory="YES" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="IIq-8X-ob9">
<rect key="frame" x="323" y="0.0" width="44" height="44"/>
<constraints>
<constraint firstAttribute="width" constant="44" id="A45-B7-xiX"/>
<constraint firstAttribute="height" constant="44" id="Cc5-Wh-nUa"/>
</constraints>
<state key="normal" image="Share">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="share" destination="-1" eventType="touchUpInside" id="AOU-RO-sAc"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="oPl-2f-Knx" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" id="4hr-Ua-OMa"/>
<constraint firstItem="cQD-2Q-pvo" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" id="ErM-q5-uey"/>
<constraint firstItem="Wsy-fe-raz" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" id="GNz-6j-kdf"/>
<constraint firstItem="IIq-8X-ob9" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="topMargin" id="Rc4-N7-QgR"/>
<constraint firstItem="Wsy-fe-raz" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="topMargin" id="Tze-CU-2Ab"/>
<constraint firstItem="rHA-5m-kkS" firstAttribute="width" secondItem="iN0-l3-epB" secondAttribute="width" id="Zh5-4A-UbJ"/>
<constraint firstAttribute="trailing" secondItem="cQD-2Q-pvo" secondAttribute="trailing" id="fiR-iH-AGe"/>
<constraint firstAttribute="bottom" secondItem="cQD-2Q-pvo" secondAttribute="bottom" id="kQk-Ie-iGl"/>
<constraint firstItem="oPl-2f-Knx" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="lBh-77-nAV"/>
<constraint firstItem="cQD-2Q-pvo" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" id="lTy-Fb-3fn"/>
<constraint firstAttribute="trailing" secondItem="IIq-8X-ob9" secondAttribute="trailing" constant="8" id="qJ5-62-oJN"/>
<constraint firstItem="rHA-5m-kkS" firstAttribute="height" secondItem="iN0-l3-epB" secondAttribute="height" id="tGF-Ia-4A0"/>
</constraints>
<point key="canvasLocation" x="33.5" y="54.5"/>
</view>
</objects>
<resources>
<image name="Share" width="30" height="30"/>
<image name="closeButton" width="13" height="13"/>
</resources>
</document>
4 changes: 3 additions & 1 deletion ImageViewer/Transitions/ImageViewerDismissalTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ private extension ImageViewerDismissalTransition {
case .end:
self.animatableImageview.contentMode = self.toImageView.contentMode
self.animatableImageview.transform = .identity
self.animatableImageview.frame = self.toImageView.superview!.convert(self.toImageView.frame, to: nil)
if let superview = self.toImageView.superview {
self.animatableImageview.frame = superview.convert(self.toImageView.frame, to: nil)
}
self.fadeView.alpha = 0.0
}
}
Expand Down
2 changes: 1 addition & 1 deletion SimpleImageViewer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Pod::Spec.new do |s|
s.name = "SimpleImageViewer"
s.summary = "A snappy image viewer with zoom and interactive dismissal transition."
s.requires_arc = true
s.version = "1.1.1"
s.version = "1.2.1"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Lucas" => "lucas@afrogleap.com" }
s.homepage = "https://github.com/aFrogleap/SimpleImageViewer"
Expand Down
61 changes: 37 additions & 24 deletions SimpleImageViewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -281,30 +281,31 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 1020;
ORGANIZATIONNAME = aFrogleap;
TargetAttributes = {
A02EE5D31EF1897D000EAB41 = {
CreatedOnToolsVersion = 8.3.2;
DevelopmentTeam = WQEYNHG5C2;
DevelopmentTeam = DZYTXHVP4K;
ProvisioningStyle = Automatic;
};
A07E76D21ECC94A400B77D46 = {
CreatedOnToolsVersion = 8.3.2;
DevelopmentTeam = 44MAY72KU6;
LastSwiftMigration = 0900;
ProvisioningStyle = Manual;
DevelopmentTeam = DZYTXHVP4K;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
A0BF70A61EDC5B1400109F6E = {
CreatedOnToolsVersion = 8.3.2;
LastSwiftMigration = 1000;
ProvisioningStyle = Manual;
DevelopmentTeam = DZYTXHVP4K;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = A07E76CE1ECC94A400B77D46 /* Build configuration list for PBXProject "SimpleImageViewer" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Expand Down Expand Up @@ -412,7 +413,7 @@
A02EE5DD1EF1897D000EAB41 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = WQEYNHG5C2;
DEVELOPMENT_TEAM = DZYTXHVP4K;
INFOPLIST_FILE = SimpleImageViewerTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.afrogleap.SimpleImageViewerTests;
Expand All @@ -424,7 +425,7 @@
A02EE5DE1EF1897D000EAB41 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = WQEYNHG5C2;
DEVELOPMENT_TEAM = DZYTXHVP4K;
INFOPLIST_FILE = SimpleImageViewerTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.afrogleap.SimpleImageViewerTests;
Expand All @@ -437,6 +438,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
Expand All @@ -447,13 +449,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -494,6 +498,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
Expand All @@ -504,13 +509,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -545,17 +552,19 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = 44MAY72KU6;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = DZYTXHVP4K;
INFOPLIST_FILE = "$(SRCROOT)/ImageViewer/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.afrogleapbv.inhouse.swift.imageviewer;
PRODUCT_BUNDLE_IDENTIFIER = de.alexsteiner.imageviewer;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "d5b8f7cb-0120-427a-9ff1-14d28ee0046f";
PROVISIONING_PROFILE_SPECIFIER = "aFrogleap Wildcard Development";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -564,25 +573,28 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = DZYTXHVP4K;
INFOPLIST_FILE = "$(SRCROOT)/ImageViewer/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.afrogleapbv.inhouse.swift.imageviewer;
PRODUCT_BUNDLE_IDENTIFIER = de.alexsteiner.imageviewer;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Release;
};
A0BF70B11EDC5B1400109F6E /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = DZYTXHVP4K;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -594,7 +606,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -604,9 +616,10 @@
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = DZYTXHVP4K;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -618,7 +631,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down
Loading