Ios iPhone为自定义滑块计算0-50之间的数字
当用户左右移动滑块时,我想计算0到50之间的数字。我正在使用一个公式,例如,但没有成功 (点x+100)/20 其中point.x是两个UIView之间使用locatioOfView:方法获取的CGPointIos 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到您的滑块最大值(
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一样使用它