Two picture loops after each other objective-c - objective-c

Im trying to make a animation with pictures using this code:
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:22];
int i;
for (i =1; i < 22; i++) {
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"fbAnimation%d.png", i]]];
//make it animate
self.fbLogin.animationImages = [NSArray arrayWithArray:imageArray];
self.fbLogin.animationDuration =0.8;
self.fbLogin.animationRepeatCount = 5;
[self.fbLogin startAnimating];
}
int j;
NSMutableArray *imageLoading = [[NSMutableArray alloc] initWithCapacity:4];
for (j = 1; j < 4; j++) {
[imageLoading addObject:[UIImage imageNamed:[NSString stringWithFormat:#"loading%d.png", j]]];
//make it animate
self.fbLogin.animationImages = [NSArray arrayWithArray:imageLoading];
self.fbLogin.animationDuration = 1;
self.fbLogin.animationRepeatCount = 5;
[self.fbLogin startAnimating];
}
}
The problem is that either way I've tried it so far just one animation gets executed on the screen. I want the first animation to run and then the second "Loading..." until all other data from facebook is ready.

Firstly, you are calling [self.fbLogin startAnimating] and resetting the animationImages array each time you go round each for loop. You should move them outside the loops - you only need to start each animation once.
Secondly, the second for loop will be executed immediately after the first, so the result I would expect would be for the second animation to appear, and not the first.
One way of getting round the problem would be to put the code for the second animation in a separate method, and then arrange for it to be called after the first has finished by calling:
[self performSelector:#selector(animation2) withObject:nil afterDelay:durationOfPreviousAnimation];
at the end of the first for loop. I'm assuming that since this is animation code we're guaranteed to be on the main thread.

Related

Objective-C block doesn't skips code and then later executes it

I'm using the GPUImage framework and I've noticed that the compiler automatically skips everything that is within the brackets of the setColorAverageProcessingFinishedBlock. It completely skips over these contents and continues on, executing everything else in the code. Once everything else has been executed, it comes back to the content within the brackets. Obviously, this has unintended side effects.
NSMutableArray *redValues = [NSMutableArray array];
NSMutableArray *arrayOne = [NSMutableArray array];
NSUInteger arrayOneLength = [arrayOne count];
__block int counter = 0;
int amount = 1;
float totalOne, diffForAverage;
NSInteger j;
GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
GPUImageAverageColor *averageColor = [[GPUImageAverageColor alloc] init];
[averageColor setColorAverageProcessingFinishedBlock:^(CGFloat redComponent, CGFloat greenComponent, CGFloat blueComponent, CGFloat alphaComponent, CMTime frameTime)
{ // the compiler runs until here, then skips everything within these brackets
NSLog(#"%f", redComponent);
[redValues addObject:#(redComponent * 255)];
}]; // after the brackets close, it executes everything that is below this
// once everything below this has been executed, it goes back to the brackets and executes
// everything between them
[videoCamera addTarget:averageColor];
[videoCamera startCameraCapture];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 27 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[videoCamera stopCameraCapture];
});
totalOne = [redValues[24] floatValue];
float average = totalOne / amount;
NSUInteger redValuesLength = [redValues count];
for (j = (counter + 24); j < (redValuesLength - 24); j++)
{
diffForAverage = average - [redValues[j + 1] floatValue];
if (diffForAverage > -1 && diffForAverage < 1)
{
totalOne += [redValues[j + 1] floatValue];
amount++;
[arrayOne addObject:[NSNumber numberWithInt:(j - 24)]];
counter++;
}
}
How can I solve this problem?
There are two issues with the above code: a memory management one, and a misunderstanding of how blocks work.
First, you're creating a GPUImageVideoCamera instance within a method, but not retaining it as an instance variable. I'm going to assume this is code using automatic reference counting, and if that's true, this camera instance will be deallocated the instant your method is finished. At best, you'll capture maybe one frame from the camera before this is deallocated. At worst, this will crash as the camera and the entire filter chain attached to it are deallocated mid-operation.
Make an instance variable on your containing class and assign your GPUImageVideoCamera instance to it to have it last long enough to be useful.
The second issue with the above is a misunderstanding about how and when blocks will execute. Blocks are merely sections of code you can pass around, and they don't necessarily execute in serial with the rest of the code around them.
In this case, the block you're providing is a callback that will be triggered after every frame of video is processed through the average color operation. This processing takes place asynchronously on a background queue, and you have to design your code to acknowledge this.
If you want X values to be built up, have each measurement be added to an array inside that block, and then within the block check for X values to be reached. At that point, average and do whatever with them. Basically, add a check within the block and move the code you have after it into the block to be run whenever the count is greater than X. You may wish to stop camera capture at that point, if that's all you need.
The code you post is working exactly as it is supposed to work. The color average processing takes a while so it is done on a background thread so the main thread isn't stalled. After the processing is done, then the block is called.
Any code that shouldn't be executed until after the processing is done needs to go inside the block.

Shuffle Answers in Quiz in iPhone App

I've made a quiz and would like for it to shuffle the answers to make it more challenging. This means that when there should be a method that can check which button is the answer. If answer is button 1 then button 1 contains the right answer. If answer is button 4 then button 4 contains the right answer etc.
Right now the code shuffles the answer each time I play a new game, but it sets the titles of all 4 buttons with the same title and the correct answer is always button 1.
Any ideas?
Philip
This is what I have.
- (IBAction)PlayHistory_Easy:(id)sender {
// The questions
history_Easy = [[NSMutableArray alloc] initWithObjects:kHisQ_E1, kHisQ_E2, kHisQ_E3, kHisQ_E4,nil];
numberOfQuestions = [history_Easy count];
index = arc4random() % numberOfQuestions--;
NSString *dd = [history_Easy objectAtIndex:index];
questionLabel.text = dd;
// Setup code to shuffle answers
NSMutableArray *randomizeAnswer = [[NSMutableArray alloc] initWithObjects:btnOne, btnTwo, btnThree, btnFour, nil];
NSMutableArray *ranAns = [[NSMutableArray alloc] initWithObjects:#"Answer 1", #"Answer 2", #"Answer 3", #"Answer 4", nil];
if ([questionLabel.text isEqualToString:kHisQ_E1]) { // Answer 1st button
for (UIButton *btn in randomizeAnswer) {
int i = arc4random() % ranAns.count;
NSString *str = [ranAns objectAtIndex:i];
[btnOne setTitle:[ranAns objectAtIndex:i] forState:UIControlStateNormal];
[btnTwo setTitle:[ranAns objectAtIndex:i] forState:UIControlStateNormal];
[btnThree setTitle:[ranAns objectAtIndex:i] forState:UIControlStateNormal];
[btnFour setTitle:[ranAns objectAtIndex:i] forState:UIControlStateNormal];
}
}
Check the answer. Here checking the answer is done manually, but in order to shuffle the answer, the code be different obviously.
- (IBAction)button1:(id)sender {
if ([questionLabel.text isEqualToString:kHisQ_E1]) {
[self playRIGHTAnswer_EASY];
}
}
- (IBAction)button2:(id)sender {
if ([questionLabel.text isEqualToString:kHisQ_E1]) {
[self playWRONGAnswer_EASY];
}
}
- (IBAction)button3:(id)sender {
if ([questionLabel.text isEqualToString:kHisQ_E1]) {
[self playWRONGAnswer_EASY];
}
}
- (IBAction)button4:(id)sender {
if ([questionLabel.text isEqualToString:kHisQ_E1]) {
[self playWRONGAnswer_EASY];
}
}
To all who is interested in this I provide an answer to my own question.
In trying to figure this out I thought that it would be easier to simply randomly reposition the buttons with each question. So I googled that and found this link:
how to random placement of UIButton and value
I only used part of the code since I already had created my buttons in IB (with outlets and actions properly connected) and so only needed the code below. Whenever you call this code it replaces the buttons according to integer values you put in the mutable array.
- (void)randomlyPositionAnswerButtons {
//Create an array with the rectangles used for the frames
NSMutableArray *indexArray = [NSMutableArray arrayWithObjects:
[NSValue valueWithCGRect:CGRectMake(28, 230, 260, 42)],
[NSValue valueWithCGRect:CGRectMake(28, 296, 260, 42)],
[NSValue valueWithCGRect:CGRectMake(28, 359, 260, 42)],
[NSValue valueWithCGRect:CGRectMake(28, 422, 260, 42)], nil];
//Randomize the array
NSUInteger count = [indexArray count];
for (NSUInteger i = 0; i < count; ++i) {
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[indexArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
//Assign the frames
btnOne.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue];
btnTwo.frame = [((NSValue *)[indexArray objectAtIndex:1]) CGRectValue];
btnThree.frame = [((NSValue *)[indexArray objectAtIndex:2]) CGRectValue];
btnFour.frame = [((NSValue *)[indexArray objectAtIndex:3]) CGRectValue];
}
You seem to have both logical errors and copy/paste errors.
Take a step back and separate your data store (the questions and answers) from the logic (the shuffling) and from the UI (the buttons). At the moment you're mixing all 3 of these things together and it's really messy.
Your data should be stored so that you have a list of items containing:
Question
Possible answers
Correct answer
When you shuffle, just change the orders of the possible answers (maybe in a mutable copy of the source list). There are many algorithms for shuffling, but when you iterate over the list you should generally be swapping two items each time.
Now, taking your shuffled answers you can set the value of each button. This should be trivial. Don't think about shuffling buttons...
When a button is pressed, get the answer associated with it and compare it to the correct answer. Done.
If you want to shuffle the questions as a whole, that should be done as a single stage before any of the above is started.

Objective-C/Cocos2D - Display different Sprites with Animation on the same positon on the Screen

Hello i have a big question and i don't find a way to solve my problem till now.
I work on simple game with objective-c and cocos2D.
I have 3 different objects (sprites with animation) and four fixed position on the screen.
Alternately with a interval i want to display the different objects on the positions.
I wanted to do it with a double for() to position the objects. And in the for for i want to create a multidimensional array with all the objects.
And then i want to create a Method where i have access to the time-interval and the frequency of the different objects.
Do you think i can solve my problem with this solution or do you know a better way…??
It would be great if anybody could help me.
Thanky you
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"myPlist.plist"];
myArray = [[CCArray alloc] init];
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 2; j++) {
Figure *figure = [Figure spriteWithSpriteFrameName:#"a0001.png"];
figure.position = ccp(j * figure.contentSize.width + 50, i * figure.contentSize.height + 50);
[myArray addObject: figure];
[self addChild:figure z:1];
}
}
I do not know how you structured your plist, but I imagine that you have renamed the images in this way:
a0001.png
a0002.png
a0003.png
why you don't try to make a more simple for loop instead of a "for inside a for" for place only 3 images at your view and manage your file like this:
for (int i = 1; i < 3; i++) {
Figure *figure = [Figure spriteWithSpriteFrameName:[NSString stringWithFormat:#"a000%i.png", i]]
figure.position = //add something that fit with your layout
[myArray addObject: figure];
[self addChild:figure z:1];
}

UICollectionView Very Slow Custom Layout

I am using a UICollectionView with a custom layout that lays out cells in a grid format. There can be well over 50 rows and 50 columns. Scrolling occurs both vertically and horizontally. Currently, I am doing all of the layout setup in prepareLayout and storing it in arrays:
- (void)prepareLayout {
NSMutableArray *newLayoutInfo = [[NSMutableArray alloc] init];
NSMutableArray *newLinearLayoutInfor = [[NSMutableArray alloc] init];
NSInteger sectionCount = [self.collectionView numberOfSections];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
self.heightForRows = [delegate collectionViewHeightForAllRows];
self.totalWidthsForRows = [[NSMutableArray alloc] init];
for (int i = 0; i < sectionCount; i++) {
[self.totalWidthsForRows addObject:[NSNumber numberWithInt:0]];
}
for (NSInteger section = 0; section < sectionCount; section++) {
NSMutableArray *cellLayoutInfo = [[NSMutableArray alloc] init];
NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
for (NSInteger item = 0; item < itemCount; item++) {
indexPath = [NSIndexPath indexPathForItem:item inSection:section];
UICollectionViewLayoutAttributes *itemAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
itemAttributes.frame = [self frameForCellAtIndexPath:indexPath];
[cellLayoutInfo addObject:itemAttributes];
[newLinearLayoutInfor addObject:itemAttributes];
}
[newLayoutInfo addObject:cellLayoutInfo];
}
self.layoutInfo = newLayoutInfo;
self.linearLayoutInfo = newLinearLayoutInfor;
}
Then in layoutAttributesForElementsInRect I have:
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *rows = [self.linearLayoutInfo filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) {
return CGRectIntersectsRect(rect, [evaluatedObject frame]);
}]];
This works okay, but it is laggy and jumpy when I have over 50 columns and 50 rows. The problem I now have is that I must set
-(BOOL)shouldInvalidateLayoutForBoundsChange {
return YES;
}
This makes it prepare the entire layout every time the bounds change, which, needless to say, has a huge impact on performance and you can barely scroll. The cells consist of just text with an opaque background, so there is no issue there.
I am sure I am not doing this right and that there must be a better way. Thanks for the help in advance.
In custom flow layout I do this and it seems to help:
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return !(CGSizeEqualToSize(newBounds.size, self.collectionView.frame.size));
}
-(BOOL)shouldInvalidateLayoutForBoundsChange {
return YES;
}
Causes the layout to do prepareLayout() every time it scrolls, which means anything of heavy computing in prepare will lead to a laggy practice, so one possible direction to solve this is to check what's really taking much time. One possibility is what's inside
for (NSInteger section = 0; section < sectionCount; section++)
{
// generate attributes ...
}
in order to generate attributes for the layout. Every time it scrolls, every time this generalization reruns, so that it impacts on the scroll appear to be jumpy and clumsy. So in order to solve this issue, or at least sort out that this is not the really trouble, I suggest setting a flag in this layout algorithm, say, isScrolling, standing for the situation where the layout needs to prepare. Every time in prepareLayout() check the flag, if it is YES, then we'll know there's no need to do for loop to regenerate all the attributes, which alreay exsit ever since the first time the layout is initialised.
ok--I understand now. Here's what I recommend: create 3 collection views... one for the column headers (where each cell is column header), one for the row leaders (each cell = 1 row leader) and one collection view for your cells. Then when the scroll position of any collection view is changed by the user, update the scroll positions for the other 2 collection views as appropriate.

Label display not instant with iPhone app

I am developing an application for the iPhone. The question I have is how to display a new label with a different text every .5 seconds. For example, it would display Blue, Red, Green, Orange and Purple; one right after one another. Right now I am doing this:
results = aDictionary;
NSArray *myKeys = [results allKeys];
NSArray *sortedKeys = [myKey sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
int keyCount = [sortedKeys count];
while (flag == NO) {
NSTimeInterval timeMS = [startDate timeIntervalSinceNow] * -10000.0;
if (timeMS >= i) {
ii++;
i += 1000;
NSLog(#"endDate = %f", timeMS);
int randomNumber = rand() % keyCount + 1;
lblResult.text = [results valueForKey:[sortedKeys objectAtIndex:(randomNumber - 1)]];
result = [results valueForKey:[sortedKeys objectAtIndex:(randomNumber - 1)]];
lblResult.text = result;
}
if (ii > 25) {
flag = YES;
}
}
lblResult.text = [results valueForKey:[sortedKeys objectAtIndex:(sortedKeys.count - 1)]];
this function is called at the viewDidAppear Function and currently isn't displaying the new labels. It only displays the one at the end. Am I doing anything wrong? What would be the best method to approach this?
The problem is that you're not giving the run loop a chance to run (and therefore, drawing to happen). You'll want to use an NSTimer that fires periodically and sets the next text (you could remember in an instance variable where you currently are).
Or use something like this (assuming that items is an NSArray holding your strings):
- (void)updateText:(NSNumber *)num
{
NSUInteger index = [num unsignedInteger];
[label setText:[items objectAtIndex:index]];
index++;
// to loop, add
// if (index == [items count]) { index = 0; }
if (index < [items count]) {
[self performSelector:#selector(updateText:) withObject:[NSNumber numberWithUnsignedInteger:index] afterDelay:0.5];
}
}
At the beginning (e.g. in viewDidAppear:), you could then call
[self updateText:[NSNumber numberWithUnsignedInteger:0]];
to trigger the initial update.
You'd of course need to ensure that the performs are not continuing when your view disappears, you could do this by canceling the performSelector, or if you're using a timer, by simply invalidating it, or using a boolean, or ...
And if you want to get really fancy, use GCD :)