Ios 在drawRect不工作的情况下为cgrect创建动画
我有一个带有UIVIewcontroller的故事板场景。在这个场景中,我有一个UIImageview,其中包含背景图像、一个UIButton和一个UIView 此UIView具有一个覆盖的drawRect方法,该方法具有:Ios 在drawRect不工作的情况下为cgrect创建动画,ios,uiview,drawrect,Ios,Uiview,Drawrect,我有一个带有UIVIewcontroller的故事板场景。在这个场景中,我有一个UIImageview,其中包含背景图像、一个UIButton和一个UIView 此UIView具有一个覆盖的drawRect方法,该方法具有: - (void)drawRect:(CGRect)rect { CGFloat height = self.bounds.size.height-15; // - 15 for text space CGContextRef context = UIGr
- (void)drawRect:(CGRect)rect
{
CGFloat height = self.bounds.size.height-15; // - 15 for text space
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
UIColor *barColor = [UIColor colorWithRed:226.0/255.0 green:178.0/255.0 blue:39.0/255.0 alpha:1.0];
CGContextSetFillColorWithColor(context, barColor.CGColor);
CGFloat barWidth = 37.5;
[UIView animateWithDuration:1.0 animations:^{
int count = 0;
NSArray *values = [NSArray arrayWithObjects:@1, @0.5, @0.2, @0.7, nil];
for (NSNumber *num in values) {
CGFloat x = count * (barWidth + 18);
CGFloat startY = (height - ([num floatValue] * height));
CGRect barRect = CGRectMake(x, startY+15, barWidth, [num floatValue] * height);
CGContextAddRect(context, barRect);
// save context state first
CGContextSaveGState(context);
[self drawWords:[NSString stringWithFormat:@"%@%%",num] AtPoint:CGPointMake(x + (barWidth/2)-10 , startY) color:[UIColor whiteColor]];
count++;
// restore context state first
CGContextRestoreGState(context);
}
}];
CGContextFillPath(context);
}
我试过让酒吧的成长充满活力,但没有成功。我的问题是:我怎样才能让它成功
问候
----已使用新代码更新,但尚未运行
---图形视图代码
- (void) initHelper {
height=0;
graphBars = [[NSMutableArray alloc] initWithCapacity:0];
[self performSelector:@selector(animateBars) withObject:NULL afterDelay:0.5];
}
- (void) animateBars
{
for (GraphBar *bar in graphBars)
{
bar.bottom+=bar.height;
}
CGFloat barWidth=37.5;
CGFloat rat = self.frame.size.height*0.95;
[UIView animateWithDuration:1.0 animations:^{
NSUInteger _index = 0;
for (GraphBar *bar in graphBars)
{
bar.frame = CGRectMake(_index*(barWidth+18), self.frame.size.height-roundf(bar.barValue*rat), barWidth, roundf(bar.barValue*rat));
_index++;
}
}];
}
- (void) layoutSubviews
{
[super layoutSubviews];
CGFloat barWidth=37.5;
CGFloat rat = self.frame.size.height*0.01;
NSUInteger _index = 0;
for (GraphBar *bar in graphBars)
{
bar.frame = CGRectMake(_index*(barWidth+18), self.frame.size.height-roundf(bar.barValue*rat), barWidth, roundf(bar.barValue*rat));
_index++;
}
---条形图代码
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
UIColor *barColor = [UIColor colorWithRed:226.0/255.0 green:178.0/255.0 blue:39.0/255.0 alpha:1.0];
CGContextSetFillColorWithColor(context, barColor.CGColor);
CGContextAddRect(context, rect);
CGContextFillPath(context);
}
}使用更详细的
+animateWithDuration:delay:options:animations:completion:
方法,而不是简单地使用+animateWithDuration:animations:
方法,该方法允许您传递UIViewAnimationOptionAllowAnimatedContent
选项。然后在动画
块中更改视图的高度,以使视图和栏的高度在动画期间增加
此选项专门用于告诉CoreAnimation在动画期间重新绘制内容(在动画的每个步骤调用drawRect:
)。如果没有它,动画将在快照图像上完成
从文件中:
UIViewAnimationOptionAllowAnimatedContent
通过动态更改特性值并重新绘制视图来设置视图动画。如果此键不存在,则使用快照图像为视图设置动画
或者,您可以为每个条形图使用一个
UIView
,并使用+animateWithDuration:animations:
为这些条形图帧设置动画,而不是使用CoreGraphics绘制它们(因为UIView
动画方法用于为视图属性设置动画(frame
,alpha
,…)它的子视图
,主要不是用CoreGraphics
)制作动画我试着按你说的做,但不起作用..我用新代码编辑了我的问题当然不行了。为什么要将animateWithDuration:…
方法调用…放在drawRect:
方法中?这根本没有道理!您应该改为在外部调用它,CoreAnimation将确保为视图高度的更改设置动画…并且按照您指定的上述选项在每个帧绘制视图drawRect:
仅负责绘制一帧/状态。我使用正在更新的代码创建函数-(void)anim,然后在viewController的viewDidLoad中调用此函数,但我无法查看动画,仅在瞬间将高度0更改为最大值。为什么?我不明白:在最新的代码更新中,您的高度变量是多少?一个被完全忽略和丢失的局部变量?!(或者稍后在drawRect
代码中使用的实例变量?在最新的情况下,这就是它的工作原理,如前所述。这是将设置动画的UIView
帧的更改,而不是任何高度
任意变量的影响!(你真的应该阅读与CoreAnimation和animatable properties相关的编程指南!)谢谢你的帮助我已经做了所有的更改,但仍然无法正常工作。我将每个条形图作为一个独立的简单视图(一个矩形)在我做了一个图形视图之后,所有的条形图都是子视图。但是条形图并没有长大…更新的代码有问题