Commit 0ff4ab03 by Daniel Dahan

added contentsGravityPreset where appropriate

parent 77e68079
......@@ -63,7 +63,7 @@ class FeedViewController: UIViewController {
menuButton.pulseScale = false
menuButton.setImage(image, forState: .Normal)
menuButton.setImage(image, forState: .Highlighted)
menuButton.addTarget(self, action: "handleMenuButton", forControlEvents: .TouchUpInside)
// menuButton.addTarget(self, action: "handleMenuButton", forControlEvents: .TouchUpInside)
// Switch control.
let switchControl: MaterialSwitch = MaterialSwitch(state: .Off, style: .LightContent, size: .Small)
......@@ -75,7 +75,7 @@ class FeedViewController: UIViewController {
searchButton.pulseScale = false
searchButton.setImage(image, forState: .Normal)
searchButton.setImage(image, forState: .Highlighted)
searchButton.addTarget(self, action: "handleSearchButton", forControlEvents: .TouchUpInside)
// searchButton.addTarget(self, action: "handleSearchButton", forControlEvents: .TouchUpInside)
navigationController?.navigationBar.leftControls = [menuButton]
navigationController?.navigationBar.rightControls = [switchControl, searchButton]
......
......@@ -109,7 +109,7 @@ class ViewController: UIViewController {
cardView.pulseColor = nil
cardView.image = UIImage(named: "Material-iTunesArtWork")?.resize(toHeight: 150)
cardView.contentsGravity = .BottomRight
cardView.contentsGravityPreset = .BottomRight
// Title label.
let titleLabel: UILabel = UILabel()
......@@ -154,7 +154,7 @@ class ViewController: UIViewController {
// Image.
cardView.image = UIImage(named: "Graph")?.resize(toHeight: 150)
cardView.contentsGravity = .Right
cardView.contentsGravityPreset = .Right
// Title label.
let titleLabel: UILabel = UILabel()
......
......@@ -191,7 +191,7 @@ class ViewController: UIViewController {
var image: UIImage? = UIImage(named: "VeganCakeFull")
let imageView: MaterialView = MaterialView()
imageView.image = image
imageView.contentsGravity = .ResizeAspectFill
imageView.contentsGravityPreset = .ResizeAspectFill
cardView.addSubview(imageView)
let contentView: MaterialView = MaterialView()
......@@ -288,7 +288,7 @@ class ViewController: UIViewController {
var image: UIImage? = UIImage(named: "CosmicMindInverted")
let imageView: MaterialView = MaterialView()
imageView.image = image
imageView.contentsGravity = .ResizeAspectFill
imageView.contentsGravityPreset = .ResizeAspectFill
cardView.addSubview(imageView)
let contentView: MaterialView = MaterialView()
......@@ -387,17 +387,17 @@ class ViewController: UIViewController {
let leftImageView: MaterialView = MaterialView()
leftImageView.image = image
leftImageView.contentsGravity = .ResizeAspectFill
leftImageView.contentsGravityPreset = .ResizeAspectFill
cardView.addSubview(leftImageView)
let topImageView: MaterialView = MaterialView()
topImageView.image = image
topImageView.contentsGravity = .ResizeAspectFill
topImageView.contentsGravityPreset = .ResizeAspectFill
cardView.addSubview(topImageView)
let bottomImageView: MaterialView = MaterialView()
bottomImageView.image = image
bottomImageView.contentsGravity = .ResizeAspectFill
bottomImageView.contentsGravityPreset = .ResizeAspectFill
cardView.addSubview(bottomImageView)
let contentView: MaterialView = MaterialView()
......
......@@ -100,7 +100,7 @@ class ViewController: UIViewController {
// Image.
secondCardView.image = UIImage(named: "Graph")?.resize(toHeight: 100)
secondCardView.contentsGravity = .TopLeft
secondCardView.contentsGravityPreset = .TopLeft
// Title label.
let titleLabel: UILabel = UILabel()
......
......@@ -152,13 +152,14 @@ public class ImageCardView : MaterialPulseView {
}
}
/**
:name: contentsGravity
*/
public override var contentsGravity: MaterialGravity {
didSet {
/// Determines how content should be aligned within the visualLayer's bounds.
public override var contentsGravity: String {
get {
return nil == imageLayer ? "" : imageLayer!.contentsGravity
}
set(value) {
prepareImageLayer()
imageLayer?.contentsGravity = MaterialGravityToString(contentsGravity)
imageLayer?.contentsGravity = value
}
}
......
......@@ -33,6 +33,28 @@ import UIKit
@objc(MaterialCollectionViewCell)
public class MaterialCollectionViewCell : UICollectionViewCell {
/**
A CAShapeLayer used to manage elements that would be affected by
the clipToBounds property of the backing layer. For example, this
allows the dropshadow effect on the backing layer, while clipping
the image to a desired shape within the visualLayer.
*/
public private(set) lazy var visualLayer: CAShapeLayer = CAShapeLayer()
/**
A base delegate reference used when subclassing MaterialView.
*/
public weak var delegate: MaterialDelegate?
/// Sets whether the scaling animation should be used.
public lazy var pulseScale: Bool = true
/// The opcaity value for the pulse animation.
public var pulseColorOpacity: CGFloat = 0.25
/// The color of the pulse effect.
public var pulseColor: UIColor?
/**
A property that manages an image for the visualLayer's contents
property. Images should not be set to the backing layer's contents
property to avoid conflicts when using clipsToBounds.
......@@ -43,10 +65,62 @@ public class MaterialCollectionViewCell : UICollectionViewCell {
}
}
/// Determines how content should be aligned within the visualLayer's bounds.
public var contentsGravity: MaterialGravity {
/**
Allows a relative subrectangle within the range of 0 to 1 to be
specified for the visualLayer's contents property. This allows
much greater flexibility than the contentsGravity property in
terms of how the image is cropped and stretched.
*/
public var contentsRect: CGRect {
get {
return visualLayer.contentsRect
}
set(value) {
visualLayer.contentsRect = value
}
}
/**
A CGRect that defines a stretchable region inside the visualLayer
with a fixed border around the edge.
*/
public var contentsCenter: CGRect {
get {
return visualLayer.contentsCenter
}
set(value) {
visualLayer.contentsCenter = value
}
}
/**
A floating point value that defines a ratio between the pixel
dimensions of the visualLayer's contents property and the size
of the view. By default, this value is set to the MaterialDevice.scale.
*/
public var contentsScale: CGFloat {
get {
return visualLayer.contentsScale
}
set(value) {
visualLayer.contentsScale = value
}
}
/// A Preset for the contentsGravity property.
public var contentsGravityPreset: MaterialGravity {
didSet {
visualLayer.contentsGravity = MaterialGravityToString(contentsGravity)
contentsGravity = MaterialGravityToString(contentsGravityPreset)
}
}
/// Determines how content should be aligned within the visualLayer's bounds.
public var contentsGravity: String {
get {
return visualLayer.contentsGravity
}
set(value) {
visualLayer.contentsGravity = value
}
}
......@@ -88,28 +162,6 @@ public class MaterialCollectionViewCell : UICollectionViewCell {
}
/**
A CAShapeLayer used to manage elements that would be affected by
the clipToBounds property of the backing layer. For example, this
allows the dropshadow effect on the backing layer, while clipping
the image to a desired shape within the visualLayer.
*/
public private(set) lazy var visualLayer: CAShapeLayer = CAShapeLayer()
/**
A base delegate reference used when subclassing MaterialView.
*/
public weak var delegate: MaterialDelegate?
/// Sets whether the scaling animation should be used.
public lazy var pulseScale: Bool = true
/// The opcaity value for the pulse animation.
public var pulseColorOpacity: CGFloat = 0.25
/// The color of the pulse effect.
public var pulseColor: UIColor?
/**
This property is the same as clipsToBounds. It crops any of the view's
contents from bleeding past the view's frame. If an image is set using
the image property, then this value does not need to be set, since the
......@@ -360,7 +412,7 @@ public class MaterialCollectionViewCell : UICollectionViewCell {
depth = .None
cornerRadiusPreset = .None
shape = .None
contentsGravity = .ResizeAspectFill
contentsGravityPreset = .ResizeAspectFill
super.init(coder: aDecoder)
prepareView()
}
......@@ -375,7 +427,7 @@ public class MaterialCollectionViewCell : UICollectionViewCell {
depth = .None
cornerRadiusPreset = .None
shape = .None
contentsGravity = .ResizeAspectFill
contentsGravityPreset = .ResizeAspectFill
super.init(frame: frame)
prepareView()
}
......
......@@ -76,4 +76,14 @@ public struct MaterialDevice {
public static var scale: CGFloat {
return UIScreen.mainScreen().scale
}
/// Retrieves the device status bar style.
public static var statusBarStyle: UIStatusBarStyle {
get {
return UIApplication.sharedApplication().statusBarStyle
}
set(value) {
UIApplication.sharedApplication().statusBarStyle = value
}
}
}
\ No newline at end of file
......@@ -143,6 +143,13 @@ public class MaterialLayer : CAShapeLayer {
}
}
/// A Preset for the contentsGravity property.
public var contentsGravityPreset: MaterialGravity {
didSet {
contentsGravity = MaterialGravityToString(contentsGravityPreset)
}
}
/// Determines how content should be aligned within the visualLayer's bounds.
public override var contentsGravity: String {
get {
......@@ -236,6 +243,7 @@ public class MaterialLayer : CAShapeLayer {
- Parameter aDecoder: A NSCoder instance.
*/
public required init?(coder aDecoder: NSCoder) {
contentsGravityPreset = .ResizeAspectFill
super.init(coder: aDecoder)
prepareVisualLayer()
}
......@@ -246,12 +254,14 @@ public class MaterialLayer : CAShapeLayer {
- Parameter layer: AnyObject.
*/
public override init(layer: AnyObject) {
contentsGravityPreset = .ResizeAspectFill
super.init()
prepareVisualLayer()
}
/// A convenience initializer.
public override init() {
contentsGravityPreset = .ResizeAspectFill
super.init()
prepareVisualLayer()
}
......
......@@ -63,8 +63,11 @@ public class MaterialView : UIView {
terms of how the image is cropped and stretched.
*/
public var contentsRect: CGRect {
didSet {
visualLayer.contentsRect = contentsRect
get {
return visualLayer.contentsRect
}
set(value) {
visualLayer.contentsRect = value
}
}
......@@ -73,8 +76,11 @@ public class MaterialView : UIView {
with a fixed border around the edge.
*/
public var contentsCenter: CGRect {
didSet {
visualLayer.contentsCenter = contentsCenter
get {
return visualLayer.contentsCenter
}
set(value) {
visualLayer.contentsCenter = value
}
}
......@@ -84,15 +90,28 @@ public class MaterialView : UIView {
of the view. By default, this value is set to the MaterialDevice.scale.
*/
public var contentsScale: CGFloat {
get {
return visualLayer.contentsScale
}
set(value) {
visualLayer.contentsScale = value
}
}
/// A Preset for the contentsGravity property.
public var contentsGravityPreset: MaterialGravity {
didSet {
visualLayer.contentsScale = contentsScale
contentsGravity = MaterialGravityToString(contentsGravityPreset)
}
}
/// Determines how content should be aligned within the visualLayer's bounds.
public var contentsGravity: MaterialGravity {
didSet {
visualLayer.contentsGravity = MaterialGravityToString(contentsGravity)
public var contentsGravity: String {
get {
return visualLayer.contentsGravity
}
set(value) {
visualLayer.contentsGravity = value
}
}
......@@ -344,10 +363,7 @@ public class MaterialView : UIView {
- Parameter aDecoder: A NSCoder instance.
*/
public required init?(coder aDecoder: NSCoder) {
contentsRect = CGRectMake(0, 0, 1, 1)
contentsCenter = CGRectMake(0, 0, 1, 1)
contentsScale = MaterialDevice.scale
contentsGravity = .ResizeAspectFill
contentsGravityPreset = .ResizeAspectFill
super.init(coder: aDecoder)
prepareView()
}
......@@ -359,10 +375,7 @@ public class MaterialView : UIView {
- Parameter frame: A CGRect instance.
*/
public override init(frame: CGRect) {
contentsRect = CGRectMake(0, 0, 1, 1)
contentsCenter = CGRectMake(0, 0, 1, 1)
contentsScale = MaterialDevice.scale
contentsGravity = .ResizeAspectFill
contentsGravityPreset = .ResizeAspectFill
super.init(frame: frame)
prepareView()
}
......
......@@ -33,9 +33,13 @@ import UIKit
/// A memory reference to the Grid instance for UIView extensions.
private var NavigationBarKey: UInt8 = 0
public class Controls {
public class NavigationBarControls {
/// A preset wrapper around contentInset.
public var contentInsetPreset: MaterialEdgeInset
public var contentInsetPreset: MaterialEdgeInset {
didSet {
contentInset = MaterialEdgeInsetToValue(contentInsetPreset)
}
}
/// A wrapper around grid.contentInset.
public var contentInset: UIEdgeInsets
......@@ -47,17 +51,17 @@ public class Controls {
public var rightControls: Array<UIControl>?
public init() {
contentInsetPreset = .Square3
contentInset = MaterialEdgeInsetToValue(.Square3)
contentInsetPreset = .Square2
contentInset = MaterialEdgeInsetToValue(.Square2)
}
}
public extension UINavigationBar {
/// Controls reference.
public var controls: Controls {
/// NavigationBarControls reference.
public var controls: NavigationBarControls {
get {
return MaterialObjectAssociatedObject(self, key: &NavigationBarKey) {
return Controls()
return NavigationBarControls()
}
}
set(value) {
......@@ -68,10 +72,10 @@ public extension UINavigationBar {
/// Device status bar style.
public var statusBarStyle: UIStatusBarStyle {
get {
return UIApplication.sharedApplication().statusBarStyle
return MaterialDevice.statusBarStyle
}
set(value) {
UIApplication.sharedApplication().statusBarStyle = value
MaterialDevice.statusBarStyle = value
}
}
......@@ -83,13 +87,11 @@ public extension UINavigationBar {
var c: Array<UIBarButtonItem> = Array<UIBarButtonItem>()
if let v: Array<UIControl> = value {
for q in v {
q.frame.size = CGSizeMake(48, 32)
if let p: UIButton = q as? UIButton {
p.frame.size = CGSizeMake(40, 28)
p.contentEdgeInsets = UIEdgeInsetsZero
c.append(UIBarButtonItem(customView: p))
} else {
c.append(UIBarButtonItem(customView: q))
}
c.append(UIBarButtonItem(customView: q))
}
}
......@@ -110,13 +112,11 @@ public extension UINavigationBar {
var c: Array<UIBarButtonItem> = Array<UIBarButtonItem>()
if let v: Array<UIControl> = value {
for q in v {
q.frame.size = CGSizeMake(48, 32)
if let p: UIButton = q as? UIButton {
p.frame.size = CGSizeMake(40, 28)
p.contentEdgeInsets = UIEdgeInsetsZero
c.append(UIBarButtonItem(customView: p))
} else {
c.append(UIBarButtonItem(customView: q))
}
c.append(UIBarButtonItem(customView: q))
}
}
......@@ -131,23 +131,133 @@ public extension UINavigationBar {
}
public class NavigationBar : UINavigationBar {
/**
A CAShapeLayer used to manage elements that would be affected by
the clipToBounds property of the backing layer. For example, this
allows the dropshadow effect on the backing layer, while clipping
the image to a desired shape within the visualLayer.
*/
public private(set) lazy var visualLayer: CAShapeLayer = CAShapeLayer()
/**
A property that manages an image for the visualLayer's contents
property. Images should not be set to the backing layer's contents
property to avoid conflicts when using clipsToBounds.
*/
public var image: UIImage? {
didSet {
visualLayer.contents = image?.CGImage
}
}
/**
Allows a relative subrectangle within the range of 0 to 1 to be
specified for the visualLayer's contents property. This allows
much greater flexibility than the contentsGravity property in
terms of how the image is cropped and stretched.
*/
public var contentsRect: CGRect {
get {
return visualLayer.contentsRect
}
set(value) {
visualLayer.contentsRect = value
}
}
/**
A CGRect that defines a stretchable region inside the visualLayer
with a fixed border around the edge.
*/
public var contentsCenter: CGRect {
get {
return visualLayer.contentsCenter
}
set(value) {
visualLayer.contentsCenter = value
}
}
/**
A floating point value that defines a ratio between the pixel
dimensions of the visualLayer's contents property and the size
of the view. By default, this value is set to the MaterialDevice.scale.
*/
public var contentsScale: CGFloat {
get {
return visualLayer.contentsScale
}
set(value) {
visualLayer.contentsScale = value
}
}
/// A Preset for the contentsGravity property.
public var contentsGravityPreset: MaterialGravity {
didSet {
contentsGravity = MaterialGravityToString(contentsGravityPreset)
}
}
/// Determines how content should be aligned within the visualLayer's bounds.
public var contentsGravity: String {
get {
return visualLayer.contentsGravity
}
set(value) {
visualLayer.contentsGravity = value
}
}
/**
This property is the same as clipsToBounds. It crops any of the view's
contents from bleeding past the view's frame. If an image is set using
the image property, then this value does not need to be set, since the
visualLayer's maskToBounds is set to true by default.
*/
public var masksToBounds: Bool {
get {
return layer.masksToBounds
}
set(value) {
layer.masksToBounds = value
}
}
/// A preset wrapper around contentInset.
public var contentInsetPreset: MaterialEdgeInset {
get {
return controls.contentInsetPreset
return grid.contentInsetPreset
}
set(value) {
controls.contentInsetPreset = value
grid.contentInsetPreset = value
}
}
/// A wrapper around grid.contentInset.
public var contentInset: UIEdgeInsets {
get {
return controls.contentInset
return grid.contentInset
}
set(value) {
grid.contentInset = value
}
}
/// A preset wrapper around spacing.
public var spacingPreset: MaterialSpacing = .None {
didSet {
spacing = MaterialSpacingToValue(spacingPreset)
}
}
/// A wrapper around grid.spacing.
public var spacing: CGFloat {
get {
return grid.spacing
}
set(value) {
controls.contentInset = value
grid.spacing = value
}
}
......@@ -172,6 +282,56 @@ public class NavigationBar : UINavigationBar {
}
}
/// A property that accesses the layer.frame.origin.x property.
public var x: CGFloat {
get {
return layer.frame.origin.x
}
set(value) {
layer.frame.origin.x = value
}
}
/// A property that accesses the layer.frame.origin.y property.
public var y: CGFloat {
get {
return layer.frame.origin.y
}
set(value) {
layer.frame.origin.y = value
}
}
/**
A property that accesses the layer.frame.origin.width property.
When setting this property in conjunction with the shape property having a
value that is not .None, the height will be adjusted to maintain the correct
shape.
*/
public var width: CGFloat {
get {
return layer.frame.size.width
}
set(value) {
layer.frame.size.width = value
}
}
/**
A property that accesses the layer.frame.origin.height property.
When setting this property in conjunction with the shape property having a
value that is not .None, the width will be adjusted to maintain the correct
shape.
*/
public var height: CGFloat {
get {
return layer.frame.size.height
}
set(value) {
layer.frame.size.height = value
}
}
/// A property that accesses the backing layer's shadowColor.
public var shadowColor: UIColor? {
didSet {
......@@ -223,7 +383,7 @@ public class NavigationBar : UINavigationBar {
public var shadowPathAutoSizeEnabled: Bool = false {
didSet {
if shadowPathAutoSizeEnabled {
// layoutShadowPath()
layoutShadowPath()
} else {
shadowPath = nil
}
......@@ -241,16 +401,86 @@ public class NavigationBar : UINavigationBar {
shadowOffset = value.offset
shadowOpacity = value.opacity
shadowRadius = value.radius
// layoutShadowPath()
layoutShadowPath()
}
}
/// A property that sets the cornerRadius of the backing layer.
public var cornerRadiusPreset: MaterialRadius = .None {
didSet {
if let v: MaterialRadius = cornerRadiusPreset {
cornerRadius = MaterialRadiusToValue(v)
}
}
}
/// A property that accesses the layer.cornerRadius.
public var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set(value) {
layer.cornerRadius = value
layoutShadowPath()
}
}
/// A preset property to set the borderWidth.
public var borderWidthPreset: MaterialBorder = .None {
didSet {
borderWidth = MaterialBorderToValue(borderWidthPreset)
}
}
/// A property that accesses the layer.borderWith.
public var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set(value) {
layer.borderWidth = value
}
}
/// A property that accesses the layer.borderColor property.
public var borderColor: UIColor? {
get {
return nil == layer.borderColor ? nil : UIColor(CGColor: layer.borderColor!)
}
set(value) {
layer.borderColor = value?.CGColor
}
}
/// A property that accesses the layer.position property.
public var position: CGPoint {
get {
return layer.position
}
set(value) {
layer.position = value
}
}
/// A property that accesses the layer.zPosition property.
public var zPosition: CGFloat {
get {
return layer.zPosition
}
set(value) {
layer.zPosition = value
}
}
public required init?(coder aDecoder: NSCoder) {
contentsGravityPreset = .ResizeAspectFill
super.init(coder: aDecoder)
prepareView()
}
public override init(frame: CGRect) {
contentsGravityPreset = .ResizeAspectFill
super.init(frame: frame)
prepareView()
}
......@@ -261,11 +491,34 @@ public class NavigationBar : UINavigationBar {
backButtonImage = nil
backgroundColor = MaterialColor.white
depth = .Depth1
spacingPreset = .Spacing2
contentInsetPreset = .Square2
titleTextAttributes = [NSFontAttributeName: RobotoFont.regularWithSize(20)]
prepareVisualLayer()
backgroundColor = MaterialColor.white
}
/// Prepares the visualLayer property.
internal func prepareVisualLayer() {
visualLayer.zPosition = 0
visualLayer.masksToBounds = true
layer.addSublayer(visualLayer)
}
/// Manages the layout for the visualLayer property.
internal func layoutVisualLayer() {
visualLayer.frame = bounds
visualLayer.cornerRadius = cornerRadius
}
public func reloadView() {
leftControls = controls.leftControls
rightControls = controls.rightControls
/// Sets the shadow path.
internal func layoutShadowPath() {
if shadowPathAutoSizeEnabled {
if .None == depth {
shadowPath = nil
} else if nil == shadowPath {
shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).CGPath
}
}
}
}
......@@ -56,11 +56,4 @@ public class NavigationController : UINavigationController {
public func prepareView() {
navigationItem.title = "Title"
}
public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let v: NavigationBar = navigationBar as? NavigationBar {
v.reloadView()
}
}
}
......@@ -41,9 +41,12 @@ public class StatusBarView : ControlView {
public var heightForLandscapeOrientation: CGFloat = 44
/// Device status bar style.
public var statusBarStyle: UIStatusBarStyle = UIApplication.sharedApplication().statusBarStyle {
didSet {
UIApplication.sharedApplication().statusBarStyle = statusBarStyle
public var statusBarStyle: UIStatusBarStyle {
get {
return MaterialDevice.statusBarStyle
}
set(value) {
MaterialDevice.statusBarStyle = value
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment