re-molecule image view

This commit is contained in:
Pfeil, Scott Robert 2019-06-20 16:04:59 -04:00
parent 7e83e0fba9
commit a7f9104bd8
4 changed files with 66 additions and 64 deletions

View File

@ -22,6 +22,7 @@ import UIKit
var edges: UIRectEdge? var edges: UIRectEdge?
var spinnerHeight: CGFloat? var spinnerHeight: CGFloat?
var width: CGFloat? var width: CGFloat?
var height: CGFloat?
var loadingImageName: String? var loadingImageName: String?
var isFallbackImage: Bool = false var isFallbackImage: Bool = false
@ -50,62 +51,24 @@ import UIKit
imageView.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: NSLayoutConstraint.Axis.vertical) imageView.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: NSLayoutConstraint.Axis.vertical)
} }
if let topPin = topPin { if edge.contains(UIRectEdge.top) && edge.contains(UIRectEdge.bottom) {
removeConstraint(topPin) alignFillVertical()
} } else if edge.contains(UIRectEdge.top) {
if edge.contains(UIRectEdge.top) { alignTop()
topPin = imageView.topAnchor.constraint(equalTo: topAnchor) } else if edge.contains(UIRectEdge.bottom) {
alignBottom()
} else { } else {
topPin = imageView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor) alignCenterVertical()
} }
topPin?.isActive = true
if let bottomPin = bottomPin { if edge.contains(UIRectEdge.left) && edge.contains(UIRectEdge.right) {
removeConstraint(bottomPin) alignFillHorizontal()
} } else if edge.contains(UIRectEdge.left) {
if edge.contains(UIRectEdge.bottom) { alignLeft()
bottomPin = bottomAnchor.constraint(equalTo: imageView.bottomAnchor) } else if edge.contains(UIRectEdge.right) {
alignRight()
} else { } else {
bottomPin = bottomAnchor.constraint(greaterThanOrEqualTo: imageView.bottomAnchor) alignCenterHorizontal()
}
bottomPin?.isActive = true
if let leftPin = leftPin {
removeConstraint(leftPin)
}
if edge.contains(UIRectEdge.left) {
leftPin = imageView.leftAnchor.constraint(equalTo: leftAnchor)
} else {
leftPin = imageView.leftAnchor.constraint(greaterThanOrEqualTo: leftAnchor)
}
leftPin?.isActive = true
if let rightPin = rightPin {
removeConstraint(rightPin)
}
if edge.contains(UIRectEdge.right) {
rightPin = rightAnchor.constraint(equalTo: imageView.rightAnchor)
} else {
rightPin = rightAnchor.constraint(greaterThanOrEqualTo: imageView.rightAnchor)
}
rightPin?.isActive = true
// If neither the top or the bottom are pinned, center it.
if let centerY = centerY {
removeConstraint(centerY)
}
if !edge.contains(UIRectEdge.top) && !edge.contains(UIRectEdge.bottom) {
centerY = imageView.centerYAnchor.constraint(equalTo: centerYAnchor)
centerY?.isActive = true
}
// If neither the left or the right are pinned, center it.
if let centerX = centerX {
removeConstraint(centerX)
}
if !edge.contains(UIRectEdge.left) && !edge.contains(UIRectEdge.right) {
centerX = imageView.centerXAnchor.constraint(equalTo: centerXAnchor)
centerX?.isActive = true
} }
} }
@ -121,6 +84,7 @@ import UIKit
// Setup image. // Setup image.
imageView.translatesAutoresizingMaskIntoConstraints = false; imageView.translatesAutoresizingMaskIntoConstraints = false;
addSubview(imageView) addSubview(imageView)
pinView(toSuperView: imageView)
// Setup edges constraints // Setup edges constraints
if edges == nil { if edges == nil {
@ -162,7 +126,27 @@ import UIKit
guard let currentWidth = self.width else { guard let currentWidth = self.width else {
return true return true
} }
return (imageView.image == nil && imageView.animatedImage == nil) || imageName != loadingImageName || !MVMCoreGetterUtility.cgfequal(width, currentWidth) || self.isFallbackImage return (imageView.image == nil && imageView.animatedImage == nil) || imageName != loadingImageName || !MVMCoreGetterUtility.cgfequal(width, currentWidth) || isFallbackImage
}
open func shouldLoadImage(withName imageName: String?, width: CGFloat?, height: CGFloat?) -> Bool {
// We should load a new image if there is no current image, the image names are different, or we are using a fallback image.
if ((imageView.image == nil && imageView.animatedImage == nil) || imageName != loadingImageName || isFallbackImage) {
return true
}
// load new image if the width is different
if let oldWidth = self.width, let newWidth = width, !MVMCoreGetterUtility.cgfequal(oldWidth, newWidth) {
return true
} else if (self.width != nil && width == nil) || (self.width == nil && width != nil) {
return true
}
// load new image if the height is different
if let oldHeight = self.height, let newHeight = height, !MVMCoreGetterUtility.cgfequal(oldHeight, newHeight) {
return true
} else if (self.height != nil && height == nil) || (self.height == nil && height != nil) {
return true
}
return false
} }
// Constrains the image view to be the size provided. Used to size it to the image to fix aspect fit defect. // Constrains the image view to be the size provided. Used to size it to the image to fix aspect fit defect.
@ -191,17 +175,11 @@ import UIKit
imageView.setContentHuggingPriority(UILayoutPriority.defaultLow, for: NSLayoutConstraint.Axis.vertical) imageView.setContentHuggingPriority(UILayoutPriority.defaultLow, for: NSLayoutConstraint.Axis.vertical)
} }
open override func updateView(_ size: CGFloat) { // MARK: - MVMCoreUIMoleculeViewProtocol functions
super.updateView(size) open override func setAsMolecule() {
let width = size.rounded() addSizeConstraintsForAspectRatio = true
if let imageName = json?.optionalStringForKey("image"), shouldLoadImage(withName: imageName, width: width) {
imageView.image = nil
imageView.animatedImage = nil
loadImage(withName: imageName, format: json?.optionalStringForKey("imageFormat"), width: NSNumber(value: Double(width)), height: nil, customFallbackImage: json?.optionalStringForKey("fallbackImage"))
}
} }
// MARK: - MVMCoreUIMoleculeViewProtocol functions
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
if let backgroundColorString = json?.optionalStringForKey(KeyBackgroundColor) { if let backgroundColorString = json?.optionalStringForKey(KeyBackgroundColor) {
@ -212,6 +190,21 @@ import UIKit
imageView.accessibilityTraits = .staticText imageView.accessibilityTraits = .staticText
imageView.isAccessibilityElement = true imageView.isAccessibilityElement = true
} }
let width = json?.optionalCGFloatForKey("width")
let height = json?.optionalCGFloatForKey("height")
if let imageName = json?.optionalStringForKey("image"), shouldLoadImage(withName: imageName, width: width, height: height) {
imageView.image = nil
imageView.animatedImage = nil
var widthNumber: NSNumber?
if let width = width {
widthNumber = NSNumber(value: Double(width))
}
var heightNumber: NSNumber?
if let height = height {
heightNumber = NSNumber(value: Double(height))
}
loadImage(withName: imageName, format: json?.optionalStringForKey("imageFormat"), width: widthNumber, height: heightNumber, customFallbackImage: json?.optionalStringForKey("fallbackImage"))
}
} }
// MARK: - load functions // MARK: - load functions
@ -221,6 +214,9 @@ import UIKit
if let width = width { if let width = width {
self.width = width.cgfloat() self.width = width.cgfloat()
} }
if let height = height {
self.height = height.cgfloat()
}
self.loadingSpinner.resumeSpinnerAfterDelay() self.loadingSpinner.resumeSpinnerAfterDelay()
if let height = self.spinnerHeight { if let height = self.spinnerHeight {
self.loadingSpinnerHeightConstraint?.constant = height self.loadingSpinnerHeightConstraint?.constant = height

View File

@ -163,7 +163,7 @@ public class MoleculeStackView: ViewConstrainingView {
alignVertical(ViewConstrainingView.getAlignmentFor(json?.optionalStringForKey("alignment"), defaultAlignment: .fill)) alignVertical(ViewConstrainingView.getAlignmentFor(json?.optionalStringForKey("alignment"), defaultAlignment: .fill))
} else { } else {
alignHorizontal(ViewConstrainingView.getAlignmentFor(json?.optionalStringForKey("alignment"), defaultAlignment: .fill)) alignHorizontal(ViewConstrainingView.getAlignmentFor(json?.optionalStringForKey("alignment"), defaultAlignment: .fill))
alignVertical(.leading) alignVertical(.fill)
} }
// Adds the molecules and sets the json. // Adds the molecules and sets the json.

View File

@ -105,6 +105,11 @@ import UIKit
} }
} else { } else {
molecule?.setWithJSON(moleculeJSON, delegateObject: delegateObject, additionalData: additionalData) molecule?.setWithJSON(moleculeJSON, delegateObject: delegateObject, additionalData: additionalData)
if let castView = molecule as? MVMCoreUIViewConstrainingProtocol {
let standardConstraints = castView.useStandardConstraints?() ?? true
castView.shouldSetHorizontalMargins?(!standardConstraints)
castView.shouldSetVerticalMargins?(!standardConstraints)
}
} }
backgroundColor = molecule?.backgroundColor backgroundColor = molecule?.backgroundColor

View File

@ -42,7 +42,8 @@
@"checkbox": MVMCoreUICheckBox.class, @"checkbox": MVMCoreUICheckBox.class,
@"listItem": MoleculeTableViewCell.class, @"listItem": MoleculeTableViewCell.class,
@"switchLineItem": SwitchLineItem.class, @"switchLineItem": SwitchLineItem.class,
@"switch": Switch.class @"switch": Switch.class,
@"image": MFLoadImageView.class
} mutableCopy]; } mutableCopy];
}); });
return mapping; return mapping;