I'm looking for a way to remove an array of buttons created in Swift.
My project is a game of anagrams where a word is broken down into its letters, shuffled, and then shown in buttons. That works very well with the code below.
The only thing is that I'd like to remove the buttons before showing a new word, so they don't overlap (makes for bad UI). So my quest is this:
Check if the characterArray that holds the letters is empty, or nil
If nil, then load new word
If a word is already there, then remove the buttons (the word) before adding the new buttons (word)
#IBAction func shuffleString() {
txtFileArray = fileContent!.componentsSeparatedByString("\n")
// 1. Shuffle the plist with the words to form anagrams from
currentQuestion = Int(arc4random_uniform(UInt32(txtFileArray.count)))
anagramString = txtFileArray.objectAtIndex(currentQuestion) as String
var staticY: CGFloat = self.view.bounds.size.height / 9 * 1; // Static X for all buttons.
var staticWidth: CGFloat = 46; // Static Width for all Buttons.
var staticHeight: CGFloat = 46; // Static Height for all buttons.
var staticPadding: CGFloat = 10; // Padding to add between each button.
var characters: String? = anagramString
// 2. Initialize the buttons for the letters
var button = UIButton()
// 3. Add the word to the characterArray
var characterArray = Array(anagramString)
/* HERE I NEED CONDITIONAL STATEMENT AS DESCRIBED IN MY QUESTION: */
/* CHECK IF CHARACTERARRAY IS EMPTY, IF NOT REMOVE THE CURRENT BUTTONS */
/* BEFORE ADDING NEW ONES */
for (index, value) in enumerate(characterArray) {
button.removeFromSuperview() // THE BUTTONS ARE NOT REMOVED
println("Clear Tiles")
println("index: \(index) and value: \(value)")
}
// 4. Add the word from the characterArray to corresponding amount of buttons
for (index, value) in enumerate(characterArray) /* randomIndex */ {
button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button = UIButton(frame:CGRectMake(0, 0, 44, 44))
button.frame.origin.x = (CGFloat(index) * 45.0) + 100.0
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
button.setBackgroundImage(UIImage(named:"Tile.png"), forState: .Normal)
button.setTitle("\(value)", forState: .Normal)
buttonSeriesArray.append(button)
self.view.addSubview(button)
// println("index: \(index) and value: \(value)")
}
println(characterArray)
}
Your code is simply broken. First you create a button and then you remove it from superview, but it is not assigned to any. And you do that in a loop.
Related
I have a table view that every time I create a new row, a different color is randomly created to change it`s background. using this function:
func getRandomColor() -> UIColor{
let red:CGFloat = CGFloat(drand48())
let green:CGFloat = CGFloat(drand48())
let blue:CGFloat = CGFloat(drand48())
return UIColor(red:red, green: green, blue: blue, alpha: 1.0)
}
I store the color in an array, doing this I want to avoid equals colors in my table view.
The array is populated when the table view is created in table view cellForRow, like this:
colors.append(category.color as! UIColor)
category is my object.
The problem is when I close the app and starts it again. The memory continuos and start to randomly create the same colors. So I'm trying to compare colors to keep generating colors until it is a new color. Using this function:
func validateColor(color: UIColor) -> Bool {
let primeiraCor = colors.first! as UIColor
if primeiraCor == color {
return false
} else {
return true
}
}
colors is my array of color, and color the color I want to compare. But every time the function is called it's return true.
What can I do?
it can be done easier:
func validateColor(color: UIColor) -> Bool {
return !colors.contains(color)
}
or if you want to add some threshold for equality, then you could do something like this:
The extension is taken from Extract RGB Values From UIColor
extension UIColor {
var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? {
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
return getRed(&r, green: &g, blue: &b, alpha: &a) ? (r,g,b,a) : nil
}
}
func validateColor(color: UIColor) -> Bool {
let maxDistance: CGFloat = 0.3
guard let colorComponents = color.components else {
return true
}
return !colors.contains { c in
guard let components = c.components else {
return false
}
let distance = abs(colorComponents.red - components.red) + abs(colorComponents.green - components.green) + abs(colorComponents.blue - components.blue)
return distance < maxDistance
}
}
Set the threshold in maxDistance variable or move it to a static member of your class for better readability.
firstly, you'll always get the same colours on each run because drand48() generates the same sequence of numbers unless you seed it. See this answer on how to do that, but here is the code, using the current time for randomise the seed:
let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
srand48(Int(time))
let number = drand48()
Secondly - you are comparing a totally random colour (effectively a number between 0 and nearly 17,000,000) and wondering why it never seems to match the first colour in your colour array? I think you should be amazed if it ever matched :-)
The app I'm working on has multiple nodes which are added repeatedly to the game. What I have been doing is creating new SKSpriteNodes using an array of texture, and once the node has been added, I assign that node with a specific physics body. But what I would like to do is instead of a having an array of textures, have an array of SKSpriteNodes with the physics body pre-assigned.
I believe I have figured out how to do this, the problem I have is when I try to add that SKSpriteNode more then once to the scene.
For example, below is I'm been playing around with, which works, but if I add another line of addChild(arraySegment_Type1[0] as SKSpriteNode), I get an error and the app crashes.
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
var arraySegment_Type1:[SKSpriteNode] = []
var sprite = SKSpriteNode(imageNamed: "myimage")
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(sprite.size.width, sprite.size.height))
sprite.physicsBody?.dynamic = false
sprite.name = "mysprite"
arraySegment_Type1.append(sprite)
addChild(arraySegment_Type1[0] as SKSpriteNode)
}
Does anybody know how I would be able to load SKSpriteNodes multiple times from an array?
What your code does at the moment is the following:
Creates an empty array of the type [SKSpriteNode]
Creates a single SKSpriteNode instance
Ads a physicsBody to the sprite
Ads this single spriteNode to an array
Picks out the first object in the array (index 0) and ads it to the parentNode.
Your app crashes because you are trying to add the same SKSpriteNode instance to the scene several times over.
Here is a reworked version of your code that actually ads several sprites to the scene, it technically works but does not make a lot of sense just yet:
override func didMoveToView(view: SKView) {
// your ARRAY is not an array-segment it is a collection of sprites
var sprites:[SKSpriteNode] = []
let numberOfSprites = 16 // or any other number you'd like
// Creates 16 instances of a sprite an add them to the sprites array
for __ in 0..<numberOfSprites {
var sprite = SKSpriteNode(imageNamed: "myimage")
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite.physicsBody?.dynamic = false
sprite.name = "mysprite" // not sure what your plans are here...
sprites.append(sprite)
}
// picks out each sprite in the sprites array
for sprite in sprites {
addChild(sprite)
}
}
Now as I said this doesn't make all that much sense. As you've probably noticed the sprites all appear on top of each other for one thing. More importantly, doing this all in the didMoveToView means it makes very little sense going through the whole array-dance, you could simply do the following instead:
override func didMoveToView(view: SKView) {
let numberOfSprites = 16 // or any other number you'd like
for __ in 0..<numberOfSprites {
var sprite = SKSpriteNode(imageNamed: "myimage")
// skipping the physicsBody stuff for now as it is not part of the question
addChild(sprite)
}
}
An improvement obviously, but all sprites are still covering each other, and didMoveToView might not be the best place to handle all this anyway, so perhaps something like this to illustrate the point:
override func didMoveToView(view: SKView) {
let sprites = spritesCollection(count: 16)
for sprite in sprites {
addChild(sprite)
}
}
private func spritesCollection(#count: Int) -> [SKSpriteNode] {
var sprites = [SKSpriteNode]()
for __ in 0..<count {
var sprite = SKSpriteNode(imageNamed: "myimage")
// skipping the physicsBody stuff for now as it is not part of the question
// giving the sprites a random position
let x = CGFloat(arc4random() % UInt32(size.width))
let y = CGFloat(arc4random() % UInt32(size.height))
sprite.position = CGPointMake(x, y)
sprites.append(sprite)
}
return sprites
}
Now, long-wound as this is it is still just a starting point towards a working and more sensible code-structure...
I have a spriteNode which has a default texture of a black circle and I have placed it in the center of the screen. I also have an array which contains 4 textures. What I want to do is when I click on the screen the black circle in the center randomly picks a SKTexture from the array and changes into set texture. I was thinking a long the lines of the code in the didBeginTouches but I'm stuck on how to truly execute this idea. Thanks for any help. :)
var array = [SKTexture(imageNamed: "GreenBall"), SKTexture(imageNamed: "RedBall"), SKTexture(imageNamed: "YellowBall"), SKTexture(imageNamed: "BlueBall")]
override func didMoveToView(view: SKView) {
var choiceBallImg = SKTexture(imageNamed: "BlackBall")
choiceBall = SKSpriteNode(texture: choiceBallImg)
choiceBall.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
self.addChild(choiceBall)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
choiceBall.texture = SKTexture(imageNamed: arc4random(array))
//error: Cannot assign a value of type 'SKTexture!' to a value of type 'SKTexture?'
}
Almost there, change your touchesBegan to this:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
choiceBall.texture = array[randomIndex]
}
First line generates a random number from 0 to the size of your array-1 using the arc4random_uniform function. We also need to convert the size of your array to an Unsigned Integer because Swift is very strict (and rightfully so). We then cast it back to an Integer and use it to access the textures that you already created in your array.
Lets say that mySaveNewT.data.myNText = 20, and in a for loop, 20 MovieClips (tbox) are populated on the stage. When a tbox instance gets clicked I want to change its visibility to false.
How do I reference an individual MovieClip that gets clicked, without having to set every MovieClip's visibility to false? (i.e if MC[2] and MC[10] get clicked, but the rest aren't)
How do I push this into an Array?
Here is my for loop:
for (var i: Number = 0; i < mySaveNewT.data.myNText; ++i) {
newText = new tbox();
newText.x = -220;
newText.y = -513 + i * 69 + 0 * 3.8;
VWD.addChild(newText);
}
To push into an array, and add the click listener and change visibility, see code comments:
//you need to define an array to store the clips in
var clickedBoxes:Array = []; //this makes a new empty array, same as doing: = new Array();
for (var i: Number = 0; i < mySaveNewT.data.myNText; ++i) {
newText = new tbox();
newText.x = -220;
newText.y = -513 + i * 69 + 0 * 3.8;
VWD.addChild(newText);
newText.addEventListener(MouseEvent.CLICK, clipClickHandler,false,0,true); //now you add a click listener to this clip
}
function clipClickHandler(e:MouseEvent):void {
//e.currentTarget will be a reference to the item that was clicked
MovieClip(e.currentTarget).visible= false; //we wrap e.currentTarget in MovieClip so the compiler knows it has a visible property (casting)
clickedBoxes.push(e.currentTarget);
}
To loop through your array later:
for(var index:int=0;index<clickedBoxes.length;index++){
clickedBoxes[index].visible = true; //you may have to cast to avoid a compiler error MovieClip(clickedBoxes[index]).visivle = true;
}
I'm going to create a book shelf in one of my projects. The basic requirements are as follows:
similar to iBooks bookshelf
supports both orientations well
supports all kinds of iOS devices well (different resolutions)
supports deleting and inserting items
supports reordering of items by long press gesture
shows a hidden logo when first row is pulled down
The UICollectionView is the first option that occurred to me. It easily supports grid cells. So I googled it and found some very useful tutorials:
Bryan Hansen's UICollectionView custom layout tutorial
Mark Pospesel's How to Add a Decoration View to a UICollectionView
LXReorderableCollectionViewFlowLayout
And here is the result: (Please ignore the color inconsistency problem because of the graphics I chose are not perfect yet.)
What I did:
Created a custom layout by creating a class inherited from LXReorderableCollectionViewFlowLayout (for reordering purposes) which inherited from UICollectionFlowLayout
Added a decoration view for showing logo
Added a decoration view for showing the bookshelfs
But I ran into a few problems:
1. I can't scroll at all if the items can be shown in one screen
Then I added the following code, to make the contentsize bigger
- (CGSize)collectionViewContentSize
{
return CGSizeMake(self.collectionView.bounds.size.width, self.collectionView.bounds.size.height+100);
}
Then I can scroll now. Here I pull down the first row:
You can see the the decoration view for logo is working.
2. But I got the second set of problems when I pull up the last row:
You can see the decoration view is not added at the green box part.
3. The background of decoration view for bookshelf is getting darker and darker. (Please refer to same problem here
4. The book shelf bar sometimes moves when I reorder the items
I list some of the important code here:
- (void)prepareLayout
{
// call super so flow layout can do all the math for cells, headers, and footers
[super prepareLayout];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSMutableDictionary *shelfLayoutInfo = [NSMutableDictionary dictionary];
// decoration view - emblem
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewLayoutAttributes *emblemAttributes =
[UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:[EmblemView kind]
withIndexPath:indexPath];
emblemAttributes.frame = [self frameForEmblem:YES];
dictionary[[EmblemView kind]] = #{indexPath: emblemAttributes};
// Calculate where shelves go in a vertical layout
int sectionCount = [self.collectionView numberOfSections];
CGFloat y = 0;
CGFloat availableWidth = self.collectionViewContentSize.width - (self.sectionInset.left + self.sectionInset.right);
int itemsAcross = floorf((availableWidth + self.minimumInteritemSpacing) / (self.itemSize.width + self.minimumInteritemSpacing));
for (int section = 0; section < sectionCount; section++)
{
y += self.headerReferenceSize.height;
//y += self.sectionInset.top;
int itemCount = [self.collectionView numberOfItemsInSection:section];
int rows = ceilf(itemCount/(float)itemsAcross)+1; // add 2 more empty row which doesn't have any data
for (int row = 0; row < rows; row++)
{
indexPath = [NSIndexPath indexPathForItem:row inSection:section];
shelfLayoutInfo[indexPath] = [NSValue valueWithCGRect:CGRectMake(0,y, self.collectionViewContentSize.width, self.itemSize.height + DECORATION_HEIGHT)];
y += self.itemSize.height;
if (row < rows - 1)
y += self.minimumLineSpacing;
}
y += self.sectionInset.bottom;
y += self.footerReferenceSize.height;
}
dictionary[[ShelfView kind]] = shelfLayoutInfo;
self.shelfLayoutInfo = dictionary;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *attributesArrayInRect = [super layoutAttributesForElementsInRect:rect];
// cell layout info
for (BookShelfLayoutAttributes *attribs in attributesArrayInRect)
{
attribs.zIndex = 1;
CATransform3D t = CATransform3DIdentity;
t = CATransform3DTranslate(t, 0, 0, 40);
attribs.transform3D = CATransform3DRotate(t, 15 * M_PI / 180, 1, 0, 0);
}
// Add our decoration views (shelves)
NSMutableDictionary* shelfDictionary = self.shelfLayoutInfo[[ShelfView kind]];
NSMutableArray *newArray = [attributesArrayInRect mutableCopy];
[shelfDictionary enumerateKeysAndObjectsUsingBlock:^(id key, NSValue* obj, BOOL *stop) {
if (CGRectIntersectsRect([obj CGRectValue], rect))
{
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:[ShelfView kind] withIndexPath:key];
attributes.frame = [obj CGRectValue];
NSLog(#"decorationView rect = %#",NSStringFromCGRect(attributes.frame));
attributes.zIndex = 0;
//attributes.alpha = 0.5; // screenshots
[newArray addObject:attributes];
}
}];
attributesArrayInRect = [NSArray arrayWithArray:newArray];
NSMutableDictionary* emblemDictionary = self.shelfLayoutInfo[[EmblemView kind]];
NSMutableArray *newArray2 = [attributesArrayInRect mutableCopy];
[emblemDictionary enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *innerStop) {
if (CGRectIntersectsRect(rect, attributes.frame)) {
[newArray2 addObject:attributes];
}
}];
attributesArrayInRect = [NSArray arrayWithArray:newArray2];
return attributesArrayInRect;
}
I'll appreciate if you're patient enough to read this post and provide any advice or suggestions. And I'll post the complete source code if I can fix all the issues. Thank you in advance.
I would suggest to check your last row and do the [self.collectionView ScrollEnable:NO] same for first row, set background color clear of collectionView and collectionViewCell and that bookshelf image sets on background view.