Add and removing a unspecified number of UIImageviews programmatically
My app is a standard casino game where you bet casino chips. When the user taps a开发者_如何学Go chip that chip gets added to the chip pile being used for the bet. I do this by adding a UIImageView on top of (slightly offset to give the appearance of a stack of chips) the other chips (also uiimageviews).
UIImageView *addChip = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chip"]];
addChip.opaque = YES;
addChip.frame = CGRectMake(131, 268, 57, 57);
[self.view addSubview:addChip];
[addChip release];
This pile of chips can obviously be any number UIImageViews (depending on how many chips the player puts down). When the user wants to remove a chip from the pile or the player loses their bet how do I know which subviews to remove?
You can access an image afterwards through the tag property. For example: you index the chips
int numberOfCoins = 0;
//add new coin
UIImageView *addChip = ...
addChip.tag = numberOfCoins;
[self.view addSubview:addChip]
numberOfCoins++;
The next time you add a coin, you can do it the same way. If you want to remove the last coin you can access the image view with the tag and remove it
[[self.view viewWithTag:numberOfCoins] removeFromSuperview];
numberOfCoins--;
精彩评论