我想用手指从底部到顶部移动UIView,就像iOS的控制中心一样,尤其是与iOS的控制中心一样进行“平滑”移动(速度看起来有界,当速度很高时,没有需要用手指走远才能使UIView移到顶部等)。 我的问题是:我可以使用任何预设或库来代替自己的自定义而不是精确的计算吗?我不太擅长于实现这种“平滑”效果,也许UIKit提供了一些在panGestureRecognizer处理程序中使用的功能,以平滑sender.translation之后的运动转换?
现在我的poseRecognizer处理程序是:
var y = sender.translation(in: view).y
let vy = sender.velocity(in: view).y
// we need to move constraintHeightInfoView.constant
// this is the height of my "ios control panel"
// i noticed the ios control panel move less if user tries
// to move it out of screen bounds, so i'm first calculating
// how much we are out of bound, in order to know "how much"
// we will reduce velocity
var outofbound:CGFloat = 0
if(constraintHeightInfoView.constant < minH)
{
outofbound = minH - constraintHeightInfoView.constant
}
else if(constraintHeightInfoView.constant > maxH)
{
outofbound = constraintHeightInfoView.constant - maxH
}
// velocity target in pt/s
// i noticed the smooth effect in ios control panel includes
// a velocity bound : if user pulls very fast the control panel,
// it is not following the user finger, but it's a bit slower :
// so there is a "vmax". When user release, i guess this vmax is
// used for the final animation too
let vtarget:CGFloat = 300
print("\(y), \(vy), ofb : \(outofbound)")
// Here i'm lost : im trying to say :
// if velocity above vtarget, reduce y so it match vtarget
if(abs(vy) > vtarget) {
let dv = abs(vy) - vtarget
y = // ????
}
switch sender.state {
case .began, .changed :
if case .began = sender.state {
initialY = constraintHeightInfoView.constant
}
let newH = (expandInfoViewInitialY ?? minH) - y
constraintHeightInfoView.constant = newH
view.layoutIfNeeded()
case .ended :
let heightTo:CGFloat
let blurAlpha:CGFloat
// 250 is the middle between top position and
// bottom position : if velocity is high, dont need to pull
// a lot to proceed the final release animation
// if vy is 3000, even very small movement proceed the
// animation. Otherwise, if very slow movement,
// user needs to go at least at the middle between top and bottom (250)
if(constraintHeightInfoView.constant > 250 - (-vy/300) * 50)
{
heightTo = maxH
blurAlpha = 1
}
else
{
heightTo = minH
blurAlpha = 0
}
// time of animation depends on remaining distance to bottom/top
let dy = abs(self.constraintHeightInfoView.constant - heightTo)
var t = dy / vtarget
print("end : d:\(dy), t:\(t)")
UIView.animate(withDuration:TimeInterval(t), delay:0,options:[.curveEaseOut], animations :{
self.constraintHeightInfoView.constant = heightTo
self.viewInfoBlurEffect.alpha = blurAlpha
self.view.layoutIfNeeded()
})
default :
break
}
有了所有这些,我的过渡距离苹果控制中心的转变还很遥远……
答案 0 :(得分:1)
如果将其设置为从上到下以及从上到下的动画效果,您将获得更加令人满意的体验。现在,将其“冻结”并通过UIViewPropertyAnimator从摇摄手势识别器中插入。这使其成为交互式动画。
例如,这将使您仅在用户拖动超过特定量的情况下“完成”动画即可。而且,您可以通过同一动画师同时执行轻按以关闭和拖动以关闭。