Iphone 自定义视图的UIView动画

Iphone 自定义视图的UIView动画,iphone,performance,uiview,core-graphics,uiviewanimation,Iphone,Performance,Uiview,Core Graphics,Uiviewanimation,我创建了一个UIView子类,正在使用UIBezierpath绘制圆角矩形,并用一些渐变填充它 简而言之,子类draw rect就是这样的: CGRect frame1 = //size of Frame 1 CGRect frame2 = //size of Frame 2 //gradientingStart CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); if (options == false) {

我创建了一个UIView子类,正在使用UIBezierpath绘制圆角矩形,并用一些渐变填充它

简而言之,子类draw rect就是这样的:

CGRect frame1 = //size of Frame 1

CGRect frame2 = //size of Frame 2



//gradientingStart
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

if (options == false) {
    self.color = [self createRandomColor];
}



NSArray *gradientColors = [NSArray arrayWithObjects:
                           (id)[UIColor colorWithHue:color saturation:saturationVal brightness:brightnessVal alpha:1].CGColor,
                           (id)[UIColor colorWithHue:color+0.04 saturation:saturationVal+0.15 brightness:brightnessVal-0.15 alpha:1].CGColor, nil];

CGFloat gradientLocations2[] = {0.25,1};
CGGradientRef gradient2 = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations2);
CGContextSaveGState(context);
//gradientingEnd

UIBezierPath *result =
[UIBezierPath bezierPathWithRoundedRect: frame1 cornerRadius:radius];
[result appendPath:
 [UIBezierPath bezierPathWithRoundedRect: frame2 cornerRadius:radius]];
[result setLineWidth:3];
[[UIColor blackColor]setStroke];
[result stroke];
[result fill];
[result addClip];
CGContextDrawLinearGradient(context, gradient2, CGPointMake(0, 0), CGPointMake(0, self.bounds.size.height), 0);

//important to prevent leaks
CGGradientRelease(gradient2);
CGColorSpaceRelease(colorSpace);
//-------
UIImage *texture = [UIImage imageNamed:@"texture2.png"];
[texture drawInRect:CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height)];
现在,我在我的视图控制器中创建了几个实例,并尝试使用如下动画移动它们:

[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
    costumview.alpha = 0;
    [costumview setFrame:CGRectMake(320,0,320,420)];
    [self reorderViewsFrom:costumview.intValue];
    overlay.alpha = 0;
}completion:^(BOOL finished){
}];
但不幸的是,动画是在一个非常滞后的设备上,显示的肋骨视图越多(5-10个),效果就越差


如何提高性能并提供平滑的动画?我认为更改costum视图不是一种选择,因为这会破坏应用程序的感觉。有没有更快的方法来设置肋骨视图的动画?

您的drawRect方法非常强大。绘制这些渐变和圆角是高性能的例行程序,因为您在动画中更改了图像的帧,所以drawRect经常被调用

我会在draw rect中添加一个NSLog,看看在设置自定义视图的动画时它会被调用多少次。你可能会感到惊讶

您可以尝试使用:

view.layer.shouldRasterize = YES
通过这种方式,可以在设置自定义视图的动画之前渲染该视图的位图。再次设置帧更改动画后,应再次调用drawRect并禁用shouldRasterize

请在此处阅读有关应光栅化的更多信息:


我指出,使用一些CALayer转换(圆角)会导致延迟,这在我发布的代码片段中并不存在


我通过使用自己的uiview子类创建圆角来避免这种情况。这使得应用程序更加流畅。所有这些光栅化的东西都不适合我…

在模拟器或设备上运行?模拟器的动画性能与设备相去甚远。在模拟器上运行平稳,但在iPhone4上运行非常慢。真的吗?我通常会遇到相反的情况。是的,在模拟器中测试了几次,结果很顺利,现在在数据集为laggy的设备上。有什么想法吗?听起来很好,但毫无帮助-它仍然像地狱一样迟钝。。。