2015-03-05 6 views
0

У меня есть мнение: DragView и вид: GameAreaView, и объявить GameAreaView в GameViewController.Objective C DrawRect в подвид не вызывается после того, как добавить его в супер вид и setNeedsDisplay

Проблема заключается в том, что мой пользовательский подзаголовок: DragView не рисовалRect, как и ожидалось, в супер-представлении ничего нет.

GameViewController.m

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
...... get touch point , but haven't use it 

// Initialise a dragView when user touches in gameAreaView 
DragView *agentDragger = [[DragView alloc] initWithFrame:CGRectMake(10, 10,  
50, 50)]; 

[self.gameAreaView addSubview:agentDragger]; 

[[self gameAreaView] setNeedsDisplay]; 
} 

GameAreaView.m

-(void)drawRect:(CGRect)rect { 
    NSLog(@"I am GameAreaView Rect!!"); 
} 

DragView.m - (ID) initwithFrame: :(CGRect) кадр

-(id) initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     NSLog(@"1.Hi i am in if "); 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 

    [MyStyleKitAgent drawAgentNodeWithFrame:_frame 
         longPressed:_isLongPressed 
           vname:_name 
          pressed:_isPressed]; 


// Test for draw lines 
NSLog(@"I am in DragView"); 

Но если я изменитьinitwithFrame к initwithImage

-(id) initWithImage:(UIImage *) anImage 
{ 
    self = [super initWithImage:anImage]; 
    return self; 
} 

В подвид с изображением отображается в GameAreaView, который не использовал DrawRect.

Я также пробовал использовать setNeesDisplayInRect, ничего не меняется.

Почему drawRect в DragView.m не вызывается при использовании - (id) initwithFrame? Почему это происходит? И как это исправить?

+0

где вы назвали вид сопротивления? –

+0

Сделайте свойство вашего вида перетаскивания и используйте [self.dragview setneedsdisplay]; –

+0

Я вызвал его в - (void) touchesEnded: (NSSet *) затрагивает событиеEvent: (UIEvent *) – iMonkey

ответ

0

Вы должны назвать свой подвид по setNeedsDisplay не ваш главный вид

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    //...... get touch point , but haven't use it 

// Initialise a dragView when user touches in gameAreaView 
DragView *agentDragger = [[DragView alloc] initWithFrame:CGRectMake(10, 10,  
    50, 50)]; 

[self.gameAreaView addSubview:agentDragger]; 

    [agentDragger setNeedsDisplay]; 
} 
+0

Спасибо за ваш ответ, я снова попробовал код, когда вы пишете, но все еще не называется drawRect в dragView. – iMonkey

 Смежные вопросы

  • Нет связанных вопросов^_^