diff --git a/.gitignore b/.gitignore index b22f023..c879a0d 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ DerivedData *.hmap *.ipa *.xcuserstate +IDEWorkspaceChecks.plist # CocoaPods # diff --git a/SpringIndicator/RefreshIndicator.swift b/SpringIndicator/RefreshIndicator.swift index 8f06ca5..b83ab02 100644 --- a/SpringIndicator/RefreshIndicator.swift +++ b/SpringIndicator/RefreshIndicator.swift @@ -45,6 +45,10 @@ open class RefreshIndicator: UIControl { setupIndicator() } + deinit { + stopIndicatorAnimation() + } + private func setupIndicator() { indicator.lineWidth = 2 indicator.rotationDuration = 1 diff --git a/SpringIndicator/SpringIndicator.swift b/SpringIndicator/SpringIndicator.swift index b0572c8..6fb224d 100644 --- a/SpringIndicator/SpringIndicator.swift +++ b/SpringIndicator/SpringIndicator.swift @@ -10,9 +10,6 @@ import UIKit @IBDesignable open class SpringIndicator: UIView { - deinit { - stop() - } let indicatorView: UIView fileprivate var pathLayer: CAShapeLayer? { @@ -65,15 +62,30 @@ open class SpringIndicator: UIView { backgroundColor = UIColor.clear } - + + deinit { + stop() + } + open override func draw(_ rect: CGRect) { super.draw(rect) - if animating { start(for: .begin) } } + open override func layoutSubviews() { + super.layoutSubviews() + restartAnimationsIfNeeded() + } + + private func restartAnimationsIfNeeded() { + if isSpinning { + stop() + start(for: .begin) + } + } + private func makeRotationPath(for process: AnimationProcess) -> UIBezierPath { let start = CGFloat(process.startAngle()) let end = CGFloat(Double.pi + Double.pi_2) + start diff --git a/SpringIndicatorExample/SpringIndicatorExample.xcodeproj/project.pbxproj b/SpringIndicatorExample/SpringIndicatorExample.xcodeproj/project.pbxproj index 5653dbf..42520ec 100644 --- a/SpringIndicatorExample/SpringIndicatorExample.xcodeproj/project.pbxproj +++ b/SpringIndicatorExample/SpringIndicatorExample.xcodeproj/project.pbxproj @@ -453,6 +453,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; INFOPLIST_FILE = SpringIndicatorExample/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 9.1; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.kyoheiito.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -467,6 +468,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; INFOPLIST_FILE = SpringIndicatorExample/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 9.1; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.kyoheiito.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/SpringIndicatorExample/SpringIndicatorExample/Base.lproj/Main.storyboard b/SpringIndicatorExample/SpringIndicatorExample/Base.lproj/Main.storyboard index ca8c892..a9bf132 100644 --- a/SpringIndicatorExample/SpringIndicatorExample/Base.lproj/Main.storyboard +++ b/SpringIndicatorExample/SpringIndicatorExample/Base.lproj/Main.storyboard @@ -1,8 +1,12 @@ - - + + + + + - + + @@ -10,7 +14,7 @@ - + @@ -30,12 +34,12 @@ - + - - + + @@ -45,34 +49,31 @@ - - - - + - + @@ -86,6 +87,7 @@ + @@ -96,18 +98,18 @@ - + - - + + - + @@ -132,15 +134,15 @@ - + - - + + - + diff --git a/SpringIndicatorExample/SpringIndicatorExample/TableViewController.swift b/SpringIndicatorExample/SpringIndicatorExample/TableViewController.swift index 2aac91e..b0352ee 100644 --- a/SpringIndicatorExample/SpringIndicatorExample/TableViewController.swift +++ b/SpringIndicatorExample/SpringIndicatorExample/TableViewController.swift @@ -10,24 +10,25 @@ import UIKit import SpringIndicator class TableViewController: UIViewController { + @IBOutlet weak var tableView: UITableView! + let refreshControl = RefreshIndicator() let dataSourceList: [[String]] = [[Int](0..<20).map({ "section 0, cell \($0)" })] override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view, typically from a nib. - tableView.dataSource = self tableView.delegate = self - + tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") - + refreshControl.addTarget(self, action: #selector(TableViewController.onRefresh), for: .valueChanged) + tableView.addSubview(refreshControl) } - + @objc func onRefresh() { DispatchQueue.main.asyncAfter(deadline: .now() + 2) { self.refreshControl.endRefreshing() diff --git a/SpringIndicatorExample/SpringIndicatorExample/ViewController.swift b/SpringIndicatorExample/SpringIndicatorExample/ViewController.swift index b44a22e..1586296 100644 --- a/SpringIndicatorExample/SpringIndicatorExample/ViewController.swift +++ b/SpringIndicatorExample/SpringIndicatorExample/ViewController.swift @@ -13,12 +13,25 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view, typically from a nib. - let defaultIndicator = SpringIndicator(frame: CGRect(x: 100, y: 100, width: 60, height: 60)) defaultIndicator.lineColors = [.red, .blue, .orange, .green] defaultIndicator.rotationDuration = 2 + defaultIndicator.translatesAutoresizingMaskIntoConstraints = false view.addSubview(defaultIndicator) + + let topAnchor: NSLayoutYAxisAnchor + if #available(iOS 11.0, *) { + topAnchor = view.safeAreaLayoutGuide.topAnchor + } else { + topAnchor = topLayoutGuide.topAnchor + } + + NSLayoutConstraint.activate([ + defaultIndicator.topAnchor.constraint(equalTo: topAnchor, constant: 40), + defaultIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor), + defaultIndicator.heightAnchor.constraint(equalToConstant: 40), + defaultIndicator.widthAnchor.constraint(equalToConstant: 40), + ]) defaultIndicator.start() let colorIndicator = SpringIndicator(frame: CGRect(x: 300, y: 100, width: 20, height: 20)) @@ -29,9 +42,4 @@ class ViewController: UIViewController { colorIndicator.start() } - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } } -