drawRect setNeedsDisplay funktioniert (updatet) nicht, Storyboard?

M

mike76

Registriert
Thread Starter
Dabei seit
07.06.2012
Beiträge
2
Reaktionspunkte
0
Hallo

Seit Tagen suche ich eine Lösung für mein Problem. In meiner Subview stelle ich einen Kompass in Balkenform dar. Der wird wunderbar mit fixen Werten gezeichnet. Jetzt wäre es natürlich toll wenn er sich bewegen würde...
Die Methode zeichneKompass wird aufgerufen und erhält auch korekte Werte. Dies sehe ich im Log.
setNeedsDisplay funktioniert aber nicht, denn weder Zeichnung noch Text (Grad) werden upgedatet.
Kann es daran liegen dass ich Storyboards verwende? Wenn ja, was habe ich da für alternativen?

Da es mein erstes eigenes Projekt ist, kann es auch ein "dummer" Fehler sein...

Gruss Michael


//
// KompassView.m
// SeeNaviApp
//
// Created by Michael Treier on 03.06.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "KompassView.h"

@implementation KompassView
@synthesize richtung = mRichtung;




- (id)initWithFrame:(CGRect)frame

{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}

-(void)zeichneKompass{
[self setNeedsDisplay];
NSLog(@"--- zeichneKompass --- %f", self.richtung);
grad.text = [NSString stringWithFormat:mad:"%f°", self.richtung];

//grad.text = @"123"; => funktioniert auch nicht



}


