Skip to content

Commit

Permalink
migrate swift5
Browse files Browse the repository at this point in the history
  • Loading branch information
NguyenHieu committed Oct 27, 2020
1 parent 9f32177 commit 8ed6eac
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Classes/ExtensionGestureRecognizerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import UIKit

extension HImageView: UIGestureRecognizerDelegate {
extension HNImageView: UIGestureRecognizerDelegate {

public func gestureRecognizer(
_ gestureRecognizer: UIGestureRecognizer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import UIKit

extension HImageView {
extension HNImageView {

func addViewContainer() {
guard let window = UIApplication.shared.keyWindow else { return }
Expand All @@ -17,6 +17,7 @@ extension HImageView {
func viewContainnerAddImageView() {
imageView = UIImageView(frame: self.globalFrame ?? CGRect())
imageView?.image = self.image
imageView?.contentMode = self.contentMode
guard let imageView = imageView else { return }
viewContainer?.addSubview(imageView)
}
Expand All @@ -35,12 +36,15 @@ extension HImageView {
updateFlags()
self.backgroundColor = .black
self.image = nil
delegate?.hnImageView(state: .start)
}
}

func stopZoom() {
if viewContainer != nil {
dismiss()
dismiss { [weak self] in
self?.delegate?.hnImageView(state: .end)
}
}
}

Expand All @@ -60,7 +64,9 @@ extension HImageView {

if newScale < config.minZoom {
if config.autoStopWhenZoomMin {
dismiss()
dismiss{ [weak self] in
self?.delegate?.hnImageView(state: .end)
}
}
else {
newScale = 1
Expand All @@ -82,11 +88,11 @@ extension HImageView {
sender.scale = 1
}
}
// delegate?.output(scale: newScale, frameImageView: view.frame)
delegate?.hnImageView(state: .changed)
}

func acceptNewValueAlpha() {
let areaImageView = (imageView?.frame.height ?? 0) * (imageView?.frame.width ?? 0)
let areaImageView = (imageView?.frame.height ?? 1) * (imageView?.frame.width ?? 1)
let areaImageViewOriginal = frameImageOriginal.height * frameImageOriginal.width
let result = 1.2 - areaImageViewOriginal/areaImageView
if result >= 0.9 {
Expand Down
8 changes: 3 additions & 5 deletions Classes/HImageViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
//

import UIKit

public protocol HImageViewDelegate: class {

func output(scale: CGFloat, frameImageView: CGRect)


public protocol HNImageViewDelegate: class {
func hnImageView(state: HNImageView.StateZoomImageView)
}
33 changes: 26 additions & 7 deletions Classes/HUIImageView.swift → Classes/HNImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

import UIKit

public class HImageView: UIImageView {
public class HNImageView: UIImageView {

public enum StateZoomImageView {
case start
case changed
case end
}

//MARK: Delegate + Config
// weak var delegate: HImageViewDelegate?
var config: HImageViewConfigure = HImageViewConfigure()
public weak var delegate: HNImageViewDelegate?
var config: HNImageViewConfigure = HNImageViewConfigure()

//MARK: Varible
var isZooming = false
Expand All @@ -23,7 +29,7 @@ public class HImageView: UIImageView {
var viewContainer: UIView? = nil
var imageView: UIImageView? = UIImageView(frame: .zero)

public func config(_ model: HImageViewConfigure) {
public func config(_ model: HNImageViewConfigure) {
self.config = model
}

Expand Down Expand Up @@ -52,6 +58,19 @@ public class HImageView: UIImageView {
}

//MARK: Init
override init(image: UIImage?) {
super.init(image: image)
}

override init(frame: CGRect) {
super.init(frame: frame)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}


public override func awakeFromNib() {
super.awakeFromNib()
initPanGesture()
Expand Down Expand Up @@ -80,6 +99,7 @@ public class HImageView: UIImageView {
let newY: CGFloat = imageView.center.y + translation.y
imageView.center = CGPoint(x: newX, y: newY)
sender.setTranslation(CGPoint.zero, in: self)
delegate?.hnImageView(state: .changed)
}
}

Expand All @@ -93,7 +113,7 @@ public class HImageView: UIImageView {
acceptNewValueAlpha()
}

func dismiss() {
func dismiss(completion: @escaping ()->()) {
UIView.animate(withDuration: config.durationDismissZoom, animations: {
self.imageView?.center = self.originalImageCenter ?? CGPoint()
self.imageView!.transform = CGAffineTransform.identity
Expand All @@ -105,8 +125,7 @@ public class HImageView: UIImageView {
self.viewContainer?.removeFromSuperview()
self.imageView?.removeFromSuperview()
self.viewContainer = nil
// self.imageView = nil
// NotificationCenter.default.post(name: NSNotification.Name.CellStopZoom, object: nil)
completion()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import UIKit

public struct HImageViewConfigure {
public struct HNImageViewConfigure {

public var backgroundColor: UIColor = .clear
public var durationDismissZoom: TimeInterval = 0.2
Expand Down
16 changes: 12 additions & 4 deletions Example/HNImageView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,12 @@
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = 4XB4SZ6RM3;
LastSwiftMigration = 0900;
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = 4XB4SZ6RM3;
LastSwiftMigration = 0900;
TestTargetID = 607FACCF1AFB9204008FA782;
};
Expand Down Expand Up @@ -494,13 +496,14 @@
baseConfigurationReference = 894A100F699B51005FC3DCB5 /* Pods-HNImageView_Example.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 4XB4SZ6RM3;
INFOPLIST_FILE = HNImageView/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -509,20 +512,23 @@
baseConfigurationReference = 0CEC503AFBF8106F8A32FFEA /* Pods-HNImageView_Example.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 4XB4SZ6RM3;
INFOPLIST_FILE = HNImageView/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
607FACF31AFB9204008FA782 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = AB9FDA68A16F8A248DDB4557 /* Pods-HNImageView_Tests.debug.xcconfig */;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
DEVELOPMENT_TEAM = 4XB4SZ6RM3;
FRAMEWORK_SEARCH_PATHS = (
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -536,7 +542,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/HNImageView_Example.app/HNImageView_Example";
};
name = Debug;
Expand All @@ -545,6 +551,8 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 701EA20AA143C0C9926147A4 /* Pods-HNImageView_Tests.release.xcconfig */;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
DEVELOPMENT_TEAM = 4XB4SZ6RM3;
FRAMEWORK_SEARCH_PATHS = (
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -554,7 +562,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/HNImageView_Example.app/HNImageView_Example";
};
name = Release;
Expand Down
2 changes: 1 addition & 1 deletion Example/HNImageView/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
Expand Down
14 changes: 8 additions & 6 deletions Example/HNImageView/Cells/MediaCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@

import UIKit
import HNImageView
class MediaCell: UICollectionViewCell {

@IBOutlet weak var imageView: HImageView!
class MediaCell: UICollectionViewCell {

@IBOutlet weak var imageView: HNImageView!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let configZoom = HImageViewConfigure(backgroundColor: .black,

let configZoomImageView = HNImageViewConfigure(backgroundColor: .black,
durationDismissZoom: 0.2,
maxZoom: 3,
minZoom: 0.8,
vibrateWhenStop: false,
autoStopWhenZoomMin: false,
isUpdateAlphaWhenHandle: true)
imageView.config(configZoom)
imageView.image = UIImage.init(named: ["a","c","d"].randomElement() ?? "a")
imageView.config(configZoomImageView)

imageView.image = UIImage.init(named: ["pic1", "pic2", "pic3", "pic4", "pic5"].randomElement() ?? "pic1")
}

override func prepareForReuse() {
Expand Down
4 changes: 2 additions & 2 deletions Example/HNImageView/Cells/MediaCell.xib
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<rect key="frame" x="0.0" y="0.0" width="196" height="241"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="QbO-5g-vXJ" customClass="HImageView" customModule="HNImageView">
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="QbO-5g-vXJ" customClass="HNImageView" customModule="HNImageView">
<rect key="frame" x="0.0" y="0.0" width="196" height="241"/>
<color key="backgroundColor" red="1" green="0.086274509799999996" blue="0.47058823529999999" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</imageView>
</subviews>
</view>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"images" : [
{
"idiom" : "universal",
"filename" : "Green-Leaves-to.jpg",
"filename" : "image 206.png",
"scale" : "1x"
},
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"images" : [
{
"idiom" : "universal",
"filename" : "d73355bf49e6b0b8e9f7.jpg",
"filename" : "image 201.png",
"scale" : "1x"
},
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"images" : [
{
"idiom" : "universal",
"filename" : "Screenshot 2019-08-29 at 09.42.41.png",
"filename" : "image 210.png",
"scale" : "1x"
},
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions Example/HNImageView/Images.xcassets/pic4.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "image 208.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"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.
21 changes: 21 additions & 0 deletions Example/HNImageView/Images.xcassets/pic5.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "Profile Picture.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"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

0 comments on commit 8ed6eac

Please sign in to comment.