Skip to content
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

Update shout view to increase larger touch area #132

Merged
merged 2 commits into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Demo/WhisperDemo/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Whisper (3.1.1)
- Whisper (4.0.0)

DEPENDENCIES:
- Whisper (from `../../`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../../"

SPEC CHECKSUMS:
Whisper: 74decae53daf8caff5acf8628e24b92d3f05a453
Whisper: 08be92623311f8e53201e62e17f6d7b9599a4714

PODFILE CHECKSUM: e20a3f258a5cd7e57b37f62aadb18fd14d135133

COCOAPODS: 1.1.0.rc.2
COCOAPODS: 1.1.1
10 changes: 8 additions & 2 deletions Demo/WhisperDemo/WhisperDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@
};
29AD42991BBC2BD2004292F1 = {
CreatedOnToolsVersion = 7.0;
DevelopmentTeam = LG4DBY4QF9;
LastSwiftMigration = 0800;
ProvisioningStyle = Automatic;
};
};
};
Expand Down Expand Up @@ -435,9 +437,11 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = LG4DBY4QF9;
INFOPLIST_FILE = WhisperDemo/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = RamonGilabert.WhisperDemo;
PRODUCT_BUNDLE_IDENTIFIER = no.hyper.WhisperDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
Expand All @@ -449,9 +453,11 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = LG4DBY4QF9;
INFOPLIST_FILE = WhisperDemo/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = RamonGilabert.WhisperDemo;
PRODUCT_BUNDLE_IDENTIFIER = no.hyper.WhisperDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
Expand Down
4 changes: 2 additions & 2 deletions Demo/WhisperDemo/WhisperDemo/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand Down Expand Up @@ -43,5 +41,7 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
</plist>
84 changes: 42 additions & 42 deletions Source/ShoutFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ open class ShoutView: UIView {
public static let indicatorWidth: CGFloat = 50
public static let imageSize: CGFloat = 48
public static let imageOffset: CGFloat = 18
public static var height: CGFloat = UIApplication.shared.isStatusBarHidden ? 55 : 65
public static var textOffset: CGFloat = 75
public static var touchOffset: CGFloat = 40
}

