繁体   English   中英

快速将 UIImage 移动到另一个 UIImage 之上

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM