Objective-C 翻译成 Swift:绑定宏?

Objective-C Translation to Swift: Bound Macros?

我正在查看 this tutorial about how to make double sliders while also learning how to translate from ObjC to Swift (I only know Swift really but I'm learning a little about ObjC), it's going pretty well but I'm stuck at this one point trying to translate this part to swift. I don't know what a BOUND Macro is for one thing, it's not in any guides like this one,另外我不知道如何将其翻译成 Swift。

这是 objective C 中的代码:

#define BOUND(VALUE, UPPER, LOWER)  MIN(MAX(VALUE, LOWER), UPPER)

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:self];

    // 1. determine by how much the user has dragged
    float delta = touchPoint.x - _previousTouchPoint.x;
    float valueDelta = (_maximumValue - _minimumValue) * delta / _useableTrackLength;

    _previousTouchPoint = touchPoint;

    // 2. update the values
    if (_lowerKnobLayer.highlighted)
    {
        _lowerValue += valueDelta;
        _lowerValue = BOUND(_lowerValue, _upperValue, _minimumValue);
    }
    if (_upperKnobLayer.highlighted)
    {
        _upperValue += valueDelta;
        _upperValue = BOUND(_upperValue, _maximumValue, _lowerValue);
    }

    // 3. Update the UI state
    [CATransaction begin];
    [CATransaction setDisableActions:YES] ;

    [self setLayerFrames];

    [CATransaction commit];

    return YES;
}

这是我在 swift 中的代码:

override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
        var touchPoint: CGPoint = touch.locationInView(self)

        //Determine how much the user has dragged
        var delta: CGFloat = touchPoint.x - previousTouchPoint.x
        var valueDelta: CGFloat = (maximumValue - minimumValue) * delta / usableTrackLength

        previousTouchPoint = touchPoint

        //Update the values
        if lowerKnobLayer.highlighted {
            lowerValue += valueDelta
            lowerValue = BOUND(upperValue, maximumValue, lowerValue) //this won't compile, there is no BOUND identifier
        }
        if upperKnobLayer.highlighted {
            upperValue += valueDelta
            upperValue = BOUND(_upperValue, _maximumValue, _lowerValue) //This doesn't work
        }

        //Update the UI State
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        self.setLayerFrames()
        CATransaction.commit()
    }

什么是 BOUND 宏?我如何在 Swift 中做到这一点?

根据以下答案进行编辑:

lowerValue = min(max(lowerValue, upperValue), minimumValue)

upperValue = min(max(upperValue, maximumValue), lowerValue)

BOUND 是代码中的自声明宏,它执行 MIN(MAX(VALUE, LOWER), UPPER)

其中 MINMAX 只是C宏,用于获取传入值之间的最小值和最大值。它获取值的最大值与下限之间的最小值,以及上限(我猜有点混乱)。

Swift 定义了这些宏: Swift equivalent for MIN and MAX macros

创建一个方法来执行 BOUND 正在做的事情并调用它,上面的示例很好地解释了 post。