good working state.
This commit is contained in:
parent
ab0ff64677
commit
c32a0d71b0
@ -26,17 +26,12 @@ import UIKit
|
||||
|
||||
public var isAnimated = true
|
||||
|
||||
private var valueShouldChange = true
|
||||
private var shouldTouchToSwitch = true
|
||||
|
||||
private var actionBlock: ActionBlock?
|
||||
|
||||
// Sizes are from InVision design specs.
|
||||
static let trackSize = CGSize(width: 46, height: 24)
|
||||
static let thumbSize = CGSize(width: 22, height: 22)
|
||||
|
||||
static let shakeIntensity: CGFloat = 2
|
||||
|
||||
private var thumbView: View = {
|
||||
let view = View()
|
||||
view.backgroundColor = .white
|
||||
@ -56,6 +51,12 @@ import UIKit
|
||||
}
|
||||
}
|
||||
|
||||
public var isLocked: Bool = false {
|
||||
didSet {
|
||||
isUserInteractionEnabled = !isLocked
|
||||
}
|
||||
}
|
||||
|
||||
open var isOn: Bool = false {
|
||||
didSet {
|
||||
isSelected = isOn
|
||||
@ -73,21 +74,10 @@ import UIKit
|
||||
|
||||
}, completion: nil)
|
||||
|
||||
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.3, options: [], animations: {
|
||||
if self.isOn {
|
||||
self.thumbLeadingConstraint?.isActive = true
|
||||
self.thumbTrailingConstraint?.isActive = false
|
||||
self.relativeThumbLeadingConstraint?.isActive = false
|
||||
self.relativeThumbTrailingConstraint?.isActive = true
|
||||
|
||||
} else {
|
||||
self.thumbLeadingConstraint?.isActive = false
|
||||
self.thumbTrailingConstraint?.isActive = true
|
||||
self.relativeThumbLeadingConstraint?.isActive = true
|
||||
self.relativeThumbTrailingConstraint?.isActive = false
|
||||
}
|
||||
UIView.animate(withDuration: 0.33, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.2, options: [], animations: {
|
||||
|
||||
self.thumbWidthConstraint?.constant = Switch.getThumbWidth() + PaddingOne
|
||||
self.constrainThumb()
|
||||
self.thumbWidthConstraint?.constant = Switch.getThumbWidth()
|
||||
self.layoutIfNeeded()
|
||||
|
||||
}, completion: nil)
|
||||
@ -95,6 +85,7 @@ import UIKit
|
||||
} else {
|
||||
backgroundColor = isOn ? trackTintColor?.on : trackTintColor?.off
|
||||
thumbView.backgroundColor = isOn ? thumbTintColor?.on : thumbTintColor?.off
|
||||
self.constrainThumb()
|
||||
}
|
||||
|
||||
FormValidator.enableByValidationWith(delegate: delegateObject?.formValidationProtocol)
|
||||
@ -116,14 +107,17 @@ import UIKit
|
||||
|
||||
private var thumbLeadingConstraint: NSLayoutConstraint?
|
||||
private var thumbTrailingConstraint: NSLayoutConstraint?
|
||||
private var relativeThumbLeadingConstraint: NSLayoutConstraint?
|
||||
private var relativeThumbTrailingConstraint: NSLayoutConstraint?
|
||||
|
||||
private var thumbHeightConstraint: NSLayoutConstraint?
|
||||
private var thumbWidthConstraint: NSLayoutConstraint?
|
||||
private var heightConstraint: NSLayoutConstraint?
|
||||
private var widthConstraint: NSLayoutConstraint?
|
||||
|
||||
private func constrainThumb() {
|
||||
|
||||
self.thumbLeadingConstraint?.isActive = isOn
|
||||
self.thumbTrailingConstraint?.isActive = !isOn
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Initializers
|
||||
//--------------------------------------------------
|
||||
@ -186,35 +180,51 @@ import UIKit
|
||||
|
||||
addSubview(thumbView)
|
||||
|
||||
thumbView.heightAnchor.constraint(equalToConstant: Switch.thumbSize.height).isActive = true
|
||||
thumbView.widthAnchor.constraint(equalToConstant: Switch.thumbSize.width).isActive = true
|
||||
thumbHeightConstraint = thumbView.heightAnchor.constraint(equalToConstant: Switch.thumbSize.height)
|
||||
thumbHeightConstraint?.isActive = true
|
||||
thumbWidthConstraint = thumbView.widthAnchor.constraint(equalToConstant: Switch.thumbSize.width)
|
||||
thumbWidthConstraint?.isActive = true
|
||||
|
||||
thumbView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
|
||||
thumbView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor).isActive = true
|
||||
bottomAnchor.constraint(greaterThanOrEqualTo: thumbView.bottomAnchor).isActive = true
|
||||
|
||||
thumbTrailingConstraint = trailingAnchor.constraint(equalTo: thumbView.trailingAnchor, constant: 1)
|
||||
relativeThumbTrailingConstraint = trailingAnchor.constraint(greaterThanOrEqualTo: thumbView.trailingAnchor)
|
||||
relativeThumbTrailingConstraint?.isActive = true
|
||||
|
||||
relativeThumbLeadingConstraint = thumbView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor)
|
||||
thumbLeadingConstraint = thumbView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 1)
|
||||
thumbLeadingConstraint?.isActive = true
|
||||
|
||||
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(stretchThumb))
|
||||
addGestureRecognizer(longPress)
|
||||
|
||||
accessibilityLabel = MVMCoreUIUtility.hardcodedString(withKey: "Switch_buttonlabel")
|
||||
}
|
||||
|
||||
@objc private func stretchThumb() {
|
||||
|
||||
UIView.animate(withDuration: 0.1, animations: {
|
||||
self.thumbWidthConstraint?.constant = 26
|
||||
self.layoutIfNeeded()
|
||||
})
|
||||
}
|
||||
|
||||
class func getTrackWidth() -> CGFloat {
|
||||
return (MFSizeObject(standardSize: Switch.trackSize.width, standardiPadPortraitSize: CGFloat(Switch.trackSize.width * 1.5)))!.getValueBasedOnApplicationWidth()
|
||||
let trackWidth = Switch.trackSize.width
|
||||
return (MFSizeObject(standardSize: trackWidth, standardiPadPortraitSize: CGFloat(Switch.trackSize.width * 1.5)))?.getValueBasedOnApplicationWidth() ?? trackWidth
|
||||
}
|
||||
|
||||
class func getTrackHeight() -> CGFloat {
|
||||
return (MFSizeObject(standardSize: Switch.trackSize.height, standardiPadPortraitSize: CGFloat(Switch.trackSize.height * 1.5) ))!.getValueBasedOnApplicationWidth()
|
||||
let trackHeight = Switch.trackSize.height
|
||||
return (MFSizeObject(standardSize: trackHeight, standardiPadPortraitSize: CGFloat(Switch.trackSize.height * 1.5)))?.getValueBasedOnApplicationWidth() ?? trackHeight
|
||||
}
|
||||
|
||||
class func getThumbWidth() -> CGFloat {
|
||||
return (MFSizeObject(standardSize: Switch.thumbSize.width, standardiPadPortraitSize: CGFloat(Switch.thumbSize.width * 1.5)))!.getValueBasedOnApplicationWidth()
|
||||
let thumbWidth = Switch.thumbSize.width
|
||||
return (MFSizeObject(standardSize: thumbWidth, standardiPadPortraitSize: CGFloat(Switch.thumbSize.width * 1.5)))?.getValueBasedOnApplicationWidth() ?? thumbWidth
|
||||
}
|
||||
|
||||
class func getThumbHeight() -> CGFloat {
|
||||
return (MFSizeObject(standardSize: Switch.thumbSize.height, standardiPadPortraitSize: CGFloat(Switch.thumbSize.height * 1.5)))!.getValueBasedOnApplicationWidth()
|
||||
let thumbHeight = Switch.thumbSize.width
|
||||
return (MFSizeObject(standardSize: thumbHeight, standardiPadPortraitSize: CGFloat(Switch.thumbSize.height * 1.5)))?.getValueBasedOnApplicationWidth() ?? thumbHeight
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
@ -248,22 +258,35 @@ import UIKit
|
||||
return area.contains(point)
|
||||
}
|
||||
|
||||
public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
|
||||
|
||||
UIView.animate(withDuration: 0.1, animations: {
|
||||
self.thumbWidthConstraint?.constant += PaddingOne
|
||||
self.layoutIfNeeded()
|
||||
})
|
||||
|
||||
sendActions(for: .touchDown)
|
||||
}
|
||||
|
||||
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
|
||||
|
||||
thumbReformAnimation()
|
||||
valueShouldChange = true
|
||||
sendActions(for: .touchUpInside)
|
||||
}
|
||||
/*
|
||||
open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
|
||||
|
||||
if shouldTouchToSwitch {
|
||||
if touchMoves(toLeft: touches) {
|
||||
thumbMoveAnitmationTo(on: false)
|
||||
thumbShakeAnitmationTo(on: false)
|
||||
thumbReformAnimation(true)
|
||||
} else {
|
||||
thumbMoveAnitmationTo(on: true)
|
||||
thumbShakeAnitmationTo(on: true)
|
||||
thumbReformAnimation(true)
|
||||
}
|
||||
}
|
||||
|
||||
if touchIsOutSide(touches) {
|
||||
sendActions(for: .touchDragOutside)
|
||||
if !shouldTouchToSwitch {
|
||||
thumbReformAnimation(true)
|
||||
}
|
||||
} else {
|
||||
sendActions(for: .touchDragInside)
|
||||
}
|
||||
}*/
|
||||
|
||||
public func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
|
||||
|
||||
@ -347,7 +370,7 @@ extension Switch {
|
||||
}
|
||||
|
||||
if let actionMap = dictionary.optionalDictionaryForKey("actionMap") {
|
||||
// actionBlock = { MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) }
|
||||
actionBlock = { MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) }
|
||||
}
|
||||
|
||||
if let isAnimated = dictionary["state"] as? Bool {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user