NSTimer导致UIScrollViews仅停止在iPod Touch 4号上滚动

时间:2013-08-02 20:39:59

标签: ios uiscrollview nstimer

我正在使用以下代码更新我的UIView子类中的自定义drawRects

NSTimer *timer = [NSTimer timerWithTimeInterval:... 
                                         target:...
                                       selector:....
                                       userInfo:...
                                        repeats:...];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

将间隔设置为每1/30秒重复运行一次以更新我的UIViews。它似乎工作正常,但我的所有UIScrollViews都停止“弹跳”,这意味着,如果我要将UIScrollView滚动到内容框架范围之外,它们就不会反弹。这只发生在iPod touch 4th gens上。在我的iPad第三代和我的iPhone 5上,我的UIScrollViews反弹回内容框架。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你的问题必须在其他地方休息。我测试了一个绘制圆圈的简单自定义视图:

@interface CircleView ()

@property (nonatomic) CFAbsoluteTime startTime;

@end

@implementation CircleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _startTime = CFAbsoluteTimeGetCurrent();
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CFAbsoluteTime elapsed = CFAbsoluteTimeGetCurrent() - self.startTime;
    float seconds;
    float fractionsOfSecond = modff(elapsed, &seconds);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextAddArc(context, self.bounds.size.width * fabs(fractionsOfSecond - 0.5) * 2.0, self.bounds.size.height / 2.0, 10.0, 0, M_PI * 2.0, YES);
    CGContextDrawPath(context, kCGPathStroke);
}

@end

我在滚动视图中添加了其中三个并启动了一个计时器:

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *circles;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // add three circle views

    CircleView *circle;
    CGRect frame = self.view.bounds;
    CGFloat height = frame.size.height / 3.0;
    frame.size.height = height;

    self.circles = [NSMutableArray arrayWithCapacity:3];
    for (int i = 0; i < 3; i++)
    {
        circle = [[CircleView alloc] initWithFrame:frame];
        circle.backgroundColor = [UIColor clearColor];
        [self.scrollView addSubview:circle];
        self.circles[i] = circle;
        frame.origin.y += height;
    }

    // start timer

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0/30.0
                                             target:self
                                           selector:@selector(handleTimer)
                                           userInfo:nil
                                            repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)handleTimer
{
    // show the time so I can see the timer in action

    static NSDateFormatter *formatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"HH:mm:ss.SSS";
    });
    self.timeLabel.text = [formatter stringFromDate:[NSDate date]];

    // let's update three circles

    for (UIView *view in self.circles)
        [view setNeedsDisplay];
}

@end

它在第4代iPod Touch上运行良好。也许您可以分享一些代码,我们可以确定问题的根源。