0

我有一个视图:DragView和一个视图:GameAreaView,并在GameViewController中声明GameAreaView

问题是我的自定义子视图: DragView 没有按预期绘制矩形,在超级视图中没有绘制任何内容。

游戏视图控制器.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];
}

游戏区视图.m

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

DragView.m -(id) initwithFrame::(CGRect)frame

-(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,没有任何变化。

为什么使用 -(id)initwithFrame 时DragView.m中的 drawRect没有被调用?为什么会发生这种情况?以及如何解决?

4

1 回答 1

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];
}
于 2015-03-05T23:30:46.037 回答