gpt4 book ai didi

ios - CGAffineTransforms 性能在 iOS 7 上真的很慢

转载 作者:IT王子 更新时间:2023-10-29 05:52:04 25 4
gpt4 key购买 nike

目前,我正在通过 CGAffineTransform 创建一些过渡和转换以获得平移 View ,但由于 iOS 7 下的转换性能,我遇到了麻烦,并且iPhone 4

我深入研究了 Istruments 并记录了这些东西,当我将我的转换应用到 View 时,繁重的工作就完成了。

当前实现

func handlePan(recognizer : UIPanGestureRecognizer) {

let drawerLocation = recognizer.locationInView(drawerView!)
let locationInView = recognizer.locationInView(containerView!)
let progressMax = containerView!.frame.height - 40 - 20

if(recognizer.state == .Changed) {

let offsetDrag = dragStartPosition.y - locationInView.y
let progress = Float(offsetDrag / progressMax)

if(offsetDrag >= 0) {

let positionTransform = CGAffineTransformMakeTranslation(0, -((containerView!.bounds.height - 40 - 20) * CGFloat(normalizedProgress)))
viewWithTransform.transform = positionTransform // really bad performance here
} else {
// reset the transition
}
}
}

iOS 7 的解决方法

func handlePan(recognizer : UIPanGestureRecognizer) {

let drawerLocation = recognizer.locationInView(drawerView!)
let locationInView = recognizer.locationInView(containerView!)
let progressMax = containerView!.frame.height - 40 - 20

if(recognizer.state == .Changed) {

let offsetDrag = dragStartPosition.y - locationInView.y
let progress = Float(offsetDrag / progressMax)

if(offsetDrag >= 0) {
if UIDevice.currentDevice().systemMajorVersion() > 7 {
let positionTransform = CGAffineTransformMakeTranslation(0, -((containerView!.bounds.height - 40 - 20) * CGFloat(progress)))
viewWithTransform.transform = positionTransform // really bad performance here
} else {
viewWithTransform.frame = CGRectMake(0, -((containerView!.bounds.height - 40 - 20) * CGFloat(progress)), drawerView!.frame.size.width, drawerView!.frame.size.height); // works like a charm on iOS 7
}

} else {
// reset the transition
}
}
}

问题

为什么使用 CGAffineTransforms 在 iOS 7 和我的 iPhone 4 上性能如此糟糕?因为它对偏移量和解决方法中的帧设置做同样的事情。当我将 UIView.animateWithDuration() 与转换一起使用时,它以 60fps 的速度执行。我该怎么做才能不在我的 iOS 7 基础上重写整个实现?

7 月 28 日更新发现AutoLayout可能涉及此问题。这是我当前调用的 TimeProfiler 堆栈:

Time Profiler

现在我在当前的实现中遇到了一个大问题,因为我依赖于 AutoLayout。在 iOS 7 上解决这个麻烦的最简单的解决方案是什么?

最佳答案

虽然他们做同样的事情是对的,但在引擎盖下,这并不容易 - 到处都有矩阵乘法。 More on that can be found here .

奇怪的是,如果你只是这样做,它会影响你的性能——但我猜你的布局很复杂,所以重新渲染需要很多时间;实际上,一周前我遇到了同样的问题,所以这对我有帮助:

  • 出于某种原因从该特定 View 中删除 AutoLayout 有很大帮助
  • 当您不旋转/缩放 View 时,应使用框架操作。当您使用它们时,它真的变得很需要。
  • 移除阴影和半透明 View ,尤其是当它们彼此之间有更多阴影时,使用变换时会真正降低 FPS

此外,您可以尝试在异步调用中分配该转换,看看是否有帮助:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// Transform view
})
})

如果你真的想花哨,请使用 POP Framework from Facebook .它非常适合你想要的东西,它允许你做一些花哨的东西,比如弹跳、弹性等。下面是你如何使用它:

// Create spring animation (there are more types, if you want)
let animation = POPSpringAnimation(propertyNamed: kPOPViewCenter)

// Configure it properly
animation.autoreverses = false
animation.removedOnCompletion = true
animation.fromValue = view.center
animation.toValue = finalPositionPoint

// Add new animation to your view - animation key can be whatever you like, serves as reference
view.pop_addAnimation(animation, forKey: "YourAnimationKey")

编辑:如果您只是移动 View ,请使用 .center 属性,而不是框架。它使您无需再次定义高度/宽度,并让您更清楚地了解您的意图。

关于ios - CGAffineTransforms 性能在 iOS 7 上真的很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931640/

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