Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios iPhone为自定义滑块计算0-50之间的数字_Ios_Iphone_Uiview - Fatal编程技术网

Ios iPhone为自定义滑块计算0-50之间的数字

Ios iPhone为自定义滑块计算0-50之间的数字,ios,iphone,uiview,Ios,Iphone,Uiview,当用户左右移动滑块时,我想计算0到50之间的数字。我正在使用一个公式,例如,但没有成功 (点x+100)/20 其中point.x是两个UIView之间使用locatioOfView:方法获取的CGPoint CGPoint point = [sender locationInView:self.viewBaseSlider]; 注意:这里self.viewBaseSlider不是一个滑块,而是一个自定义视图 有什么想法吗?您必须将位置的范围从0到您的自定义视图宽度转换为从0到您的滑块最大值(

当用户左右移动滑块时,我想计算0到50之间的数字。我正在使用一个公式,例如,但没有成功

(点x+100)/20

其中point.x是两个UIView之间使用locatioOfView:方法获取的CGPoint

CGPoint point = [sender locationInView:self.viewBaseSlider];
注意:这里self.viewBaseSlider不是一个滑块,而是一个自定义视图


有什么想法吗?

您必须将
位置
的范围从0到
您的自定义视图宽度
转换为从0到
您的滑块最大值
(在这种情况下为50)

以下是我在
swift
中对它的看法:

let maxPoints = 50
let customViewWidth = customView.frame.size.width
let pointWidth = Float(customViewWidth) / Float(maxPoints)

if let touch = touches.first {
    let location = touch.location(in: customView)
    let point = Int(roundf(Float(location.x) / pointWidth))
    print("Selected \(point)")
}
下面是一个工作示例,其中灰色区域是
customView
,控制台中的输出来自于单击灰色区域中间和末端的开始:


您到底有什么问题?您可以直接获取self.viewBaseSloider。value@anbu.KarthikViewBaseSloider是一个自定义的uiview@DarshanMothreja-然后首先获取子视图,然后子视图与滑块相等,然后获取直接滑块值为什么不将ViewBaseSlaider设置为
UIControl
的子类,并在其移动时设置其值,并发送一个
didChange
操作?然后您可以像使用
UISlider一样使用它