我想实现一些非常简单的线条绘图动画我的iPhone应用程序。我想提请上的延迟后,我的观点一个矩形。我使用performSelector运行我的绘图方法。

-(void) drawRowAndColumn: (id) rowAndColumn
{
    int rc = [rowAndColumn intValue];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
    CGRect rect = CGRectMake(rc * 100, 100, 100, 100);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context, kCGPathFillStroke);

}

,然后将其通过援引:

int col = 10;
[self performSelector:@selector(drawRowAndColumn:) 
           withObject:[NSNumber numberWithInt:col]
           afterDelay:0.2];

但似乎当实际发送drawRowAndColumn:消息时,它不再拥有访问有效CGContextRef,因为我得到的错误,例如:

<Error>: CGContextAddRect: invalid context

如果我的直接调用替换performSelectordrawRowAndColumn,它工作正常。所以我的第一个想法是还通过了在通过CGContextRefperformSelector,但我似乎无法弄清楚如何在同一时间通过多个参数(这是另一个很好的问题。)

这有什么错上面的代码?

有帮助吗?

解决方案

您不能只画在那种任何时候。您需要实现的UIView的drawRect:方法,把你的绘图代码在那里。 为了得到drawRect:解雇你需要让可可知道该视图需要绘制。对于可以调用setNeedsDisplay,或setNeedsDisplayInRect:

直接翻译您的尝试这种方式,你会打电话setNeedsDisplay使用performSelector:withObject:afterDelay - 但是这可能不是做动画的好办法

这取决于你真正想要做的事情 - 但你可以考虑,例如,把你的绘制代码在drawRect我建议,但启动视图中隐藏。然后,您可以拨打setHidden:NO使用performSelector使其出现延迟之后 - 或者你可以顺利通过启动不隐藏动画它,但为0的α,那么一个UIView动画块中改变阿尔法1(有许多关于这在文档)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top