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!