Skip to content

Instantly share code, notes, and snippets.

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
linesConvergingAnimation.isReversed = true
// Finish animation in reverse
linesConvergingAnimation.startAnimation()
// 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)
class LinkResponsiveTextView: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
self.delaysContentTouches = false
// required for tap to pass through on to superview & for links to work
self.isScrollEnabled = false
self.isEditable = false
self.isUserInteractionEnabled = true
@saoudrizwan
saoudrizwan / Safari.swift
Last active February 28, 2017 16:23
Helper method to open a safari view in app. Don't forget to import the SafariServices framework in 'Project' > 'Linked Frameworks'.
import SafariServices
var safariViewController: SFSafariViewController?
extension ViewController: SFSafariViewControllerDelegate {
func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
if !didLoadSuccessfully {
// check if online
print("couldn't open safari view")
@saoudrizwan
saoudrizwan / TouchUpInsideViews.swift
Last active October 29, 2023 14:16
Using a long press gesture recognizer, you can recreate a 'touch up inside' button effect on any view.
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(upgradeAlertViewOtherUpgradesLongPressHandler))
longPress.minimumPressDuration = 0
var longPressGRStartPoint: CGPoint?
var didCancelLongPressGR = false
func viewTouched(sender: UILongPressGestureRecognizer) {
let currentPoint = sender.location(in: self.view)
switch sender.state {
case .began:
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)
}
}
let evenNumbers = [1, 2, 3, 4, 5].filter { (number) -> Bool in
if number % 2 == 0 {
return true
} else {
return false
}
}