should did action.
This commit is contained in:
parent
c32a0d71b0
commit
d1ee242ca7
@ -9,6 +9,8 @@
|
||||
import MVMCore
|
||||
import UIKit
|
||||
|
||||
public typealias ActionBlockConfirmation = () -> (Bool)
|
||||
|
||||
/**
|
||||
A custom implementation of Apple's UISwitch.
|
||||
|
||||
@ -26,7 +28,10 @@ import UIKit
|
||||
|
||||
public var isAnimated = true
|
||||
|
||||
private var actionBlock: ActionBlock?
|
||||
public var didSwitchAction: ActionBlock?
|
||||
public var shouldSwitchAction: ActionBlockConfirmation? = {
|
||||
return { return true }
|
||||
}()
|
||||
|
||||
// Sizes are from InVision design specs.
|
||||
static let trackSize = CGSize(width: 46, height: 24)
|
||||
@ -131,15 +136,21 @@ import UIKit
|
||||
self.init(frame: .zero)
|
||||
}
|
||||
|
||||
public convenience init(isOn: Bool, actionBlock: ActionBlock?) {
|
||||
public convenience init(isOn: Bool, didSwitchAction: ActionBlock?) {
|
||||
self.init(frame: .zero)
|
||||
self.isOn = isOn
|
||||
self.actionBlock = actionBlock
|
||||
self.didSwitchAction = didSwitchAction
|
||||
}
|
||||
|
||||
public convenience init(actionBlock: ActionBlock?) {
|
||||
public convenience init(didSwitchAction: ActionBlock?) {
|
||||
self.init(frame: .zero)
|
||||
self.actionBlock = actionBlock
|
||||
self.didSwitchAction = didSwitchAction
|
||||
}
|
||||
|
||||
public convenience init(shouldSwitchAction: ActionBlockConfirmation?, didSwitchAction: ActionBlock?) {
|
||||
self.init(frame: .zero)
|
||||
self.didSwitchAction = didSwitchAction
|
||||
self.shouldSwitchAction = shouldSwitchAction
|
||||
}
|
||||
|
||||
public required init?(coder: NSCoder) {
|
||||
@ -184,7 +195,6 @@ import UIKit
|
||||
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
|
||||
@ -193,20 +203,9 @@ import UIKit
|
||||
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 {
|
||||
let trackWidth = Switch.trackSize.width
|
||||
return (MFSizeObject(standardSize: trackWidth, standardiPadPortraitSize: CGFloat(Switch.trackSize.width * 1.5)))?.getValueBasedOnApplicationWidth() ?? trackWidth
|
||||
@ -243,8 +242,11 @@ import UIKit
|
||||
|
||||
/// This will toggle the state of the Switch and execute the actionBlock if provided.
|
||||
public func toggleAndAction() {
|
||||
isOn.toggle()
|
||||
actionBlock?()
|
||||
|
||||
if let result = shouldSwitchAction?(), result {
|
||||
isOn.toggle()
|
||||
didSwitchAction?()
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
@ -258,35 +260,27 @@ import UIKit
|
||||
return area.contains(point)
|
||||
}
|
||||
|
||||
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
||||
|
||||
UIView.animate(withDuration: 0.1, animations: {
|
||||
self.thumbWidthConstraint?.constant += PaddingOne
|
||||
self.layoutIfNeeded()
|
||||
})
|
||||
}
|
||||
|
||||
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
|
||||
|
||||
thumbReformAnimation()
|
||||
|
||||
guard let coordinates: CGPoint = touches.first?.location(in: self),
|
||||
coordinates.x > -20,
|
||||
coordinates.x < bounds.width + 20,
|
||||
coordinates.y > -20,
|
||||
coordinates.y < bounds.height + 20
|
||||
else { return }
|
||||
|
||||
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) {
|
||||
|
||||
@ -320,7 +314,7 @@ extension Switch {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: FormValidationProtocol
|
||||
// MARK: - FormValidationProtocol
|
||||
extension Switch {
|
||||
|
||||
public func isValidField() -> Bool {
|
||||
@ -370,7 +364,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