簡體   English   中英

如何使用 swift 使用 Animation 更改 UIImageView ContentMode?

[英]How to Change UIImageView ContentMode With Animation using swift?

我將嘗試使用此代碼,但不起作用

let contentMode: UIView.ContentMode = imageView.contentMode == .scaleAspectFill ?
.scaleAspectFit : .scaleAspectFill
 
 UIView.animate(withDuration: 0.5) {
     self.imageView.contentMode = contentMode
 }

你不能這樣做。 這不是動畫屬性。 目前還不清楚哪種 animation 甚至有意義。

另一種方法是用不同的內容模式替換第二個圖像視圖,並從一個圖像視圖交叉融合到另一個(轉換)。

你為什么不只是為 UIImageView 本身制作動畫? 下面的代碼是一個增長 animation 的例子。

    let height = imageView.frame.height
    let width = imageView.frame.width
    
    let x = imageView.frame.origin.x
    let y = imageView.frame.origin.y
            
    imageView.frame = CGRect(x: x, y: y, width: 0, height: 0)
    
    UIView.animate(withDuration: 0.5) {
        self.imageView.frame = CGRect(x: x, y: y, width: width, height: height)
    }
    

我在過渡的幫助下完成了這項工作

let contentMode: UIView.ContentMode = imageView.contentMode == .scaleAspectFill ? .scaleAspectFit : .scaleAspectFill
        UIView.transition(with: imageView, duration: 0.5, options: .transitionCrossDissolve, animations: {
                self.imageView.contentMode = contentMode
            })

暫無
暫無

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

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