Domanda

ho scritto un po 'di vista-applicazione personalizzata utilizzando cacao. E più tardi (sì, lo so che è male) Ho appena mi sono chiesto: sarebbe questo lavoro per il tocco di cacao come bene? Naturalmente id non ha funzionato subito, ho dovuto cambiare i nomi di classe e così via. Beh, ho rinfrescato la vista, ogni volta che era necessario, utilizzando un NSTimer e il metodo setNeedsDisplay:. Ha funzionato abbastanza bene sotto il cacao, ma assolutamente non sotto tocco di cacao.

Non riesco a spiegarlo a me stesso un Io in realtà non so cosa righe di codice potrebbe aiutare qualcuno a risolvere il problema. Forse qui è il Timer:

[self setMyTimer: [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(myTarget:) userInfo:nil repeats:YES]];

E del bersaglio:

- (void) myTarget:(NSTimer *)timer {
    [self setNeedsDisplay];
}

Il timer viene richiamato ogni 30 ms, ho controllato che con una NSLog.

Nel metodo drawRect: ho in realtà solo disegnare alcune forme e non ha fatto niente altro. Solo nel caso sarebbe necessario chiamare un qualche tipo di metodo clearRect:. Come ho già detto, sotto il cacao ha funzionato.

È stato utile?

Soluzione

I would first verify whether drawRect: is running by using a breakpoint or log statement.

Then, make sure that your view is actually on the screen. What is the value of [self superview]? You should also do something like self.backgroundColor = [UIColor redColor]; so that you can see where your view is.

Just because you're marking the view dirty every 30ms doesn't mean it will draw every 30ms. It generally should (that's about 30fps), but there isn't a guarantee. drawRect: shouldn't rely on how often it's called. From your question, I assume you mean that it's never drawing, rather than just not drawing as often as expected.

Altri suggerimenti

Here's the discussion about setNeedsDisplay (note the LACK of arguments) from the documentation of UIView:

You can use this method to notify the system that your view’s contents need to be redrawn. This method makes a note of the request and returns control back to your code immediately. The view is not actually redrawn until the next drawing cycle, at which point all invalidated views are updated.

You should use this method to request that a view be redrawn only when the content or appearance of the view change. If you simply change the geometry of the view, the view is typically not redrawn. Instead, its existing content is adjusted based on the value in the view’s contentMode property. Redisplaying the existing content improves performance by avoiding the need to redraw content that has not changed.

In contrast, here's the discussion about setNeedsDisplay: (note the argument) from the documentation of NSView:

Whenever the data or state used for drawing a view object changes, the view should be sent a setNeedsDisplay: message. NSView objects marked as needing display are automatically redisplayed on each pass through the application’s event loop. (View objects that need to redisplay before the event loop comes around can of course immediately be sent the appropriate display... method.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top