adding subviews to a UIImageView

I'm very new to swift and to Xcode; I'm still playing around with it and learning how to make it do what I want it to in the hopes of using Swift and Xcode to build a game. But at the moment, I'm having a lot of trouble.

The reason I'm not posting about this in the games forum is that it's a card game, and since there's no physics involved, I figure it makes more sense to build it as a Single-View Application than it does to build as a Game---please do correct me and point me towards an appropriate tutorial if that's a bad decision.


In any case, what I'm trying to figure out how to do at the moment is how to visually build a card dynamically. What I'd like to do is have one UIImageView be the background (different cards can have the same background), then another UIImageView drawn on top of that background with a picture that'll be different from card to card, and then also a label underneath that image.


Here's what I have in ViewController.swift at the moment---this is in an app I'm making to get used to Xcode and Swift, figure out how it works, etc.:



@IBOutlet weak var handCard2x1: UIImageView!


override func viewDidLoad() {

super.viewDidLoad()

handCard2x1.image = UIImage(named: "trans orange empty space.png");

}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

let cardWidth = CGFloat(130.0);

let viewBounds = self.view.bounds

let xCenter = viewBounds.width / 2;

let orig2x2CenterY = viewBounds.height * 7 / 8

handCard2x1.center = CGPoint(x: xCenter - cardWidth, y: orig2x2CenterY)

var subView = UIImageView(image: UIImage(named: "robin hood.JPG"))

subView.transform = CGAffineTransformScale(subView.transform,

0.15, 0.15)

handCard2x1.addSubview(subView)

}



Confusingly and infuriatingly, when I comment out the addSubview call, the card actually snaps into place. When I uncomment it, I'm expecting the image of robin hood to appear inside the card's frame and the card to snap into place. But that doesn't happen: instead, when I touch the screen in the iPhone6 simulator, nothing happens---the empty blue card frame doesn't even move!


What am I doing wrong? And if the answer is "everything", please be specific!

Replies

Two things - one, don't add subviews to a UIImageView. It's not a general purpose container. Add them to self.view or some other container view that also contains the background UIImageView.


Two, although UIViewController is a UIResponder, normally you would not implement touchesBegan etc. there. Normally you would subclass a UIView and do your touch handling there if you need low-level touch handling. But from the view controller, for your purposes, it would be much easier to use a higher-level API and add gesture recognizers to your view. They will call methods in your view controller when a tap, pan, or whatever gesture is recognized.

I'm not planning to generally have touchesBegan on my ViewController; I'm just doing that as a way of playing around with Swift and Xcode to better understand them. In my final product, I'm expecting to add gesture recognizers, but I'm a ways away from that.


I get that adding subviews to a UIImageView isn't the right way to go, but what would the best way be to do what I want to do, which is to generally draw one image on top of another? Should I play around with handCard2x1.layer.zPosition to make sure one image is on top of another?


Or should I try playing around with the game framework instead of using a Single-View Application, even if the game I ultimately want to make doesn't require any physics?