[英]Move UIImage on top of another UIImage in swift
我想要實現的目標: CardBackImage 是 ViewController 的子視圖。 視圖放置在某處(alpha = 0 時不可見),我想將其重新定位在 deckPile 頂部並使其可見。
我的代碼:
let deckFrame = self.view.convert(deckPileView.frame, from:
deckPileView.superview)
print("deck frame is: \(deckFrame)")
fakeCardBack.frame = deckFrame
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 2, delay: 0, options: [], animations: { [self] in fakeCardBack.alpha = 1 }, completion: { _ in print("its works?") })
這段代碼的問題:卡片在原始位置,而不是在 deckPile 位置。
請幫忙。
直接使用UIImage
並不容易,但您可以輕松地將圖像放在UIImageView
中,並且可以將一個視圖設置為另一個視圖的子視圖 - 因為它們只是UIView
的子類。
這是一些示例代碼,用於將視圖固定在另一個視圖之上:
extension UIView {
func pin(subview: UIView) {
subview.translatesAutoresizingMaskIntoConstraints = false
subview.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
subview.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
subview.topAnchor.constraint(equalTo: topAnchor).isActive = true
subview.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.