2014-04-04 29 views
0

我是iOS上的新開發人員,我正在努力使用DrawRect方法:第一次調用時,它實際上繪製了我想要的位置,但是接下來調用DrawRect無法移動我的視圖(儘管它們調整了它的大小)。
因此,我做了一個簡約測試應用程序來重現我的問題,但仍無法隔離原因。
這個程序只是畫在左上角有一個藍色的長方形,每一次你點擊進去,它應該:
iOS:DrawRect不會移動我的視圖

  • 開關的寬度和高度(此確實工作)
  • 移動10點到右邊的矩形(這工作)

當然我檢查了實際的drawRect被調用,並且其界限確實改變了我想要的,但仍然,我的觀點不想移動。

============================================= =
這是我的ViewController(RVTViewController.m)
===================================== =========

@implementation RVTViewController 
(void)viewDidLoad` 
{ 
    [super viewDidLoad]; 
    if (!self.myView) 
    { 
     CGRect viewBounds = CGRectMake(0,0,100,150); 
     self.myView = [[RVTView alloc] initWithFrame:viewBounds]; 
     [self.view addSubview:self.myView];   
     UITapGestureRecognizer* gestRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; 
     [self.myView addGestureRecognizer:gestRec]; 
    } 
} 

-(void) tap:(UITapGestureRecognizer*) gesture 
{ 
    CGRect newViewBounds = CGRectMake(self.myView.bounds.origin.x + 10, 
             self.myView.bounds.origin.y, 
             self.myView.bounds.size.height, 
             self.myView.bounds.size.width); 
    self.myView.bounds = newViewBounds; 
    [self.myView setNeedsLayout]; 
    [self.myView setNeedsDisplay]; 
} 
@end 

================================= ====
這裏是我的自定義視圖(RVTView.m)
================================ =====

@implementation RVTView 
- (void)drawRect:(CGRect)rect 
{ 
    CGRect newBounds = self.bounds; 
    UIBezierPath* newRect = [UIBezierPath bezierPathWithRect:newBounds]; 
    [[UIColor blueColor] setFill]; 
    UIRectFill(self.bounds); 
    [newRect stroke]; 
} 
@end 

有人可以告訴我我錯了什麼嗎?

+0

你是否真的想改變視圖的框架但不是界限?邊界是視圖的內部幾何圖形,框架是其超級視圖座標系統中的位置和大小。 – x4snowman

+0

你只想改變對應每個水龍頭的視圖的x座標和寬度和高度,對吧? –

+0

是的,正好。非常感謝這個評論。所以我明白我會用「框架」而不是「界限」。而實際上使用「setFrame」正是我想要的。非常感謝你們。 – Frank

回答

-1

使用[self.myView setFrame:newViewBounds];代替self.myView.bounds = newViewBounds;修復了這個問題。

相關問題