Skip to content

Instantly share code, notes, and snippets.

View saoudrizwan's full-sized avatar

Saoud Rizwan saoudrizwan

View GitHub Profile
linesConvergingAnimation.isReversed = true
// Finish animation in reverse
linesConvergingAnimation.startAnimation()
linesConvergingAnimation = UIViewPropertyAnimator(duration: 0.2, curve: UIViewAnimationCurve.linear, animations: {
// Set the new constraints
self.firstLine.snp.remakeConstraints({ (make) in
make.height.equalTo(self.lineHeight) // constant
make.width.equalTo(self.containerView.snp.width)
make.centerX.equalTo(self.containerView.snp.centerX)
make.centerY.equalTo(self.containerView.snp.centerY)
})
self.thirdLine.snp.remakeConstraints({ (make) in
make.height.equalTo(self.lineHeight) // constant
// Add a completion block to the UIViewPropertyAnimator
linesConvergingAnimation.addCompletion({ (position) in
// since this is the reversed animation's completion block, the
// "final position" is the initial animation's target-start-position.
if position == .start {
self.linesAreX = false
// reset constraints back to normal
self.firstLine.snp.remakeConstraints({ (make) in
make.height.equalTo(self.lineHeight)
make.width.equalTo(self.containerView.snp.width)
// show a nav bar
// 1. change some constraints
navBarHeightConstraint.update(offset: 80)
// 2. animate
UIView.animate(withDuration: 0.10, delay: 0.0, options: .curveLinear, animations: {
self.view.layoutIfNeeded() // Apple docs: "layout the subviews immediately"
}, completion: nil)
// 1. change some constraints
navBarHeightConstraint.update(offset: 80)
// 2. animate
UIView.animate(withDuration: 0.10, delay: 0.0, options: .curveLinear, animations: {
self.navBar.layoutIfNeeded()
}, completion: nil)
let numbers = [1, 2, 3, 4, 5]
var evenNumbers = [Int]()
for i in 0..<numbers.count {
let number = numbers[i]
if number % 2 == 0 {
evenNumbers.append(number)
}
}
/// Returns an array containing, in order, the elements of the sequence
/// that satisfy the given predicate.
/// - Parameter shouldInclude: A closure that takes an element of the
/// sequence as its argument and returns a Boolean value indicating
/// whether the element should be included in the returned array.
/// - Returns: An array of the elements that `includeElement` allowed.
public func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element]
let numbers = [1, 2, 3, 4, 5].map { $0 * 2 }
print(numbers) // [2, 4, 6, 8, 10]
let arrayWithNoNils = [1, 2, 3, nil].flatMap { $0 }
print(arrayWithNoNils) // [1, 2, 3]
let arrayWithNoOptionals = ["1", "two", "3"].flatMap { Int($0) }
print(arrayWithNoOptionals) // [1, 3]