@@ -10,7 +10,7 @@ import UIKit
10
10
11
11
public class AASquaresLoading : UIView {
12
12
public var view : UIView = UIView ( )
13
- public var size : Float = 40
13
+ private ( set ) public var size : Float = 0
14
14
public var color : UIColor = UIColor . blackColor ( ) {
15
15
didSet {
16
16
for layer in squares {
@@ -35,66 +35,70 @@ public class AASquaresLoading : UIView {
35
35
private var squares : [ CALayer ] = [ CALayer] ( )
36
36
37
37
public init ( target: UIView ) {
38
- let frame = target. frame
39
- super. init ( frame: frame)
38
+ super. init ( frame: target. frame)
40
39
41
- setup ( target, size: self . size)
40
+ parentView = target
41
+ setup ( self . size)
42
42
}
43
43
44
44
public init ( target: UIView , size: Float ) {
45
- let frame = target. frame
46
- super. init ( frame: frame)
45
+ super. init ( frame: target. frame)
47
46
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) )
55
47
parentView = target
56
- self . initialize ( )
48
+ setup ( size )
57
49
}
58
50
59
51
override init ( frame: CGRect ) {
60
52
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 )
69
55
}
70
56
71
57
required public init ? ( coder aDecoder: NSCoder ) {
72
58
super. init ( coder: aDecoder)
73
59
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
+ }
74
78
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 )
78
82
}
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 ) )
81
85
}
82
-
83
- public func start( ) {
86
+
87
+ public func start( delay : NSTimeInterval = 0.0 ) {
84
88
if ( parentView != nil ) {
85
89
self . layer. opacity = 0
86
90
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,
88
92
animations: { ( ) -> Void in
89
93
self . layer. opacity = 1
90
94
} , completion: nil )
91
95
}
92
96
}
93
97
94
- public func stop( ) {
98
+ public func stop( delay : NSTimeInterval = 0.0 ) {
95
99
if ( parentView != nil ) {
96
100
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,
98
102
animations: { ( ) -> Void in
99
103
self . layer. opacity = 0
100
104
} , completion: { ( success: Bool ) -> Void in
@@ -111,7 +115,7 @@ public class AASquaresLoading : UIView {
111
115
squares = [ CALayer] ( )
112
116
113
117
self . addSubview ( view)
114
- self . backgroundColor = UIColor . lightGrayColor ( ) . colorWithAlphaComponent ( 0.4 )
118
+ self . backgroundColor = UIColor . whiteColor ( ) . colorWithAlphaComponent ( 0.9 )
115
119
for var i : Int = 0 ; i < 3 ; i++ {
116
120
for var j : Int = 0 ; j < 3 ; j++ {
117
121
var offsetX , offsetY : Float
0 commit comments