绘图遮罩影响性能 [英] Drawing mask influence performance

查看:58
本文介绍了绘图遮罩影响性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于为 UIView 绘制蒙版的代码.问题是,如果我在屏幕上有超过 5 个带有遮罩的 UIViews,它会在拖动 & 时影响性能.删除 UIViews:如何改进以下代码?:

Here is a code I use for drawing mask for UIView. The problem is that if I have more than 5 UIViews with mask on the screen it influence performance when drag & drop UIViews: How can I improve the code below?:

+ (UIBezierPath *)roundedPathAtCenter:(CGPoint)center size:(CGSize)size corner:(CGFloat)corner
{
    NSInteger width = size.width;
    NSInteger height = size.height;

    UIBezierPath *path = [UIBezierPath bezierPath];

    // upper left corner
    [path moveToPoint: CGPointMake(center.x - width / 2.0f + corner / 2.0f, center.y - height / 2.0f + corner / 2.0f)];

    // path to top center
    [path addQuadCurveToPoint: CGPointMake(center.x, center.y - height / 2.0f) controlPoint: CGPointMake(center.x - width / 2.0f + corner, center.y - height / 2.0f)];

    // path to upper right
    [path addQuadCurveToPoint: CGPointMake(center.x + width / 2.0f - corner / 2.0f, center.y - height / 2.0f + corner / 2.0f) controlPoint: CGPointMake(center.x + width / 2.0f - corner, center.y - height / 2.0f)];

    // path to mid right
    [path addQuadCurveToPoint: CGPointMake(center.x + width / 2.0f, center.y) controlPoint: CGPointMake(center.x + width / 2.0f, center.y - height / 2.0 + corner)];

    // path to lower right
    [path addQuadCurveToPoint: CGPointMake(center.x + width / 2.0 - corner / 2.0f, center.y + height / 2.0f - corner / 2.0f) controlPoint: CGPointMake(center.x + width / 2.0f, center.y + height / 2.0f - corner)];

    // path to center bottom
    [path addQuadCurveToPoint: CGPointMake(center.x, center.y + height / 2.0f) controlPoint: CGPointMake(center.x + width / 2.0 - corner, center.y + height / 2.0)];

    // path to lower left
    [path addQuadCurveToPoint: CGPointMake(center.x - width / 2.0f + corner / 2.0f, center.y + height / 2.0f - corner / 2.0f) controlPoint: CGPointMake(center.x - width / 2.0f + corner, center.y + height / 2.0f)];

    // path to mid left
    [path addQuadCurveToPoint: CGPointMake(center.x - width / 2.0f, center.y) controlPoint: CGPointMake(center.x - width / 2.0f, center.y + height / 2.0 - corner)];

    // path to top left

    [path addQuadCurveToPoint: CGPointMake(center.x - width / 2.0f + corner / 2.0f, center.y - height / 2.0f + corner / 2.0f) controlPoint: CGPointMake(center.x - width / 2.0f, center.y - height / 2.0f + corner)];

    [path closePath];

    return path;
}

无需为 UIViews 添加遮罩,它的运行速度非常快

Without adding mask for UIViews it works very fast

推荐答案

在这些情况下,有两种快速提高性能的技巧:

There are two quick techniques to improve performance in these situations:

  1. 在开始拖拽UIView之前,可以设置viewlayer的shouldRasterizecode> 到 true,当你完成删除它时,将 shouldRasterize 恢复到 false.有时(特别是如果拖动的图像可能会发生变化)这会产生不利影响,但尝试起来很简单,您可以查看它是否提高了性能.

  1. Before you start dragging the UIView, you can set the shouldRasterize of the layer of the view to true, and when you are done dropping it, restore shouldRasterize to false. Sometimes (especially if the dragged image could be changing) this has an adverse impact, but it's simple to try and you can see if it improves the performance.

另一种方法是手动创建快照(例如,将视图的剪辑渲染为 UIImage)并用 UIImageView 替换视图,然后然后拖动它,然后当你放下它时,用原始视图替换它(如果你需要它;有时快照就是你所需要的).

The other approach is to manually create a snapshot (e.g. render the view with its clipping to a UIImage) and replace the view with an UIImageView, and then drag that, and then when you drop it, replace it with the original view (if you need it; sometimes the snapshot is all you ever need).

我可能建议观看 WWDC 2012 视频 iOS 应用性能:图形和动画 说明了诊断和解决图形性能问题的一些技术.该视频可能已经有几年的历史了,但这些技术仍然在很大程度上适用.优化 2D 图形和动画性能视频也可能提供一些见解(尽管我认为另一个视频可能更适用于此).

I might suggest watching WWDC 2012 video iOS App Performance: Graphics and Animations which illustrates some techniques for diagnosing and resolving graphic performance issues. The video may be a few years old, but the techniques are still largely applicable. The Optimizing 2D Graphics and Animation Performance video might also offer some insights (though I think the other video is likely to be more applicable here).

顺便说一下,还有很多其他技巧可以提高性能(例如,在拖动视图时,使用预测性触摸;如果被拖动的视图包含很多透明度,可以制作一个不透明的快照来拖动;等等),但也许可以先尝试上述方法,看看是否可以最大限度地减少性能问题.此外,请确保您在目标设备上对此进行测试,不要依赖模拟器中的测试,因为性能特征通常大不相同.

By the way, there are a plethora of other tricks to improve the performance (e.g. when dragging view, use predictive touches; if the view being dragged includes a lot of transparency, maybe make an opaque snapshot to drag; etc.), but perhaps try the above first, and see if that minimizes the performance issue. Also, make sure you test this on a target device and don't rely upon testing in the simulator, as often the performance characteristics are quite different.

这篇关于绘图遮罩影响性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