简体   繁体   English

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

[英]Move UIImage on top of another UIImage in swift

Layout布局

What i want to achieve: CardBackImage is a subview of the ViewController.我想要实现的目标: CardBackImage 是 ViewController 的子视图。 The view is placed somewhere (invisible with alpha = 0), i want to relocate it on top of deckPile and make it visible.视图放置在某处(alpha = 0 时不可见),我想将其重新定位在 deckPile 顶部并使其可见。

myCode:我的代码:

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?") })

The problem with this code: The card is in the original position and not on deckPile position.这段代码的问题:卡片在原始位置,而不是在 deckPile 位置。

Please help.请帮忙。

It's not easy to do this directly with UIImage , but you can easily place the image inside a UIImageView and can set one view as a subview of another - since they are simply subclasses of UIView .直接使用UIImage并不容易,但您可以轻松地将图像放在UIImageView中,并且可以将一个视图设置为另一个视图的子视图 - 因为它们只是UIView的子类。

Here's some sample code to pin a view on top of another:这是一些示例代码,用于将视图固定在另一个视图之上:

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