0
我正在通過this tutorial關於如何製作雙滑塊,同時也學習如何從ObjC轉換到Swift(我只知道Swift真的,但我對ObjC有點了解),它會很好,但我卡住了在這一點上試圖把這部分翻譯成迅捷。我不知道BOUND Macro是一件什麼的,它不在this one之類的任何指南中,對於另一個我不知道如何將它轉換爲Swift。Objective-C翻譯爲Swift:綁定宏?
這裏的目標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)
謝謝我更新了我的問題,以反映基於此答案的代碼的最終版本。 –