我的数学比例很烂,所以我需要在这一个一些帮助,因为它是我花2小时,远不及我有没有在那里。计算一半
我启用了滑动面板(很像“刷卡透露”你上表细胞功能)的触摸。这是一个丑陋的ascii图
-----------------------------
| Slide | Reveal |
-----------------------------
您滑动左侧的“幻灯片”位,以显示揭示位。我现在想要做的是制定出来,当触摸结束时显示多少显示,并且打开或关闭。
我知道,揭示框的宽度是110,我还可以工作了,如果幻灯片已经向左移动由-75,那么“开放百分比”为75%
Open Percentage = left/width
我希望动画时间从中心0.5秒。所以如果你在50%时需要0.5秒的时间来打开,而49%需要0.5秒来快速关闭(或者在那里,我知道49%会在0.5秒之内触摸)
同样,如果滑块是只有20%打开它应该需要0.1秒来关闭,而80%打开是0.1秒来打开。
我相信我需要向左侧偏移,相对摸出半宽度,计算百分比,然后应用这一比例到最大0.5动画时间......但是,这是我卡住。
更新:这是我设法得到了一些试错后的工作,但感觉冗长和杂乱。绝对是一种更简单的方法!
// Config
let maxAnimationTime = 0.5
// Get left offset
let absLeftOffset = abs(cell.leftContstraint.layoutConstraints[0].constant)
// Get width of action box
let actionBoxWidth = cell.revealView.frame.width
let halfActionBoxWidth = actionBoxWidth/2
// Are we opening or closing?
let snapOpen = absLeftOffset > halfActionBoxWidth
// Calculate the left position relative the half width
let xPos = snapOpen ? absLeftOffset - halfActionBoxWidth : absLeftOffset
// Calculate the percentage of the left percentage relative to the half width
let perc = snapOpen ? abs(xPos - halfActionBoxWidth)/halfActionBoxWidth : xPos/halfActionBoxWidth
// Use the percentage to calculate the animation time to apply
let animationTime = (Double(perc) * maxAnimationTime).round(to: 2)
如果你知道个你能不能只使用该值的动画时间?如果其小于或等于50%,则只使用动画时间的百分比值,如果大于50则使用'100 - percentValue'作为动画时间。 – Hodson