gpt4 book ai didi

ios - 根据scrollView动态改变位置

转载 作者:搜寻专家 更新时间:2023-11-01 06:40:04 24 4
gpt4 key购买 nike

我有一个“U”形UIBezierPath我将其用作 path对于我的 myImage.layer动画。我也有一个 ScrollView 。我的目标是拥有自定义的“拉动刷新”动画。

我遇到的问题是我想要我的 myImage.layer根据 ScrollView 的滚动量进行更新。

当 ScrollView 被下拉时,myImage.layer沿“U”形动画 path .这是 path在我创建为 UIBezierPath 的代码中.

这是我计算 scrollView 被拉下多远的方法:

func scrollViewDidScroll(scrollView: UIScrollView) {
let offsetY = CGFloat(max(-(scrollView.contentOffset.y + scrollView.contentInset.top), 0.0))
self.progress = min(max(offsetY / frame.size.height, 0.0), 1.0)

if !isRefreshing {
redrawFromProgress(self.progress)
}
}

这是动态更新位置的函数(没用):

func redrawFromProgress(progress: CGFloat) {

// PROBLEM: This is not correct. Only the `x` position is dynamic based on scrollView position.
// The `y` position is static.
// I want this to be dynamic based on how much the scrollView scrolled.
myImage.layer.position = CGPoint(x: progress, y: 50)

}

基本上,这就是我想要的:

  • 如果滚动的 scrollView 为 0.0,则 myImage.layer位置应为 CGPoint(x: 0, y: 0) 或 path 的起点.

  • 如果滚动的 scrollView 为 0.5 (50%),则 myImage.layer位置应为 path 的 50% ,我不知道这里的 CGPoint 值是多少。

  • 等等...

我尝试沿着 UIBezierPath 获取 CGPoint 值并根据 ScrollView 的百分比,将 CGPoint 值分配给它,但不知道如何执行此操作。我也看了这个post但我无法让它为我工作。

编辑问题 1:

通过使用这个扩展,我能够得到一个 CGPoints 的数组其中包含 10 个基于我的 UIBezierPath 的值:

extension CGPath {
func forEachPoint(@noescape body: @convention(block) (CGPathElement) -> Void) {
typealias Body = @convention(block) (CGPathElement) -> Void
func callback(info: UnsafeMutablePointer<Void>, element: UnsafePointer<CGPathElement>) {
let body = unsafeBitCast(info, Body.self)
body(element.memory)
}
// print(sizeofValue(body))
let unsafeBody = unsafeBitCast(body, UnsafeMutablePointer<Void>.self)
CGPathApply(self, unsafeBody, callback)
}

func getPathElementsPoints() -> [CGPoint] {
var arrayPoints : [CGPoint]! = [CGPoint]()
self.forEachPoint { element in
switch (element.type) {
case CGPathElementType.MoveToPoint:
arrayPoints.append(element.points[0])
case .AddLineToPoint:
arrayPoints.append(element.points[0])
case .AddQuadCurveToPoint:
arrayPoints.append(element.points[0])
arrayPoints.append(element.points[1])
case .AddCurveToPoint:
arrayPoints.append(element.points[0])
arrayPoints.append(element.points[1])
arrayPoints.append(element.points[2])
default: break
}
}
return arrayPoints
}

我还重写了上面的函数 redrawFromProgress(progress: CGFloat)对此:

func redrawFromProgress(progress: CGFloat) {

let enterPath = paths[0]
let pathPointsArray = enterPath.CGPath
let junctionPoints = pathPointsArray.getPathElementsPoints()
// print(junctionPoints.count) // There are 10 junctionPoints

// progress means how much the scrollView has been pulled down,
// it goes from 0.0 to 1.0.

if progress <= 0.1 {

myImage.layer.position = junctionPoints[0]

} else if progress > 0.1 && progress <= 0.2 {

myImage.layer.position = junctionPoints[1]

} else if progress > 0.2 && progress <= 0.3 {

myImage.layer.position = junctionPoints[2]

} else if progress > 0.3 && progress <= 0.4 {

myImage.layer.position = junctionPoints[3]

} else if progress > 0.4 && progress <= 0.5 {

myImage.layer.position = junctionPoints[4]

} else if progress > 0.5 && progress <= 0.6 {

myImage.layer.position = junctionPoints[5]

} else if progress > 0.6 && progress <= 0.7 {

myImage.layer.position = junctionPoints[6]

} else if progress > 0.7 && progress <= 0.8 {

myImage.layer.position = junctionPoints[7]

} else if progress > 0.8 && progress <= 0.9 {

myImage.layer.position = junctionPoints[8]

} else if progress > 0.9 && progress <= 1.0 {

myImage.layer.position = junctionPoints[9]

}

}

如果我很慢地下拉 scrollView,myImage.layer实际上遵循路径。唯一的问题是,如果我非常快地下拉 scrollView,那么 myImage.layer跳到最后一点。难道是因为我写if statement的方式?以上?

有什么想法吗?

最佳答案

感谢@Sam Falconer 让我意识到这一点:

Your code is relying on the scrollViewDidScroll delegate callback to be called frequently enough to hit all of your keyframe points. When you pull quickly on the scroll view, it does not call that method frequently enough, causing the jump.

在我确认了这一点后,他还提到:

Additionally, you will find the CAKeyframeAnimation class to be useful.

使用 CAKeyfraneAnimation 我可以用这段代码手动控制它的值:

func scrollViewDidScroll(scrollView: UIScrollView) {
let offsetY = CGFloat(max(-(scrollView.contentOffset.y + scrollView.contentInset.top), 0.0))
self.progress = min(max(offsetY / frame.size.height, 0.0), 1.0)

if !isRefreshing {
redrawFromProgress(self.progress)
}
}


func redrawFromProgress(progress: CGFloat) {

// Animate image along enter path
let pathAnimation = CAKeyframeAnimation(keyPath: "position")
pathAnimation.path = myPath.CGPath
pathAnimation.calculationMode = kCAAnimationPaced
pathAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
pathAnimation.beginTime = 1e-100
pathAnimation.duration = 1.0
pathAnimation.timeOffset = CFTimeInterval() + Double(progress)
pathAnimation.removedOnCompletion = false
pathAnimation.fillMode = kCAFillModeForwards

imageLayer.addAnimation(pathAnimation, forKey: nil)
imageLayer.position = enterPath.currentPoint
}

再次感谢大家的帮助!

关于ios - 根据scrollView动态改变位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36705072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com