Skip to content

Adds init(copy) method to all visible objects #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions C4/UI/Ellipse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class Ellipse: Shape {
/// ````
///
/// - parameter frame: The frame within which to draw an ellipse that touches each of the four sides of the frame.
convenience public init(frame: Rect) {
self.init()
public override init(frame: Rect) {
super.init()
view.frame = CGRect(frame)
updatePath()
}
Expand Down
9 changes: 6 additions & 3 deletions C4/UI/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ public class Image: View, NSCopying {
/// canvas.add(b)
/// ````
/// - parameter image: A Image.
convenience public init(image: Image) {
convenience public init(copy image: Image) {
self.init()
let uiimage = image.uiimage
self.view = ImageView(image: uiimage)
copyViewStyle(image)
}

/// Initializes a new Image using a UIImage.
Expand Down Expand Up @@ -288,7 +289,9 @@ public class Image: View, NSCopying {
/// - returns: A UIImage object.
public var uiimage: UIImage {
get {
return imageView.image!
let layer = imageView.layer as CALayer
let contents = layer.contents as! CGImage // swiftlint:disable:this force_cast
return UIImage(CGImage: contents, scale: CGFloat(scale), orientation: imageView.image!.imageOrientation)
}
}

Expand Down Expand Up @@ -339,7 +342,7 @@ public class Image: View, NSCopying {

/// The scale factor of the image. (read-only)
var scale: Double {
return Double(uiimage.scale)
return Double(imageView.image!.scale)
}

/// A variable that provides access to the width of the receiver. Animatable.
Expand Down
9 changes: 8 additions & 1 deletion C4/UI/Movie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import AVFoundation
///
///A C4Movie’s resizing behaviour is to map itself to the edges of its visible frame. This functionality implicitly uses AVLayerVideoGravityResize as its layer’s default gravity. You can change the frame of the movie from an arbitrary shape back to its original proportion by using its originalSize, originalRatio, or by independently setting either its width or height properties.
public class Movie: View {
var filename: String!
var player: AVQueuePlayer?
var currentItem: AVPlayerItem?
var reachedEndAction: (()->())?
Expand Down Expand Up @@ -145,6 +146,7 @@ public class Movie: View {
let size = Size(movieTrack.naturalSize)

self.init(frame: Rect(0, 0, Double(size.width), Double(size.height)))
self.filename = filename
//initialize player with item
let newPlayer = AVQueuePlayer(playerItem: AVPlayerItem(asset: asset))
newPlayer.actionAtItemEnd = .Pause
Expand All @@ -166,11 +168,16 @@ public class Movie: View {
/// Initializes a new Movie using the specified frame.
///
/// - parameter frame: The frame of the new movie object.
public init(frame: Rect) {
public override init(frame: Rect) {
super.init()
self.view = MovieView(frame: CGRect(frame))
}

public convenience init?(copy original: Movie) {
self.init(original.filename)
self.frame = original.frame
copyViewStyle(original)
}

/// Begins playback of the current item.
///
Expand Down
4 changes: 2 additions & 2 deletions C4/UI/Rectangle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public class Rectangle: Shape {
/// ````
///
/// - parameter frame: A Rect whose dimensions are used to construct the Rectangle.
convenience public init(frame: Rect) {
self.init()
public override init(frame: Rect) {
super.init()
if frame.size.width <= corner.width * 2.0 || frame.size.height <= corner.width / 2.0 {
corner = Size()
}
Expand Down
51 changes: 34 additions & 17 deletions C4/UI/Shape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ public class Shape: View {
}
}


internal var shapeView: ShapeView {
return self.view as! ShapeView // swiftlint:disable:this force_cast
}

/// Initializes an empty Shape.
override init() {
super.init()

self.view = ShapeView()
self.view = ShapeView(frame: CGRect(frame))
strokeColor = C4Purple
fillColor = C4Blue
lineWidth = 1
Expand All @@ -79,27 +77,46 @@ public class Shape: View {
adjustToFitPath()
}

public override init(frame: Rect) {
super.init()
self.view = ShapeView(frame: CGRect(frame))
strokeColor = C4Purple
fillColor = C4Blue
lineWidth = 1
lineCap = .Round
lineJoin = .Round

let image = UIImage.createWithColor(UIColor.clearColor(), size: CGSize(width: 1, height: 1)).CGImage
shapeLayer.contents = image
}

/// Initializes a new Shape from the properties of another Shape. Essentially, this copies the provided shape.
/// - parameter shape: A Shape around which the new shape is created.
public convenience init(_ shape: Shape) {
self.init()
public convenience init(copy original: Shape) {
//If there is a scale transform we need to undo that
let t = CGAffineTransformInvert(original.view.transform)
let x = sqrt(t.a * t.a + t.c * t.c)
let y = sqrt(t.b * t.b + t.d * t.d)
let s = CGAffineTransformMakeScale(x, y)
self.init(frame: Rect(CGRectApplyAffineTransform(original.view.frame, s)))

let disable = ShapeLayer.disableActions
ShapeLayer.disableActions = true
self.path = shape.path
self.path = original.path
shapeLayer.path = path?.CGPath
self.frame = shape.frame
self.lineWidth = shape.lineWidth
self.lineDashPhase = shape.lineDashPhase
self.lineCap = shape.lineCap
self.lineJoin = shape.lineJoin
self.lineDashPattern = shape.lineDashPattern
self.fillColor = shape.fillColor
self.strokeColor = shape.strokeColor
self.strokeStart = shape.strokeStart
self.strokeEnd = shape.strokeEnd
self.miterLimit = shape.miterLimit
self.lineWidth = original.lineWidth
self.lineDashPhase = original.lineDashPhase
self.lineCap = original.lineCap
self.lineJoin = original.lineJoin
self.lineDashPattern = original.lineDashPattern
self.fillColor = original.fillColor
self.strokeColor = original.strokeColor
self.strokeStart = original.strokeStart
self.strokeEnd = original.strokeEnd
self.miterLimit = original.miterLimit
updatePath()
adjustToFitPath()
copyViewStyle(original)
ShapeLayer.disableActions = disable
}

Expand Down
43 changes: 41 additions & 2 deletions C4/UI/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,52 @@ public class View: NSObject {
self.view = view
}

public init(copyView: View) {
//If there is a scale transform we need to undo that
let t = CGAffineTransformInvert(copyView.view.transform)
let x = sqrt(t.a * t.a + t.c * t.c)
let y = sqrt(t.b * t.b + t.d * t.d)
let s = CGAffineTransformMakeScale(x, y)
let frame = Rect(CGRectApplyAffineTransform(copyView.view.frame, s))
super.init()
view.frame = CGRect(frame)
copyViewStyle(copyView)
}

public func copyViewStyle(viewToCopy: View) {
ShapeLayer.disableActions = true
anchorPoint = viewToCopy.anchorPoint
shadow = viewToCopy.shadow
border = viewToCopy.border
rotation = viewToCopy.rotation
interactionEnabled = viewToCopy.interactionEnabled
backgroundColor = viewToCopy.backgroundColor
opacity = viewToCopy.opacity

if let maskToCopy = viewToCopy.mask {
if viewToCopy.mask is Shape {
mask = Shape(copy: viewToCopy.mask as! Shape)
} else if viewToCopy.mask is Image {
mask = Image(copy: viewToCopy.mask as! Image)
} else {
mask = View(copyView: maskToCopy)
}
mask?.center = maskToCopy.center
}

transform = viewToCopy.transform
ShapeLayer.disableActions = false
}

/// Initializes a new View with the specifed frame.
/// ````
/// let f = Rect(0,0,100,100)
/// let v = View(frame: f)
/// canvas.add(v)
/// ````
/// - parameter frame: A Rect, which describes the view’s location and size in its superview’s coordinate system.
convenience public init(frame: Rect) {
self.init()
public init(frame: Rect) {
super.init()
self.view.frame = CGRect(frame)
}

Expand Down Expand Up @@ -531,3 +568,5 @@ public class View: NSObject {
zPosition = view.zPosition - 1
}
}