簡體   English   中英

使用CGAffineTransformScale自定義UIView縮放

[英]Custom UIView scaling using CGAffineTransformScale

我正在學習為iOS開發,作為教育過程的一部分,我創建了自定義UIView ,它使用drawRect繪制它的內容(基於Core Graphics + UIBezierPath )。 Everthing按預期工作,視圖是動態的,並根據它的寬度/高度呈現它的內容。 現在我想添加溶解動畫,它看起來應該從100%縮放到0%。 我寫了這段代碼來做到這一點:

[UIView animateWithDuration:1 delay:0
                options: UIViewAnimationOptionBeginFromCurrentState
             animations: ^{
     for (CardView *cardView in self.cardsViews) {
         cardView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.01, 0.01);

     }} completion:nil];

但是,此代碼不會將我的視圖從100%縮放到0%。 它只是使卡片消失(最多)。 我已經嘗試了很多方法來進行縮放,但到目前為止我所達到的效果只是從0%縮放到100%。 我的觀點完美,但不能反向縮放...而且,即使我嘗試應用非動畫變換,它也不會向上/向下縮放,例如:

cardView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.5, 0.5);

我看到我的視圖看起來改變了A BIT,但沒有擴展到50%,這是肯定的。 當我嘗試將完全相同的動畫邏輯應用於UILabel時,它完美運行! 我錯過了開發自定義UIView的哪些方面? 縮放視圖為何可能出現故障 非常感謝提前!

更新#1此代碼使我的視圖正好大兩倍,然后將其縮放回原始大小:

[UIView animateWithDuration:5 delay:0
                    options: UIViewAnimationOptionBeginFromCurrentState
                 animations: ^{
         for (CardView *cardView in self.cardsViews) {
             cardView.transform = CGAffineTransformMakeScale(0.5, 0.5);
         }
    } completion:nil];

使用CGAffineTransformScale,它很簡單,100%到0%:

cardView.view.transform =CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);

[UIView animateWithDuration:0.3/1.5 animations:^{
        cardView.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
    } completion:^(BOOL finished)
    {
              NSLog(@" Animation complet Block");
    }];

你可以嘗試是設置contentMode的視圖的UIViewContentModeRedraw並添加另一個動畫選項- UIViewAnimationOptionAllowAnimatedContent -在animateWithDuration方法。

迅速從120%擴展到100%:

logoView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
UIView.animate(withDuration: 0.55, delay: 0.1, options: [.curveEaseOut], animations: {
    self.logoView.transform = .identity
    self.view.layoutIfNeeded()
}, completion: { _ in
})

暫無
暫無

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

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