簡體   English   中英

UIView沒有改變animateWithDuration中的位置

[英]UIView not changing position in animateWithDuration

我有兩個UIViews(我很糟糕,它是一個UIView和一個UIButton ),我在同一時間制作動畫。 我最初有一個視圖和一個容器視圖,它會動畫很好,像魅力一樣工作。

現在只有我的一個UIViews會在animateWithDuration中移動/動畫,即使通過調試另一個視圖的框架說它處於某個位置它不是。

    CGRect rect = self.toggle.frame;
    CGRect tabRect = self.tabButton.frame;
    rect.origin.x = rect.origin.x - rect.size.width;
    NSLog(@"%f Before",tabRect.origin.x);
    tabRect.origin.x = tabRect.origin.x - rect.size.width;
    NSLog(@"%f After", tabRect.origin.x);
    [UIView animateWithDuration:0.3 animations:^{  // animate the following:
        self.toggle.frame = rect; // move to new location
        self.tabButton.frame = tabRect;
    }];
    NSLog(@"%f AfterAnimation", tabButton.frame.origin.x);

切換視圖移動正常,但tabButton視圖不動畫或移動。 奇怪的是,“After”和“AfterAnimation”調試代碼都返回相同的值,這表明框架確實移動了。 是否有一個特定的原因,當它作為UIContainerView工作時,當toggleUIView時,這將不起作用?

請注意,如果我刪除該行

self.toggle.frame = rect;

tabButton將正確設置動畫,但如果我移動toggletabButton將不會移動,無論它是動畫塊中的第一個還是第二個。

編輯:我已經嘗試將它們移動到單獨的塊中並更改中心點而不是框架,但無濟於事。 似乎如果toggle視圖移動, tabButton將不會移動。

編輯2:圖片證據。{

在以下屏幕截圖中,tabButton bg為綠色,切換bg為紅色。

初始位置(<code> toggle </ code>在屏幕外)正確位置

上圖:初始位置( toggle屏幕外)正確位置

問題<code> toggle </ code>的問題是正確的<code> tabButton </ code>不是

上圖:問題toggle的問題是正確的tabButton不是

當<code> self.toggle.frame = rect </ code>被注釋掉時(<code> tabButton </ code>正確,<code> toggle </ code>沒有)

上圖:當self.toggle.frame = rect時( tabButton正確, toggle不到)}

編輯3:這比我擔心的還要糟糕。{

我已經做了一些測試,即使我將動畫塊中的toggle更改為瞬間, tabButton 仍然沒有動畫。 這讓我覺得tabButton可能只是從根本上不喜歡toggle視圖和/或我自己,所以不會只是為了惹我tabButton

}

編輯4:{

如果我將tabButton動畫更改為tabButton.frame = CGRectMake(10,10,100,100) ,View會立即捕捉到該位置,並在動畫持續時間的同時動畫回原始位置。

}

如果事情不明確,我最好添加更多簿記/ TLDR信息。

  • toggleToggleDraw一個實例,它是我創建的UIView的子視圖。

  • tabButton是一個UIButton ,它是我的IB viewController的一部分,是該類的一個屬性

  • 無論toggletabButton是子視圖self.view

  • 動畫將單獨工作,不會修改rects的邏輯,但如果它們同時動畫則不起作用

  • 無論順序如何, toggle動畫似乎優先於tabButton動畫

我在IB中創建的UIView的動畫有問題(動畫沒有從視圖的當前位置開始,並且在初始位置結束)。

將layoutIfNeeded()發送到動畫塊之前的底層視圖后,一切正常:

self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5) { () -> Void in
...

我認為這不是UIView動畫的問題。 也許你的切換位置與你的tabButton有關。 試一試,您可以將切換框設置為直接舔(10,10,100,100),然后檢查結果。

我已經創建了一個你所描述的例子,一切似乎都很好。 這是我用過的:

UIView *toggle = [[UIView alloc] initWithFrame:CGRectMake(320, 64, 100, 100)];
[toggle setBackgroundColor:[UIColor redColor]];
[self.view addSubview:toggle];

UIButton *tabButton = [[UIButton alloc] initWithFrame:CGRectMake(220, 64, 100, 100)];
[tabButton setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:tabButton];

CGRect rect = toggle.frame;
CGRect tabRect = tabButton.frame;
rect.origin.x = rect.origin.x - rect.size.width;
NSLog(@"%f Before",tabRect.origin.x);
tabRect.origin.x = tabRect.origin.x - rect.size.width;
NSLog(@"%f After", tabRect.origin.x);
[UIView animateWithDuration:1 animations:^{  // animate the following:
    toggle.frame = rect; // move to new location
    tabButton.frame = tabRect;
}];

我建議的是確保代碼在mainthread上運行:

dispatch_async(dispatch_get_main_queue(), ^{
    [UIView animateWithDuration:0.3 animations:^{
        self.toggle.frame = rect; // move to new location
        self.tabButton.frame = tabRect;
    }];
});

還要考慮到動畫代碼后的日志不正確,因為它不會在動畫后運行,而是緊挨着要求動畫。

如果您希望代碼在動畫后運行,您應該使用:

[UIView animateWithDuration:0.3 animations:^{
    self.toggle.frame = rect; // move to new location
    self.tabButton.frame = tabRect;
} completion:^(BOOL finished){
    NSLog(@"Finished animating!");
}];

我找到了解決問題的方法。 如果我以編程方式而不是通過界面構建​​器初始化UIButtontabButton ),則兩個視圖將同時進行動畫處理。

然而,這對我來說似乎非常討厭 ,有點像在一只失蹤的腳上放一個綁帶,並希望它會自行解決。 我無法解決問題的根本原因,但至少我可以避免這些症狀。

如果有人知道為什么視圖不會在界面構建器中創建按鈕時動畫在這里發布答案,我有興趣知道這背后的原因。

謝謝大家的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM