-
Notifications
You must be signed in to change notification settings - Fork 95
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
Add sharing selected target view and slide in / out animation #31
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
298e624
Add sharing selected target view and slide in/out animation
eJamesLin 3085cb3
extract Design value to using enum, changed to static getter for enum…
eJamesLin 412e8da
applied existing `safeBottomAnchor`
eJamesLin 3eafe02
Make the failure explicitly by using implicitly unwrapped optional
eJamesLin 6dc084e
rename func for more common practice
eJamesLin 3c2e4ce
fix the alpha in non animated case
eJamesLin 7a5e2f2
use `setMode(_ mode: Mode, animated: Bool)` to guarantee calling `upd…
eJamesLin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// SelectedTargetView.swift | ||
// | ||
// Copyright (c) 2016-present, LINE Corporation. All rights reserved. | ||
// | ||
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use, | ||
// copy and distribute this software in source code or binary form for use | ||
// in connection with the web services and APIs provided by LINE Corporation. | ||
// | ||
// As with any software that integrates with the LINE Corporation platform, your use of this software | ||
// is subject to the LINE Developers Agreement [http://terms2.line.me/LINE_Developers_Agreement]. | ||
// This copyright notice shall be included in all copies or substantial portions of the software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// | ||
|
||
import UIKit | ||
|
||
class SelectedTargetView: UIView { | ||
enum Design { | ||
static var height: CGFloat { return 79 } | ||
static var bgColor: UIColor { return .init(hex6: 0xF7F8FA) } | ||
static var borderColor: UIColor { return .init(hex6: 0xE6E7EA) } | ||
static var borderWidth: CGFloat { return 0.5 } | ||
} | ||
|
||
private var slideAnimationViewTopConstraint: NSLayoutConstraint! | ||
|
||
private let slideAnimationView: UIView = { | ||
let view = UIView(frame: .zero) | ||
view.backgroundColor = Design.bgColor | ||
view.layer.borderWidth = Design.borderWidth | ||
view.layer.borderColor = Design.borderColor.cgColor | ||
return view | ||
}() | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
backgroundColor = .clear | ||
|
||
setupSubviews() | ||
|
||
setupLayouts() | ||
} | ||
|
||
private func setupSubviews() { | ||
addSubview(slideAnimationView) | ||
} | ||
|
||
private func setupLayouts() { | ||
slideAnimationView.translatesAutoresizingMaskIntoConstraints = false | ||
slideAnimationViewTopConstraint = slideAnimationView.topAnchor.constraint(equalTo: topAnchor) | ||
slideAnimationViewTopConstraint.isActive = true | ||
NSLayoutConstraint.activate([ | ||
slideAnimationView.leadingAnchor.constraint(equalTo: leadingAnchor), | ||
slideAnimationView.trailingAnchor.constraint(equalTo: trailingAnchor), | ||
slideAnimationView.heightAnchor.constraint(equalTo: heightAnchor) | ||
]) | ||
} | ||
|
||
enum Mode { | ||
case show | ||
case hide | ||
} | ||
|
||
func setMode(_ mode: Mode, animated: Bool) { | ||
self.mode = mode | ||
updateLayout(animated: animated) | ||
} | ||
|
||
private(set) var mode = Mode.hide | ||
|
||
private func updateLayout(animated: Bool) { | ||
self.slideAnimationViewTopConstraint.isActive = false | ||
let anchor: NSLayoutYAxisAnchor | ||
let alpha: CGFloat | ||
switch self.mode { | ||
case .show: | ||
anchor = topAnchor | ||
isUserInteractionEnabled = true | ||
alpha = 1 | ||
case .hide: | ||
anchor = bottomAnchor | ||
isUserInteractionEnabled = false | ||
alpha = 0 | ||
} | ||
self.slideAnimationViewTopConstraint = slideAnimationView.topAnchor.constraint(equalTo: anchor) | ||
self.slideAnimationViewTopConstraint.isActive = true | ||
|
||
if animated { | ||
UIView.animate(withDuration: 0.2) { | ||
self.alpha = alpha | ||
self.layoutIfNeeded() | ||
} | ||
} else { | ||
self.alpha = alpha | ||
self.layoutIfNeeded() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess if
animated == false
, thealpha
is not getting updated now. Maybe we also need the case for non-animated
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I suggest also call
layoutIfNeeded()
in theanimated == false
statement, to trigger an immediate layout. Otherwise, any code depends on the layout would not work until the next main run loop, which causes confusion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I missed the alpha setup in no animation case.
Agree. the
layoutIfNeeded
inanimated == false
will be more clear.