2014-11-03 64 views
3

我想從我的viewController底部UIWebView動畫,並且我正在使用砌體(https://github.com/Masonry/Masonry)。砌體animationWithDuration不動畫

我最初創建了我的webview,大小爲0 - (x,y,height和width),然後我嘗試對它進行動畫處理,以便webview在viewcontroller的頂部生成動畫。 webview被顯示出來,但它沒有生成動畫 - 它只是立即出現。 任何有經驗的人都可以引導我走向正確的方向嗎?

這是我的按鈕操作

-(void)didPressBtn{ 
    self.infoView = [UIWebView new]; 
    self.infoView.scalesPageToFit = YES; 
    self.infoView.backgroundColor = [UIColor whiteColor]; 
    self.infoView.delegate = self; 
    [self.scrollView addSubview:self.infoView]; 
    [self.infoView makeConstraints:^(MASConstraintMaker *make) { 
     make.edges.equalTo(@(0)); 
    }]; 


    [self.scrollView layoutIfNeeded]; 
    //FIXME: Hmmm it doesn't really animate!? 
    [UIView animateWithDuration:1 animations:^{ 
     [self.scrollView setContentOffset:CGPointMake(0, 0)]; 
     [self.infoView makeConstraints:^(MASConstraintMaker *make) { 
      make.edges.equalTo(self.scrollView); 
     }]; 

     [self.scrollView layoutIfNeeded]; 

    } completion:^(BOOL finished) { 
       [self.infoView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:NSLocalizedString(@"INFORMATION_URL",)] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15]]; 
    }]; 
} 

我的viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.scrollView = [UIScrollView new]; 
    self.scrollView.backgroundColor = [UIColor whiteColor]; 
    [self.view addSubview:self.scrollView]; 
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) { 
     make.edges.equalTo(self.view); 
    }]; 

    UIView *contentView = [UIView new]; 
    [self.scrollView addSubview:contentView]; 

    [contentView makeConstraints:^(MASConstraintMaker *make) { 
     make.edges.equalTo(self.scrollView); 
     make.width.equalTo(self.scrollView); 
    }]; 

//Then adding buttons and such... 
} 

回答

15

一個關於didPressButton快速觀察。您正在使用mas_makeConstraints來設置邊緣等於@0,並且它們之後立即使用mas_makeConstraints再次更改它們以便填充滾動視圖。這增加了第一套頂端的矛盾限制。

相反,您可以使用mas_remakeConstraints替換該視圖上的現有約束。

對於動畫,我用圖案是先更新的約束(未內的動畫塊),然後動畫的佈局:

[UIView animateWithDuration:1 
     animations:^{ 
      [theParentView layoutIfNeeded]; 
     }]; 

參見How do I animate constraint changes?

+1

謝謝!我總是忘記'layoutIfNeeded'應該在** parentView **上調用,而不是在動畫視圖本身上調用 – 2015-10-15 13:43:46