Skip to content

Commit 3213368

Browse files
committed
Merge pull request #637 from C4Framework/InitOriginal
Adds copy methods to AudioPlayer, Gradient and Vector
2 parents 54cf114 + 5bd8807 commit 3213368

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

Diff for: C4/Core/Vector.swift

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ public struct Vector: Equatable, CustomStringConvertible {
9595
z = 0
9696
}
9797

98+
/// Initializes a Vector from another Vector
99+
/// - parameter copy: a previously initialized Vector
100+
public init(copy original: Vector) {
101+
x = original.x
102+
y = original.y
103+
z = original.z
104+
}
105+
98106
/// The polar representation magnitude of the vector.
99107
/// ````
100108
/// let v = Vector(x: 2.0, y: 1.0, z: 0.0)

Diff for: C4/UI/AudioPlayer.swift

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import AVFoundation
4040
public class AudioPlayer: NSObject, AVAudioPlayerDelegate {
4141
internal var player: AVAudioPlayer!
4242

43+
var filename: String!
44+
4345
/// Initializes a new audio player from a given file name
4446
/// ````
4547
/// let ap = AudioPlayer("audioTrackFileName")
@@ -66,6 +68,11 @@ public class AudioPlayer: NSObject, AVAudioPlayerDelegate {
6668

6769
self.player = player
6870
player.delegate = self
71+
self.filename = name
72+
}
73+
74+
public convenience init?(copy original: AudioPlayer) {
75+
self.init(original.filename)
6976
}
7077

7178
/// Plays a sound asynchronously.

Diff for: C4/UI/Gradient.swift

+25-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class Gradient: View {
3636
return gradientView.gradientLayer
3737
}
3838

39-
var gradientView: GradientView {
39+
internal var gradientView: GradientView {
4040
return view as! GradientView // swiftlint:disable:this force_cast
4141
}
4242

@@ -101,18 +101,38 @@ public class Gradient: View {
101101
}
102102
}
103103

104+
/// The current rotation value of the view. Animatable.
105+
/// - returns: A Double value representing the cumulative rotation of the view, measured in Radians.
106+
public override var rotation: Double {
107+
get {
108+
if let number = gradientLayer.valueForKeyPath(Layer.rotationKey) as? NSNumber {
109+
return number.doubleValue
110+
}
111+
return 0.0
112+
}
113+
set {
114+
gradientLayer.setValue(newValue, forKeyPath: Layer.rotationKey)
115+
}
116+
}
117+
104118
/// Initializes a new Gradient.
105119
///
106120
/// - parameter frame: A Rect that defines the frame for the gradient's view.
107121
/// - parameter colors: An array of Color objects that define the gradient's colors. Defaults to [C4Blue, C4Purple].
108122
/// - parameter locations: An array of Double values that define the location of each gradient stop. Defaults to [0,1]
109-
public convenience init(frame: Rect, colors: [Color] = [C4Blue, C4Purple], locations: [Double] = [0, 1]) {
110-
assert(colors.count == locations.count, "colors and locations need to have the same number of elements")
123+
public convenience override init(frame: Rect) {
111124
self.init()
112125
self.view = GradientView(frame: CGRect(frame))
113-
self.colors = colors
114-
self.locations = locations
126+
self.colors = [C4Pink, C4Purple]
127+
self.locations = [0, 1]
115128
self.startPoint = Point()
116129
self.endPoint = Point(1, 0)
117130
}
131+
132+
public convenience init(copy original: Gradient) {
133+
self.init(frame: original.frame)
134+
self.colors = original.colors
135+
self.locations = original.locations
136+
copyViewStyle(original)
137+
}
118138
}

Diff for: C4/UI/GradientLayer.swift

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public class GradientLayer: CAGradientLayer {
2626
/// This value can be set globally, after which changes to any shape's properties will be immediate.
2727
public static var disableActions = true
2828

29+
private var _rotation = 0.0
30+
31+
/// The value of the receiver's current rotation state.
32+
/// This value is cumulative, and can represent values beyong +/- π
33+
public dynamic var rotation: Double {
34+
return _rotation
35+
}
36+
2937
/// This method searches for the given action object of the layer. Actions define dynamic behaviors for a layer. For example, the animatable properties of a layer typically have corresponding action objects to initiate the actual animations. When that property changes, the layer looks for the action object associated with the property name and executes it. You can also associate custom action objects with your layer to implement app-specific actions.
3038
///
3139
/// - parameter key: The identifier of the action.

0 commit comments

Comments
 (0)