Skip to content

Instantly share code, notes, and snippets.

@dqhieu
dqhieu / Delay.swift
Last active March 12, 2017 16:31
Delay action in Swift
// Swift 3
func Delay(seconds: TimeInterval, _ completion: @escaping () -> Void) {
let when = DispatchTime.now() + seconds
DispatchQueue.main.asyncAfter(deadline: when) {
completion()
}
}
class TimeOut: NSObject {
private let queue:dispatch_queue_t
private let semaphore:dispatch_semaphore_t
var completion:()->()
convenience init(_ seconds:Double, completion:()->()) {
self.init(seconds, fireOn:dispatch_get_main_queue(), completion:completion)
}
@dqhieu
dqhieu / Reachability.swift
Created September 6, 2016 07:37
Check reachability in ios
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0))
let myView = MyView(title: "Title", subTitle: "Sub title", content: "Content")
let myView2 = MyView(frame: .zero)
myView2.setTitle("Title", subTitle: "Sub title", content: "Content")
@dqhieu
dqhieu / MyView.swift
Last active September 10, 2017 07:55
import UIKit
import SnapKit
class MyView: UIView {
private var _titleLabel: UILabel!
private var _subTitleLabel: UILabel!
private var _contentLabel: UILabel!
init(title: String? = nil, subTitle: String? = nil, content: String? = nil) {
myView = MyView(title: "Lorem Ipsum is simply dummy text of the printing", subTitle: "Lorem Ipsum is simply dummy text of the printing", content: "Lorem Ipsum is simply dummy text of the printing")
self.view.addSubview(myView)
myView.snp.makeConstraints { (make) in
make.width.equalToSuperview().multipliedBy(0.5)
make.centerX.centerY.equalToSuperview()
}
private func setupComponents() {
_titleLabel = UILabel(frame: .zero)
_titleLabel.textColor = UIColor.black
_titleLabel.numberOfLines = 0
_titleLabel.backgroundColor = UIColor.red
addSubview(_titleLabel)
_subTitleLabel = UILabel(frame: .zero)
_subTitleLabel.textColor = UIColor.black
_subTitleLabel.numberOfLines = 0
public func setTitle(_ title: String? = nil, subTitle: String? = nil, content: String? = nil) {
_titleLabel.text = title
_subTitleLabel.text = subTitle
_contentLabel.text = content
updateFrame()
}
private func updateFrame() {
let titleLabelSize = _titleLabel.sizeThatFits(CGSize(width: _titleLabel.frame.width, height: 9999))
@dqhieu
dqhieu / TwitterAnimation.swift
Last active April 6, 2023 00:50
Twitter's new animation ❤️
struct ContentView: View {
@State var scale: CGFloat = 0
@State var showingBlankHeart = true
@State var heartScale: CGFloat = 0
@State var textOpacity: Double = 1
@State var offset1: CGFloat = 0
@State var plus1Opacity: Double = 1
@State var offset2: CGFloat = 0
@State var plus2Opacity: Double = 1
struct ContentView: View {
@State private var isActive = false
@State private var isPressing = false
@State var scale: CGFloat = 1
@State var count = 0
var body: some View {
ZStack {
Circle()