【发布时间】:2015-06-19 06:45:27
【问题描述】:
目前,我正在通过CGAffineTransform 创建一些转换和转换以用于平移视图,但由于iOS 7 和iPhone 4 下的转换性能,我遇到了麻烦。
我在 Istruments 中潜入并记录了这些东西,当我将变换应用到视图时,繁重的工作就完成了。
当前实施
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
}
}
}
问题
为什么在 iOS 7 和我的带有CGAffineTransforms 的 iPhone 4 上性能如此糟糕?因为它对偏移量和解决方法中的帧设置做同样的事情。当我将UIView.animateWithDuration() 与变换一起使用时,它以60fps 的速度执行。在我的 iOS 7 基础上,我可以做什么不重写整个实现?
7 月 28 日更新 发现此问题可能涉及 AutoLayout。这是我当前调用的 TimeProfiler 堆栈:
现在我在当前的实现中面临一个大问题,因为我依赖于 AutoLayout。在 iOS 7 上解决这个麻烦的最简单的解决方案是什么?
【问题讨论】:
-
viewWithTransform.translate =是自定义的还是应该是viewWithTransform.transform =?您是否(有意或无意)使用自动布局?您是否使用发布版本进行分析?
标签: ios objective-c swift core-animation cgaffinetransform