open fileprivate(set) lazy var backgroundView: UIView = {
Expand All @@ -22,13 +22,6 @@ open class ShoutView: UIView {
return view
}()

open fileprivate(set) lazy var gestureContainer: UIView = {
let view = UIView()
view.isUserInteractionEnabled = true

return view
}()

open fileprivate(set) lazy var indicatorView: UIView = {
let view = UIView()
view.backgroundColor = ColorList.Shout.dragIndicator
Expand Down Expand Up @@ -86,15 +79,18 @@ open class ShoutView: UIView {
open fileprivate(set) var completion: (() -> ())?

private var subtitleLabelOriginalHeight: CGFloat = 0
private var internalHeight: CGFloat = 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the height of the visible view


// MARK: - Initializers

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

addSubview(backgroundView)
[indicatorView, imageView, titleLabel, subtitleLabel, gestureContainer].forEach {
backgroundView.addSubview($0) }
[imageView, titleLabel, subtitleLabel, indicatorView].forEach {
$0.autoresizingMask = []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need autoresizing, so explicitly disable it

backgroundView.addSubview($0)
}

clipsToBounds = false
isUserInteractionEnabled = true
Expand All @@ -103,8 +99,8 @@ open class ShoutView: UIView {
layer.shadowOpacity = 0.1
layer.shadowRadius = 0.5

addGestureRecognizer(tapGestureRecognizer)
gestureContainer.addGestureRecognizer(panGestureRecognizer)
backgroundView.addGestureRecognizer(tapGestureRecognizer)
addGestureRecognizer(panGestureRecognizer)

NotificationCenter.default.addObserver(self, selector: #selector(ShoutView.orientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
Expand All @@ -120,8 +116,6 @@ open class ShoutView: UIView {
// MARK: - Configuration

open func craft(_ announcement: Announcement, to: UIViewController, completion: (() -> ())?) {
Dimensions.height = UIApplication.shared.isStatusBarHidden ? 70 : 80

panGestureActive = false
shouldSilent = false
configureView(announcement)
Expand All @@ -144,22 +138,18 @@ open class ShoutView: UIView {
}

open func shout(to controller: UIViewController) {
let width = UIScreen.main.bounds.width
controller.view.addSubview(self)

frame = CGRect(x: 0, y: 0, width: width, height: 0)
backgroundView.frame = CGRect(x: 0, y: 0, width: width, height: 0)

frame.size.height = 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset to 0 to make the animation happen

UIView.animate(withDuration: 0.35, animations: {
self.frame.size.height = Dimensions.height
self.backgroundView.frame.size.height = self.frame.height
self.frame.size.height = self.internalHeight + Dimensions.touchOffset
})
}

// MARK: - Setup

public func setupFrames() {
Dimensions.height = UIApplication.shared.isStatusBarHidden ? 55 : 65
internalHeight = (UIApplication.shared.isStatusBarHidden ? 55 : 65)

let totalWidth = UIScreen.main.bounds.width
let offset: CGFloat = UIApplication.shared.isStatusBarHidden ? 2.5 : 5
Expand All @@ -171,14 +161,9 @@ open class ShoutView: UIView {
$0.sizeToFit()
}

Dimensions.height += subtitleLabel.frame.height
internalHeight += subtitleLabel.frame.height

backgroundView.frame.size = CGSize(width: totalWidth, height: Dimensions.height)
gestureContainer.frame = backgroundView.frame
indicatorView.frame = CGRect(x: (totalWidth - Dimensions.indicatorWidth) / 2,
y: Dimensions.height - Dimensions.indicatorHeight - 5, width: Dimensions.indicatorWidth, height: Dimensions.indicatorHeight)

imageView.frame = CGRect(x: Dimensions.imageOffset, y: (Dimensions.height - imageSize) / 2 + offset,
imageView.frame = CGRect(x: Dimensions.imageOffset, y: (internalHeight - imageSize) / 2 + offset,
width: imageSize, height: imageSize)

let textOffsetY = imageView.image != nil ? imageView.frame.origin.x + 3 : textOffsetX + 5
Expand All @@ -189,14 +174,30 @@ open class ShoutView: UIView {
if subtitleLabel.text?.isEmpty ?? true {
titleLabel.center.y = imageView.center.y - 2.5
}

frame = CGRect(x: 0, y: 0, width: totalWidth, height: internalHeight + Dimensions.touchOffset)
}

// MARK: - Frame

open override var frame: CGRect {
didSet {
backgroundView.frame = CGRect(x: 0, y: 0,
width: frame.size.width,
height: frame.size.height - Dimensions.touchOffset)

indicatorView.frame = CGRect(x: (backgroundView.frame.size.width - Dimensions.indicatorWidth) / 2,
y: backgroundView.frame.height - Dimensions.indicatorHeight - 5,
width: Dimensions.indicatorWidth,
height: Dimensions.indicatorHeight)
}
}

// MARK: - Actions

open func silent() {
UIView.animate(withDuration: 0.35, animations: {
self.frame.size.height = 0
self.backgroundView.frame.size.height = self.frame.height
}, completion: { finished in
self.completion?()
self.displayTimer.invalidate()
Expand All @@ -223,7 +224,6 @@ open class ShoutView: UIView {

@objc private func handlePanGestureRecognizer() {
let translation = panGestureRecognizer.translation(in: self)
var duration: TimeInterval = 0

if panGestureRecognizer.state == .began {
subtitleLabelOriginalHeight = subtitleLabel.bounds.size.height
Expand All @@ -235,27 +235,27 @@ open class ShoutView: UIView {
let maxTranslation = subtitleLabel.bounds.size.height - subtitleLabelOriginalHeight

if translation.y >= maxTranslation {
frame.size.height = Dimensions.height + maxTranslation + (translation.y - maxTranslation) / 25
frame.size.height = internalHeight + maxTranslation
+ (translation.y - maxTranslation) / 25 + Dimensions.touchOffset
} else {
frame.size.height = Dimensions.height + translation.y
frame.size.height = internalHeight + translation.y + Dimensions.touchOffset
}
} else {
panGestureActive = false
let height = translation.y < -5 || shouldSilent ? 0 : Dimensions.height
let height = translation.y < -5 || shouldSilent ? 0 : internalHeight

duration = 0.2
subtitleLabel.numberOfLines = 2
subtitleLabel.sizeToFit()

UIView.animate(withDuration: duration, animations: {
self.frame.size.height = height
}, completion: { _ in if translation.y < -5 { self.completion?(); self.removeFromSuperview() }})
UIView.animate(withDuration: 0.2, animations: {
self.frame.size.height = height + Dimensions.touchOffset
}, completion: { _ in
if translation.y < -5 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each statement in each line

self.completion?()
self.removeFromSuperview()
}
})
}

UIView.animate(withDuration: duration, animations: {
self.backgroundView.frame.size.height = self.frame.height
self.indicatorView.frame.origin.y = self.frame.height - Dimensions.indicatorHeight - 5
})
}


Expand Down