Obj-C - Use one of two image arrays programatically? - ios

I have two sets of arrays in my app that consist of 3 images each. Every time my app is used, I want one of the two arrays to be set as array _finalSelection (e.g. each time one or the other should be set to _finalSelection randomly). How can I achieve this?
ViewController.m
-(void)viewDidLoad {
_pageImages = #[#"britt001.png", #"britt02.png", #"britt030.png"];
_pageImages2 = #[#"britt001.png", #"britt02.png", #"britt030.png"];
_finalSelection = [one of the two above arrays];
}

Use arc4random() division by 2.
_finalSelection = arc4random() % 2 ? _pageImages : _pageImages2;

Related

need to random an array of images and words. Arc4random_uniform

I'm working on a project of shapes and words. I currently have two arrays. One array is of images, the other array is just a set of strings that describes the first array of images. I'm using 4 buttons to display 4 items in the "string" array. Can someone please tell me how can I utilize arc4random but also make sure one of my 4 buttons display the name of the image.
Below is a quick example of my arrays
var myShapes = ["circle", "square", "rectangle"]
var images = ["circle.png", "square.png", "rectangle.png"]
How do I utilize arc4Random but make sure one item from each array always equals each other?
Adding information for the function to randomize my images.
func randomImage() -> UIImage {
let arrayCount = UInt32(myImages.count)
let unsignedRandomNumber = arc4random_uniform(arrayCount)
let randomNumber = Int(unsignedRandomNumber)
return UIImage(named: myImages[randomNumber])!
}
Below is how I'm doing the random text
let randomDisplayText = buttonDisplayText[Int(arc4random_uniform(UInt32(buttonDisplayText.count)))]

assigning integer variable to image

so i have a few images from 0.png to 9.png they will be used for the score.
now i want the images to be displayed based on the current score. I could do something like this:
scoreImage = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", score]];
the above code means that i have to create an infinite number of images to cover the score.
is there a more efficient way to do this? using only 10 images for each digit e.g 0.png 1.png 2.png... 9.png
Thanks in advance
The MOST efficient way to do this is to use a view object that displays text directly, like a UILabel. Then you can simply set it's font, size, and style to the desired values and set its text to the value you want.
You could craft a custom font that contains just numeric digits, and use that. I've never created a custom font, but I know it's possible.
Failing that, you could create a custom ScoreView class that contains 4 UIImageView objects. It would have an integer property score, and when you set the score value, it would install the correct digit images into it's different component image views.
The code to calculate the digit values might look like this:
int score = 7596;
int remainder = score;
int thousandsValue = remainder / 1000;
remainder -= thousandsValue*1000;
int hundredsValue = remainder /100;
remainder -= hundredsValue*100;
int tensValue = remainder/10;
int onesValue = remainder % 10;
NSLog(
#"For %d, thousandsValue = %d, hundredsValue = %d, tensValue = %d, onesValue = %d",
score,
thousandsValue,
hundredsValue,
tensValue,
onesValue
);
Then use those values to build image filenames for your thousands digit, hundreds digit, tens digit, and ones digits image views.
Finally, load and install the images into each imageView.

Non-repetitive random numbers in Objective-C

I have searched through the internet but I haven't find any straightforward answer. What I am trying to develop is basically a three same-object matching game. I have 3 UIButtons in one row. (each of these 3 buttons have a black hat icon).There are gonna be 3 unique types of hats. There are 3 rows of three items each. I want to touch the first hat and reveal a number from 0 to 2 (Let's say 1). After choosing the first hat, I want the second hat to generate a number between the 2 remained numbers( the choices are 0 and 2, let's say 2). And finally when I touch the third hat, it will generate the last remainder (number 0 in this example).The main reason for picking the numbers is because I want a certain number to represent a unique "Hat", so when I pick a hat with the number 1, a blue hat will pop out, with the number 0 a red hat etc... I have implemented the whole animation and stuff. I am just struggling in the "Unique random number picking". I am sure arrays will be a part of the "Random logic" but I haven't managed to implement it correctly... Any help would be HIGHLY appreciated :) Thank you all!
You can write a method that does this really simply with arc4random and a mutable array property that stores the numbers that have been shown as NSNumber objects.
-(NSInteger) randomNumberZeroToTwo {
NSInteger randomNumber = (NSInteger) arc4random_uniform(3); // picks between 0 and n-1 where n is 3 in this case, so it will return a result between 0 and 2
if ([self.mutableArrayContainingNumbers containsObject: [NSNumber numberWithInteger:randomNumber]])
[self randomNumberZeroToTwo] // call the method again and get a new object
} else {
// end case, it doesn't contain it so you have a number you can use
[self.mutableArrayContainingNumbers addObject: [NSNumber numberWithInteger:randomNumber]];
return randomNumber;
}
}
arc4random returns an NSUInteger so you have to cast it to avoid warnings with NSNumber.
Also make sure to instantiate your mutable array by adding this code so it does so automatically when self.mutableArrayContainingNumbers is called (i.e. lazy instantiation).
-(NSMutableArray *) mutableArrayContainingNumbers
{
if (!_mutableArrayContainingNumbers)
_mutableArrayContainingNumbers = [[NSMutableArray alloc] init];
return _mutableArrayContainingNumbers;
}
Have an array of pickable numbers.
pickable = #[0,1,2]; //Use NSNumbers but you get the idea.
/*Code to generate random number (rNum) with the range of 0-([pickable count]-1)*/
/*
Assign the number to your hat
and then remove that object from pickable
*/
[pickable removeObjectAtIndex:rNum]
and loop over that until [pickable count] == 0;
I hope that gives you a start good luck.

