簡體   English   中英

動畫UIView alpha的問題

[英]Problems animating UIView alpha

我正在嘗試將淡入淡出應用於我在另一個上面以編程方式創建的UIView。

[UIView animateWithDuration:0.5 animations:^(void) {
    [self.view setAlpha:0];
}
                 completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

完成的事件在0.5秒之后被正確調用,但我沒有看到任何淡入淡出(我應該看到底部的UIView)。

如果不使用alpha,我會移開UIView它可以工作(我看到底部UIView,而頂部UIView滑開),所以它似乎是一個與alpha相關的問題,但我無法弄清楚什么是錯的!

[UIView animateWithDuration:0.5 animations:^(void) {
    CGRect o = self.view.frame;
    o.origin.x = 320;
    self.view.frame = o;
}
                 completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

我之前使用過alpha動畫,它們通常以這種方式工作......

嘗試明確地將opaque設置為NO 我有同樣的問題和設置解決了我的問題。 顯然,不透明的視圖似乎不適合alpha。

歸功於Hampus Nilsson的評論。

[UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut 
animations:^{
    [self.view setAlpha:0];
} 
completion:^(BOOL finished) {
    [self.view removeFromSuperview];
}];

這樣做是正確的,因為options:UIViewAnimationOptionCurveEaseInOut ,你的褪色會更好。

我遇到了確切的問題。 在我的情況下,我希望首先隱藏視圖,因此我將hidden設置為true 我認為更改alpha值會自動更改hidden屬性,但事實並非如此。

因此,如果您希望首先隱藏視圖,請將其alpha設置為0

我有完全相同的問題,沒有一個建議對我有用。 我通過使用圖層不透明度來克服這個問題。 這是顯示圖層的代碼(使用Xamarin,但你會得到這個想法):

        //Enter the view fading
        zoomView.Layer.Opacity = 0;

        UIApplication.SharedApplication.KeyWindow.RootViewController.View.Add(zoomView);

        UIView.AnimateNotify(
            0.15, // duration
            () => { zoomView.Layer.Opacity = 1.0f },
            (finished) => { }
        );

這是為了淡出相同的zoomView

UIView.AnimateNotify(
                0.15, // duration
                () => { zoomView.Layer.Opacity = 0; },
                (finished) =>
                {
                    if (finished)
                    {
                        zoomView.RemoveFromSuperview();
                    }
                }
            );

我在我的情況下遇到同樣的問題因為線程所以我最終在主線程中寫了動畫塊。

dispatch_async(dispatch_get_main_queue(), ^{        
    [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        View.alpha = 0.0f;
    } completion:^(BOOL finished) {
    }];
});

還有一塊拼圖。 當您設置動畫約束時,可以在動畫塊之外設置新約束常量,然后在動畫中調用layoutIfNeeded

對於視圖alphas,這些需要直接在動畫塊內設置。

改變這個:

//setup position changes
myConstraint.constant = 10
myOtherConstraint.constant = 10
//whoops, alpha animates immediately
view.alpha = 0.5

UIView.animate(withDuration: 0.25) {
   //views positions are animating, but alpha is not   
   self.view.layoutIfNeeded()
}

//setup position changes
myConstraint.constant = 10
myOtherConstraint.constant = 10

UIView.animate(withDuration: 0.25) {
   view.alpha = 0.5
   self.view.layoutIfNeeded()
}

暫無
暫無

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

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