使用滑动手势更改 UISlider 值 swift
Change UISlider value with swipe gesture swift
我有一个带有滑动手势的 UIView。
let swipeUpGesture = UISwipeGestureRecognizer(target: self, action: #selector(NextStepCaptureVC.handleSwipeUp(gesture:)))
swipeUpGesture.direction = .up
view.addGestureRecognizer(swipeUpGesture)
func handleSwipeUp(gesture: UISwipeGestureRecognizer) {
print("Swipe Up")
heightSlider.setValue(20, animated: true)
}
当我尝试更改值时,它起作用了,但值从 0 跳到 20。我希望值在滑动时不断变化。我该怎么做?
你不想要 UISwipeGestureRecognizer
,你想要 UIPanGestureRecognizer
。滑动是一次性手势。
Apple's documentation 说 "A swipe is a discrete gesture, and thus the associated action message is sent only once per gesture."
您设置了从手势识别器到代码的主要操作(您可以为此使用界面生成器)
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.Began {
} else if recognizer.state == UIGestureRecognizerState.Ended {
} else if recognizer.state == UIGestureRecognizerState.Changed {
}
}
祝你好运! =]
从您的代码来看,您似乎正试图将屏幕上的 'panning up and down' 转换为 UISlider
值的变化。
正如其他人已经提到的,首先是将您的 UISwipeGestureRecognizer
更改为 UIPanGestureRecognizer
let pan = UIPanGestureRecognizer(target: self, action: #selector(pan(gesture:)))
view.addGestureRecognizer(pan)
然后在平移功能中,您需要根据用户平移的程度更新滑块值。
func pan(gesture: UIPanGestureRecognizer) {
// The amount of movement up/down since last change in slider
let yTranslation = gesture.translation(in: gesture.view).y
// The slide distance needed to equal one value in the slider
let tolerance: CGFloat = 5
if abs(yTranslation) >= tolerance {
let newValue = heightSlider.value + Float(yTranslation / tolerance)
heightSlider.setValue(newValue, animated: true)
// Reset the overall translation within the view
gesture.setTranslation(.zero, in: gesture.view)
}
}
只需调整 tolerance
变量即可让用户滑动 more/less 以调整滑块值。
我有一个带有滑动手势的 UIView。
let swipeUpGesture = UISwipeGestureRecognizer(target: self, action: #selector(NextStepCaptureVC.handleSwipeUp(gesture:)))
swipeUpGesture.direction = .up
view.addGestureRecognizer(swipeUpGesture)
func handleSwipeUp(gesture: UISwipeGestureRecognizer) {
print("Swipe Up")
heightSlider.setValue(20, animated: true)
}
当我尝试更改值时,它起作用了,但值从 0 跳到 20。我希望值在滑动时不断变化。我该怎么做?
你不想要 UISwipeGestureRecognizer
,你想要 UIPanGestureRecognizer
。滑动是一次性手势。
Apple's documentation 说 "A swipe is a discrete gesture, and thus the associated action message is sent only once per gesture."
您设置了从手势识别器到代码的主要操作(您可以为此使用界面生成器)
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.Began {
} else if recognizer.state == UIGestureRecognizerState.Ended {
} else if recognizer.state == UIGestureRecognizerState.Changed {
}
}
祝你好运! =]
从您的代码来看,您似乎正试图将屏幕上的 'panning up and down' 转换为 UISlider
值的变化。
正如其他人已经提到的,首先是将您的 UISwipeGestureRecognizer
更改为 UIPanGestureRecognizer
let pan = UIPanGestureRecognizer(target: self, action: #selector(pan(gesture:)))
view.addGestureRecognizer(pan)
然后在平移功能中,您需要根据用户平移的程度更新滑块值。
func pan(gesture: UIPanGestureRecognizer) {
// The amount of movement up/down since last change in slider
let yTranslation = gesture.translation(in: gesture.view).y
// The slide distance needed to equal one value in the slider
let tolerance: CGFloat = 5
if abs(yTranslation) >= tolerance {
let newValue = heightSlider.value + Float(yTranslation / tolerance)
heightSlider.setValue(newValue, animated: true)
// Reset the overall translation within the view
gesture.setTranslation(.zero, in: gesture.view)
}
}
只需调整 tolerance
变量即可让用户滑动 more/less 以调整滑块值。