iOS: Tapku Calendar: Need to select multiple dates

I've been checking the Tapku Calendar code for a bit and searched and read all the relevant questions and responses here however none seem to really offer the correct solution to the problem: How to select multiple dates, either programmatically or by tapping. Just a simple blue tile over two adjacent dates would make me happy :-) The post below seems to have a similar question however the answer does not work. The place in the code is not hit unless the month changes - not exactly what I am looking for. What would be great is a higher-level implementation of selectDate: that would select multiple dates. But just the right place to tweak in the library would be a great place to start is anyone is more familiar with the code. Much appreciated.
iOS: Tapku calendar library - allow selecting multiple dates for current month
So after a bit of stepping through code, I have this rudimentary method using a hammer. I adopted most of the code from TKCalendarMonthView.m->selectDay:day method. The method I created basically creates a new TKCalendarMonthTiles object and fills in the details and then adds subviews onto the main TKCalendarMonthTiles object (self). I tag the subviews so I can first get rid of them if they exist at the beginning of the method as I only want to select one additional day (you could leave the subviews attached if you want them to remain in the UI). I don't track the dates or store them or anything however this meets my needs.
The idea is to simply create a view with the correct tile image you want to use and one that contains the text label of the actual "date" like "14" then add those views as subviews to self. The borrowed code does all the calculations for "where" that date tile resides in the grid, so the view is drawn at the correct location. Code:
- (void)markDay:(int)day {
// First, remove any old subviews
[[self viewWithTag:42] removeFromSuperview];
[[self viewWithTag:43] removeFromSuperview];
int pre = firstOfPrev < 0 ? 0 : lastOfPrev - firstOfPrev + 1;
int tot = day + pre;
int row = tot / 7;
int column = (tot % 7)-1;
TKCalendarMonthTiles *deliveryTile = [[TKCalendarMonthTiles alloc] init];
deliveryTile.selectedImageView.image = [UIImage imageWithContentsOfFile:TKBUNDLE(#"TapkuLibrary.bundle/Images/calendar/MyDateTile.png")];
deliveryTile.currentDay.text = [NSString stringWithFormat:#"%d",day];
if(column < 0){
column = 6;
row--;
}
CGRect r = deliveryTile.selectedImageView.frame;
r.origin.x = (column*46);
r.origin.y = (row*44)-1;
deliveryTile.selectedImageView.frame = r;
deliveryTile.currentDay.frame = r;
[[deliveryTile selectedImageView] setTag:42];
[[deliveryTile currentDay] setTag:43];
[self addSubview:deliveryTile.selectedImageView];
[self addSubview:deliveryTile.currentDay];
} // markDay:
I call this method at the end of TKCalendarMonthView.m->selectDay:day as well as at the end of TKCalendarMonthView.m->-reactToTouch:down. Limited testing so far so good. Off to figure out why the timezone setting keeps thinking its tomorrow (I am in Pacific time zone).
Cheers, Michael

Randomizing pre-defined numbers

In my app I would like to randomize set values which I set in #define's. I am looking to use arc4random also. I usually would know how to do this but I have only seen tutorials with very basic things like numbers 0-10!
Any tips/help would be appreciated!
put all of your numbers to an array after that calculate an random number in range of 0 and sizeof your array. After you can get your randomized value from random place of predefined array and remove this value. Do it again for range 0 sizoef array - 1 and so on.
From the Wikipedia objective C article it looks like you can define macros using #define. From their example:
#define Add(x,y) ( x + y )
int a = 1;
int b = 2;
int c = Add(a,b);
NSLog(#"Add result: %i", c);
// this will output
// Add result: 3
I'm not sure how complex you can get with those, but I would think you'd be able to do something like #define MY_VAL() (arc4random()%100) to get a range of values, or maybe even use AlexTeho's idea within the macro.

Resources