Skip to content

Commit b166395

Browse files
committed
Fixed init issues + added delay
1 parent 0002c0d commit b166395

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

AASquaresLoadingDemo/AASquaresLoadingDemoTests/Info.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>CFBundleExecutable</key>
88
<string>$(EXECUTABLE_NAME)</string>
99
<key>CFBundleIdentifier</key>
10-
<string>co.aitali.$(PRODUCT_NAME:rfc1034identifier)</string>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>

Source/AASquaresLoading.swift

+37-33
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import UIKit
1010

1111
public class AASquaresLoading : UIView {
1212
public var view : UIView = UIView()
13-
public var size : Float = 40
13+
private(set) public var size : Float = 0
1414
public var color : UIColor = UIColor.blackColor() {
1515
didSet {
1616
for layer in squares {
@@ -35,66 +35,70 @@ public class AASquaresLoading : UIView {
3535
private var squares : [CALayer] = [CALayer]()
3636

3737
public init(target: UIView) {
38-
let frame = target.frame
39-
super.init(frame: frame)
38+
super.init(frame: target.frame)
4039

41-
setup(target, size: self.size)
40+
parentView = target
41+
setup(self.size)
4242
}
4343

4444
public init(target: UIView, size: Float) {
45-
let frame = target.frame
46-
super.init(frame: frame)
45+
super.init(frame: target.frame)
4746

48-
setup(target, size: size)
49-
}
50-
51-
private func setup(target: UIView, size: Float) {
52-
self.size = size
53-
self.view.frame = CGRectMake(frame.width / 2 - CGFloat(size) / 2,
54-
frame.height / 2 - CGFloat(size) / 2, CGFloat(size), CGFloat(size))
5547
parentView = target
56-
self.initialize()
48+
setup(size)
5749
}
5850

5951
override init(frame: CGRect) {
6052
super.init(frame: frame)
61-
62-
if size == 0 {
63-
let width = frame.size.width
64-
let height = frame.size.height
65-
size = width > height ? Float(height) : Float(width)
66-
}
67-
self.view.frame = frame
68-
self.initialize()
53+
54+
setup(0)
6955
}
7056

7157
required public init?(coder aDecoder: NSCoder) {
7258
super.init(coder: aDecoder)
7359

60+
setup(0)
61+
}
62+
63+
public override func layoutSubviews() {
64+
updateFrame()
65+
super.layoutSubviews()
66+
}
67+
68+
private func setup(size: Float) {
69+
self.size = size
70+
updateFrame()
71+
self.initialize()
72+
}
73+
74+
private func updateFrame() {
75+
if parentView != nil {
76+
self.frame = CGRectMake(0, 0, CGRectGetWidth(parentView!.frame), CGRectGetHeight(parentView!.frame))
77+
}
7478
if size == 0 {
75-
let width = CGRectGetWidth(self.frame)
76-
let height = CGRectGetHeight(self.frame)
77-
size = width > height ? Float(height) : Float(width)
79+
let width = frame.size.width
80+
let height = frame.size.height
81+
size = width > height ? Float(height/2) : Float(width/2)
7882
}
79-
self.view.frame = frame
80-
self.initialize()
83+
self.view.frame = CGRectMake(frame.width / 2 - CGFloat(size) / 2,
84+
frame.height / 2 - CGFloat(size) / 2, CGFloat(size), CGFloat(size))
8185
}
82-
83-
public func start() {
86+
87+
public func start(delay : NSTimeInterval = 0.0) {
8488
if (parentView != nil) {
8589
self.layer.opacity = 0
8690
self.parentView!.addSubview(self)
87-
UIView.animateWithDuration(0.6, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut,
91+
UIView.animateWithDuration(0.6, delay: delay, options: UIViewAnimationOptions.CurveEaseInOut,
8892
animations: { () -> Void in
8993
self.layer.opacity = 1
9094
}, completion: nil)
9195
}
9296
}
9397

94-
public func stop() {
98+
public func stop(delay : NSTimeInterval = 0.0) {
9599
if (parentView != nil) {
96100
self.layer.opacity = 1
97-
UIView.animateWithDuration(0.6, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut,
101+
UIView.animateWithDuration(0.6, delay: delay, options: UIViewAnimationOptions.CurveEaseInOut,
98102
animations: { () -> Void in
99103
self.layer.opacity = 0
100104
}, completion: { (success: Bool) -> Void in
@@ -111,7 +115,7 @@ public class AASquaresLoading : UIView {
111115
squares = [CALayer]()
112116

113117
self.addSubview(view)
114-
self.backgroundColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.4)
118+
self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.9)
115119
for var i : Int = 0; i < 3; i++ {
116120
for var j : Int = 0; j < 3; j++ {
117121
var offsetX, offsetY : Float

0 commit comments

Comments
 (0)