- (void)drawRect:(CGRect)rect{

NSLog(@"richtung =%f", self.richtung);
CGContextRef theContext = UIGraphicsGetCurrentContext();

CGRect theBounds = self.bounds;

CGContextSaveGState(theContext);
CGContextSetLineCap(theContext, kCGLineCapRound);

NSInteger peilung = self.richtung;
NSInteger ziel = 300;
// Einzustellende Parameter:
NSInteger horizontbreite = 180;
NSInteger strichabstand = 15;

CGFloat xPosStrich; //inGrad
NSInteger n = peilung;
if (n<(horizontbreite / 2)){ n += 360;}
if (ziel>((horizontbreite / 2)+peilung)){ ziel -= 360;}



for (NSInteger i=0; i<(n+(horizontbreite/2)); i+=1) { //Abscannen aller möglicher angezeigter Striche




if (((n - (horizontbreite /2)) < i) & (i < (n + (horizontbreite /2)))){ // ... < i < ...geht nicht
xPosStrich = (((i-n*1.0) / horizontbreite) + 0.5) * CGRectGetMaxX(theBounds);//*1.0 dient zur Typkonvert.



NSLog(@"i=%i --- n=%i --- i-n=%i --- ((i-n*1.0) / horizontbreite) =%i --- xpos = %f", i, n,i-n, ((i-n) / horizontbreite), xPosStrich);


//Teilstriche zeichnen
if((i) % strichabstand == 0){
CGContextSetRGBStrokeColor(theContext, 0.5, 0.5, 0.5, 1.0);
CGContextMoveToPoint(theContext,xPosStrich, 55.0);
CGContextAddLineToPoint(theContext, xPosStrich, 50.0);
CGContextSetLineWidth(theContext, 3.0);
CGContextStrokePath(theContext);
NSLog(@"------------------------------------------------Teilstrich gezeichnet bei %i - %f - %f", i , xPosStrich, xPosStrich/CGRectGetMaxX(theBounds));
}
if((i) % 360 == 0){ // N
CGContextMoveToPoint(theContext, xPosStrich-2, 55.0);
CGContextAddLineToPoint(theContext, xPosStrich, 48.0);
CGContextAddLineToPoint(theContext, xPosStrich+2, 55.0);
CGContextSetRGBStrokeColor(theContext, 0.5, 0.5, 0.0, 1.0);
CGContextSetLineWidth(theContext, 8.0);
CGContextStrokePath(theContext);
// N Zeichnen
CGContextMoveToPoint(theContext, xPosStrich-10, 27.0);
CGContextAddLineToPoint(theContext, xPosStrich-10, 2.0);
CGContextAddLineToPoint(theContext, xPosStrich+10, 27.0);
CGContextAddLineToPoint(theContext, xPosStrich+10, 2.0);

CGContextSetLineWidth(theContext, 2.0);
CGContextStrokePath(theContext);


NSLog(@"------------------------------------------------Windrichtung 0° = N gezeichnet bei %i - %f - %f ---", i , xPosStrich, xPosStrich/CGRectGetMaxX(theBounds));

}
else if((i+90) % 360 == 0){ // W
CGContextMoveToPoint(theContext, xPosStrich-2, 55.0);
CGContextAddLineToPoint(theContext, xPosStrich, 48.0);
CGContextAddLineToPoint(theContext, xPosStrich+2, 55.0);
CGContextSetRGBStrokeColor(theContext, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(theContext, 8.0);
CGContextStrokePath(theContext);

// W Zeichnen
CGContextMoveToPoint(theContext, xPosStrich-10, 2.0);
CGContextAddLineToPoint(theContext, xPosStrich-5, 27.0);
CGContextAddLineToPoint(theContext, xPosStrich, 17.0);
CGContextAddLineToPoint(theContext, xPosStrich+5, 27.0);
CGContextAddLineToPoint(theContext, xPosStrich+10, 2.0);
CGContextSetLineWidth(theContext, 2.0);
CGContextStrokePath(theContext);


NSLog(@"------------------------------------------------Windrichtung 0° = N gezeichnet bei %i - %f - %f ---", i , xPosStrich, xPosStrich/CGRectGetMaxX(theBounds));

}
else if((i+180) % 360 == 0){ // S
CGContextMoveToPoint(theContext, xPosStrich-2, 55.0);
CGContextAddLineToPoint(theContext, xPosStrich, 48.0);
CGContextAddLineToPoint(theContext, xPosStrich+2, 55.0);
CGContextSetRGBStrokeColor(theContext, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(theContext, 8.0);
CGContextStrokePath(theContext);
// S Zeichnen
CGContextMoveToPoint(theContext, xPosStrich+10, 7.0);
CGContextAddLineToPoint(theContext, xPosStrich+5, 2.0);
CGContextAddLineToPoint(theContext, xPosStrich-5, 2.0);
CGContextAddLineToPoint(theContext, xPosStrich-10, 7.0);
CGContextAddLineToPoint(theContext, xPosStrich-10, 12.0);
CGContextAddLineToPoint(theContext, xPosStrich+10, 17.0);
CGContextAddLineToPoint(theContext, xPosStrich+10, 22.0);
CGContextAddLineToPoint(theContext, xPosStrich+5, 27.0);
CGContextAddLineToPoint(theContext, xPosStrich-5, 27.0);
CGContextAddLineToPoint(theContext, xPosStrich-10, 22.0);

CGContextSetLineWidth(theContext, 2.0);
CGContextStrokePath(theContext);


NSLog(@"------------------------------------------------Windrichtung 0° = N gezeichnet bei %i - %f - %f ---", i , xPosStrich, xPosStrich/CGRectGetMaxX(theBounds));

}
else if((i+270) % 360 == 0){ // O
CGContextMoveToPoint(theContext, xPosStrich-2, 55.0);
CGContextAddLineToPoint(theContext, xPosStrich, 48.0);
CGContextAddLineToPoint(theContext, xPosStrich+2, 55.0);
CGContextSetRGBStrokeColor(theContext, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(theContext, 8.0);
CGContextStrokePath(theContext);

// O Zeichnen
CGContextMoveToPoint(theContext, xPosStrich+10, 7.0);
CGContextAddLineToPoint(theContext, xPosStrich+5, 2.0);
CGContextAddLineToPoint(theContext, xPosStrich-5, 2.0);
CGContextAddLineToPoint(theContext, xPosStrich-10, 7.0);
CGContextAddLineToPoint(theContext, xPosStrich-10, 22.0);
CGContextAddLineToPoint(theContext, xPosStrich-5, 27.0);
CGContextAddLineToPoint(theContext, xPosStrich+5, 27.0);
CGContextAddLineToPoint(theContext, xPosStrich+10, 22.0);
CGContextAddLineToPoint(theContext, xPosStrich+10, 7.0);


CGContextSetLineWidth(theContext, 2.0);
CGContextStrokePath(theContext);


NSLog(@"------------------------------------------------Windrichtung 0° = N gezeichnet bei %i - %f - %f ---", i , xPosStrich, xPosStrich/CGRectGetMaxX(theBounds));

}




}


}
//Peilungspfeil zeichnen
xPosStrich = CGRectGetMaxX(theBounds) / 2;
CGContextMoveToPoint(theContext, xPosStrich-30, 70.0);
CGContextAddLineToPoint(theContext, xPosStrich-30, 30.0);
CGContextAddLineToPoint(theContext, xPosStrich-5, 30.0);
CGContextAddLineToPoint(theContext, xPosStrich, 22.0);
CGContextAddLineToPoint(theContext, xPosStrich+5, 30.0);
CGContextAddLineToPoint(theContext, xPosStrich+30, 30.0);
CGContextAddLineToPoint(theContext, xPosStrich+30, 70.0);
CGContextSetRGBStrokeColor(theContext, 0.8, 0.0, 0.0, 1.0);
CGContextSetLineWidth(theContext, 8.0);
CGContextStrokePath(theContext);


grad.text = [NSString stringWithFormat:mad:"%i°", peilung];




}



@end
 
drawRect wird also nicht aufgerufen?

Dann würde ich mal einfach behaupten dass Dein view nicht Teil einer View Hierarchie ist.
Dass soll heissen, Du hast addSubView vergessen, der view ist hidden, oder Dein XIB ist fehlerhaft.

Egal, was Deine drawRect Methode macht, ein NSLog am Anfang sollte man schon sehen.

drawRect wird nicht aufgerufen, wenn iOS meint der view wäre nicht sichtbar...
 
Der View wird gezeichnet, leider halt nur ein mal. Er wird nicht aktualisiert.
XIB hab ich nicht, denn ich arbeite mit storyboard.
das NSLog wird einwandfrei gezeigt und aktualisiert, das heiss, dass auch die Methode zeichneKompass aufgerufen wird.
 
Zurück
Oben Unten