简体   繁体   中英

Move UIImage on top of another UIImage in swift

Layout

What i want to achieve: CardBackImage is a subview of the ViewController. The view is placed somewhere (invisible with alpha = 0), i want to relocate it on top of deckPile and make it visible.

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.

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 .

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        
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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