ios - 如何正确使用DrawRect

标签 ios objective-c cocoa-touch uikit core-animation

我目前正在构建自定义UIActivityIndi​​cator。为了做到这一点,我创建了以下绘制矩形函数:

-(void) drawRect:(CGRect)rect  
{
CGPoint point;
NSLog(@"here %d",stage);`

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 2.0);

for (int i = 1 ; i<=10; ++i) {


    CGContextSetStrokeColorWithColor(ctx, [[self getColorForStage:stage+i WithAlpha:0.1 *i] CGColor]);
    point = [self pointOnOuterCirecleWithAngel:stage+i];
    CGContextMoveToPoint(ctx, point.x, point.y);
    point = [self pointOnInnerCirecleWithAngel:stage+i];
    CGContextAddLineToPoint( ctx, point.x, point.y);
    CGContextStrokePath(ctx);
}



stage++;
}

我添加了一个 NSTimer 来调用 [self setNeedsDisplay];

动画效果很好,但是当我每次滚动表格或执行任何操作时将其导入到我的应用程序时,动画都会停止,直到表格停止移动。

我假设只有在 UI 完成更新后才会更新我的 DrawRect 但我该如何解决它或以正确的方式进行

最佳答案

如果您希望在 TableView (或任何其他 ScrollView )滚动时触发计时器,则必须使用不同的运行循环模式手动安排计时器。默认情况下,计时器在滚动时不会触发以提高性能。

NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

(您将使用 NSDefaultRunLoopMode 而不是 NSRunLoopCommonModes 获得默认行为。)

关于ios - 如何正确使用DrawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16923492/

相关文章:

ios - Xcode 8 无法打开 Model.xcdatamodeld

ios - iOS 上发布版本和调试版本之间的物理差异

ios - 创建水平滚动菜单

iphone - 更改默认的 UITabBarController 背景颜色

ios - 将NSInteger和NSString转换为字节数组

ios - 就地打开(但只是一个子集)

objective-c - 在 Swift 中循环遍历 Objective-C 枚举?

objective-c - 在 [[alloc] init] 之外调用 [init] 是否有意义?

iphone - NSString 和 NSDictionary valueForKey - 无?

objective-c - GCD 计时器从不触发