Im trying to do an animation in ios, I copied a working example (which worked for me) however I edited it to have a longer animation: press the button to start the animation, it shows the first image, but never progresses to through the rest.
Please see my code below!
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface ViewController ()
#end
#implementation ViewController
#synthesize imageView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *image = [UIImage imageNamed:#"invasive0001.png"];
self.imageView =[[UIImageView alloc] initWithFrame:CGRectMake(22, 110, 724, 451)];
[self.view insertSubview : self.imageView atIndex:0];
self.imageView.layer.cornerRadius = 9.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.borderWidth = 3.0;
self.imageView.animationImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:#"invasive0001.png"],
[UIImage imageNamed:#"invasive0002.png"],//1
[UIImage imageNamed:#"invasive0003.png"],//2
[UIImage imageNamed:#"invasive0004.png"],//3
[UIImage imageNamed:#"invasive0005.png"],//4
[UIImage imageNamed:#"invasive0006.png"],//5
[UIImage imageNamed:#"invasive0007.png"],//6
[UIImage imageNamed:#"invasive0008.png"],//7
[UIImage imageNamed:#"invasive0009.png"],//8
[UIImage imageNamed:#"invasive00010.png"],//9
[UIImage imageNamed:#"invasive0011.png"],//10
[UIImage imageNamed:#"invasive0012.png"],//11
[UIImage imageNamed:#"invasive0013.png"],//12
[UIImage imageNamed:#"invasive0014.png"],//13
[UIImage imageNamed:#"invasive0015.png"],//14
[UIImage imageNamed:#"invasive0016.png"],//15
[UIImage imageNamed:#"invasive0017.png"],//16
[UIImage imageNamed:#"invasive0018.png"],//17
[UIImage imageNamed:#"invasive0019.png"],//18
[UIImage imageNamed:#"invasive0020.png"],//19
[UIImage imageNamed:#"invasive0021.png"],//19
[UIImage imageNamed:#"invasive0022.png"],//20
[UIImage imageNamed:#"invasive0023.png"],//1
[UIImage imageNamed:#"invasive0024.png"],//2
[UIImage imageNamed:#"invasive0025.png"],//3
[UIImage imageNamed:#"invasive0026.png"],//4
[UIImage imageNamed:#"invasive0027.png"],//5
[UIImage imageNamed:#"invasive0028.png"],//6
[UIImage imageNamed:#"invasive0029.png"],//7
[UIImage imageNamed:#"invasive0030.png"],//8
[UIImage imageNamed:#"invasive0031.png"],//9
[UIImage imageNamed:#"invasive0032.png"],//10
[UIImage imageNamed:#"invasive0033.png"],//11
[UIImage imageNamed:#"invasive0034.png"],//12
[UIImage imageNamed:#"invasive0035.png"],//13
[UIImage imageNamed:#"invasive0036.png"],//14
[UIImage imageNamed:#"invasive0037.png"],//15
[UIImage imageNamed:#"invasive0038.png"],//16
[UIImage imageNamed:#"invasive0039.png"],//17
[UIImage imageNamed:#"invasive0040.png"],//18
[UIImage imageNamed:#"invasive0041.png"],//19
[UIImage imageNamed:#"invasive0042.png"],//20
[UIImage imageNamed:#"invasive0043.png"],//1
[UIImage imageNamed:#"invasive0044.png"],//2
[UIImage imageNamed:#"invasive0045.png"],//3
[UIImage imageNamed:#"invasive0046.png"],//4
[UIImage imageNamed:#"invasive0047.png"],//5
[UIImage imageNamed:#"invasive0048.png"],//6
[UIImage imageNamed:#"invasive0049.png"],//7
[UIImage imageNamed:#"invasive0050.png"],//8
[UIImage imageNamed:#"invasive0051.png"],//9
[UIImage imageNamed:#"invasive0052.png"],//10
[UIImage imageNamed:#"invasive0053.png"],//11
[UIImage imageNamed:#"invasive0054.png"],//12
[UIImage imageNamed:#"invasive0055.png"],//13
[UIImage imageNamed:#"invasive0056.png"],//14
[UIImage imageNamed:#"invasive0057.png"],//15
[UIImage imageNamed:#"invasive0058.png"],//16
[UIImage imageNamed:#"invasive0059.png"],//17
[UIImage imageNamed:#"invasive0060.png"],//18
[UIImage imageNamed:#"invasive0061.png"],//9
[UIImage imageNamed:#"invasive0062.png"],//10
[UIImage imageNamed:#"invasive0063.png"],//11
[UIImage imageNamed:#"invasive0064.png"],//12
[UIImage imageNamed:#"invasive0065.png"],//13
[UIImage imageNamed:#"invasive0066.png"],//14
[UIImage imageNamed:#"invasive0067.png"],//15
[UIImage imageNamed:#"invasive0068.png"],//16
[UIImage imageNamed:#"invasive0069.png"],//17
[UIImage imageNamed:#"invasive0070.png"],//19
[UIImage imageNamed:#"invasive0071.png"],//20
[UIImage imageNamed:#"invasive0072.png"],//1
[UIImage imageNamed:#"invasive0073.png"],//2
[UIImage imageNamed:#"invasive0074.png"],//3
[UIImage imageNamed:#"invasive0075.png"],//4
[UIImage imageNamed:#"invasive0076.png"],//5
[UIImage imageNamed:#"invasive0077.png"],//6
[UIImage imageNamed:#"invasive0078.png"],//7
[UIImage imageNamed:#"invasive0079.png"],//8
[UIImage imageNamed:#"invasive0080.png"],//9
[UIImage imageNamed:#"invasive0081.png"],//10
[UIImage imageNamed:#"invasive0082.png"],//11
[UIImage imageNamed:#"invasive0083.png"],//12
[UIImage imageNamed:#"invasive0084.png"],//13
[UIImage imageNamed:#"invasive0085.png"],//14
[UIImage imageNamed:#"invasive0086.png"],//15
[UIImage imageNamed:#"invasive0087.png"],//16
[UIImage imageNamed:#"invasive0088.png"],//17
[UIImage imageNamed:#"invasive0089.png"],//18
[UIImage imageNamed:#"invasive0090.png"],//19
[UIImage imageNamed:#"invasive0091.png"],//20
[UIImage imageNamed:#"invasive0092.png"],//1
[UIImage imageNamed:#"invasive0093.png"],//2
[UIImage imageNamed:#"invasive0094.png"],//3
[UIImage imageNamed:#"invasive0095.png"],//4
[UIImage imageNamed:#"invasive0096.png"],//5
[UIImage imageNamed:#"invasive0097.png"],//6
[UIImage imageNamed:#"invasive0098.png"],//7
[UIImage imageNamed:#"invasive0099.png"],//8
[UIImage imageNamed:#"invasive0100.png"],//9
[UIImage imageNamed:#"invasive0101.png"],
[UIImage imageNamed:#"invasive0102.png"],//1
[UIImage imageNamed:#"invasive0103.png"],//2
[UIImage imageNamed:#"invasive0104.png"],//3
[UIImage imageNamed:#"invasive0105.png"],//4
[UIImage imageNamed:#"invasive0106.png"],//5
[UIImage imageNamed:#"invasive0107.png"],//6
[UIImage imageNamed:#"invasive0108.png"],//7
[UIImage imageNamed:#"invasive0109.png"],//8
[UIImage imageNamed:#"invasive0110.png"],//9
[UIImage imageNamed:#"invasive0111.png"],//10
[UIImage imageNamed:#"invasive0112.png"],//11
[UIImage imageNamed:#"invasive0113.png"],//12
[UIImage imageNamed:#"invasive0114.png"],//13
[UIImage imageNamed:#"invasive0115.png"],//14
[UIImage imageNamed:#"invasive0116.png"],//15
[UIImage imageNamed:#"invasive0117.png"],//16
[UIImage imageNamed:#"invasive0118.png"],//17
[UIImage imageNamed:#"invasive0119.png"],//18
[UIImage imageNamed:#"invasive0120.png"],//19
[UIImage imageNamed:#"invasive0121.png"],//19
[UIImage imageNamed:#"invasive0122.png"],//20
[UIImage imageNamed:#"invasive0123.png"],//1
[UIImage imageNamed:#"invasive0124.png"],//2
[UIImage imageNamed:#"invasive0125.png"],//3
[UIImage imageNamed:#"invasive0126.png"],//4
[UIImage imageNamed:#"invasive0127.png"],//5
[UIImage imageNamed:#"invasive0128.png"],//6
[UIImage imageNamed:#"invasive0129.png"],//7
[UIImage imageNamed:#"invasive0130.png"],//8
[UIImage imageNamed:#"invasive0131.png"],//9
[UIImage imageNamed:#"invasive0132.png"],//10
[UIImage imageNamed:#"invasive0133.png"],//11
[UIImage imageNamed:#"invasive0134.png"],//12
[UIImage imageNamed:#"invasive0135.png"],//13
[UIImage imageNamed:#"invasive0136.png"],//14
[UIImage imageNamed:#"invasive0137.png"],//15
[UIImage imageNamed:#"invasive0138.png"],//16
[UIImage imageNamed:#"invasive0139.png"],//17
[UIImage imageNamed:#"invasive0140.png"],//18
[UIImage imageNamed:#"invasive0141.png"],//19
[UIImage imageNamed:#"invasive0142.png"],//20
[UIImage imageNamed:#"invasive0143.png"],//1
[UIImage imageNamed:#"invasive0144.png"],//2
[UIImage imageNamed:#"invasive0145.png"],//3
[UIImage imageNamed:#"invasive0146.png"],//4
[UIImage imageNamed:#"invasive0147.png"],//5
[UIImage imageNamed:#"invasive0148.png"],//6
[UIImage imageNamed:#"invasive0149.png"],//7
[UIImage imageNamed:#"invasive0150.png"],//8
[UIImage imageNamed:#"invasive0151.png"],//9
[UIImage imageNamed:#"invasive0152.png"],//10
[UIImage imageNamed:#"invasive0153.png"],//11
[UIImage imageNamed:#"invasive0154.png"],//12
[UIImage imageNamed:#"invasive0155.png"],//13
[UIImage imageNamed:#"invasive0156.png"],//14
[UIImage imageNamed:#"invasive0157.png"],//15
[UIImage imageNamed:#"invasive0158.png"],//16
[UIImage imageNamed:#"invasive0159.png"],//17
[UIImage imageNamed:#"invasive0160.png"],//18
[UIImage imageNamed:#"invasive0161.png"],//9
[UIImage imageNamed:#"invasive0162.png"],//10
[UIImage imageNamed:#"invasive0163.png"],//11
[UIImage imageNamed:#"invasive0164.png"],//12
[UIImage imageNamed:#"invasive0165.png"],//13
[UIImage imageNamed:#"invasive0166.png"],//14
[UIImage imageNamed:#"invasive0167.png"],//15
[UIImage imageNamed:#"invasive0168.png"],//16
[UIImage imageNamed:#"invasive0169.png"],//17
[UIImage imageNamed:#"invasive0170.png"],//19
[UIImage imageNamed:#"invasive0171.png"],//20
[UIImage imageNamed:#"invasive0172.png"],//1
[UIImage imageNamed:#"invasive0173.png"],//2
[UIImage imageNamed:#"invasive0174.png"],//3
[UIImage imageNamed:#"invasive0175.png"],//4
[UIImage imageNamed:#"invasive0176.png"],//5
[UIImage imageNamed:#"invasive0177.png"],//6
[UIImage imageNamed:#"invasive0178.png"],//7
[UIImage imageNamed:#"invasive0179.png"],//8
[UIImage imageNamed:#"invasive0180.png"],//9
[UIImage imageNamed:#"invasive0181.png"],//10
[UIImage imageNamed:#"invasive0182.png"],//11
[UIImage imageNamed:#"invasive0183.png"],//12
[UIImage imageNamed:#"invasive0184.png"],//13
[UIImage imageNamed:#"invasive0185.png"],//14
[UIImage imageNamed:#"invasive0186.png"],//15
[UIImage imageNamed:#"invasive0187.png"],//16
[UIImage imageNamed:#"invasive0188.png"],//17
[UIImage imageNamed:#"invasive0189.png"],//18
[UIImage imageNamed:#"invasive0190.png"],//19
[UIImage imageNamed:#"invasive0191.png"],//20
[UIImage imageNamed:#"invasive0192.png"],//1
[UIImage imageNamed:#"invasive0193.png"],//2
[UIImage imageNamed:#"invasive0194.png"],//3
[UIImage imageNamed:#"invasive0195.png"],//4
[UIImage imageNamed:#"invasive0196.png"],//5
[UIImage imageNamed:#"invasive0197.png"],//6
[UIImage imageNamed:#"invasive0198.png"],//7
[UIImage imageNamed:#"invasive0199.png"],//8
[UIImage imageNamed:#"invasive0200.png"],//9
[UIImage imageNamed:#"invasive0201.png"],
[UIImage imageNamed:#"invasive0202.png"],//1
[UIImage imageNamed:#"invasive0203.png"],//2
[UIImage imageNamed:#"invasive0204.png"],//3
[UIImage imageNamed:#"invasive0205.png"],//4
[UIImage imageNamed:#"invasive0206.png"],//5
[UIImage imageNamed:#"invasive0207.png"],//6
[UIImage imageNamed:#"invasive0208.png"],//7
[UIImage imageNamed:#"invasive0209.png"],//8
[UIImage imageNamed:#"invasive0210.png"],//9
[UIImage imageNamed:#"invasive0211.png"],//10
[UIImage imageNamed:#"invasive0212.png"],//11
[UIImage imageNamed:#"invasive0213.png"],//12
[UIImage imageNamed:#"invasive0214.png"],//13
[UIImage imageNamed:#"invasive0215.png"],//14
[UIImage imageNamed:#"invasive0216.png"],//15
[UIImage imageNamed:#"invasive0217.png"],//16
[UIImage imageNamed:#"invasive0218.png"],//17
[UIImage imageNamed:#"invasive0219.png"],//18
[UIImage imageNamed:#"invasive0220.png"],//19
[UIImage imageNamed:#"invasive0221.png"],//19
[UIImage imageNamed:#"invasive0222.png"],//20
[UIImage imageNamed:#"invasive0223.png"],//1
[UIImage imageNamed:#"invasive0224.png"],//2
[UIImage imageNamed:#"invasive0225.png"],//3
[UIImage imageNamed:#"invasive0226.png"],//4
[UIImage imageNamed:#"invasive0227.png"],//5
[UIImage imageNamed:#"invasive0228.png"],//6
[UIImage imageNamed:#"invasive0229.png"],//7
[UIImage imageNamed:#"invasive0230.png"],//8
[UIImage imageNamed:#"invasive0231.png"],//9
[UIImage imageNamed:#"invasive0232.png"],//10
[UIImage imageNamed:#"invasive0233.png"],//11
[UIImage imageNamed:#"invasive0234.png"],//12
[UIImage imageNamed:#"invasive0235.png"],//13
[UIImage imageNamed:#"invasive0236.png"],//14
[UIImage imageNamed:#"invasive0237.png"],//15
[UIImage imageNamed:#"invasive0238.png"],//16
[UIImage imageNamed:#"invasive0239.png"],//17
[UIImage imageNamed:#"invasive0240.png"],//18
[UIImage imageNamed:#"invasive0241.png"],//19
[UIImage imageNamed:#"invasive0242.png"],//20
[UIImage imageNamed:#"invasive0243.png"],//1
[UIImage imageNamed:#"invasive0244.png"],//2
[UIImage imageNamed:#"invasive0245.png"],//3
[UIImage imageNamed:#"invasive0246.png"],//4
[UIImage imageNamed:#"invasive0247.png"],//5
[UIImage imageNamed:#"invasive0248.png"],//6
[UIImage imageNamed:#"invasive0249.png"],//7
[UIImage imageNamed:#"invasive0250.png"],//8
[UIImage imageNamed:#"invasive0251.png"],//9
[UIImage imageNamed:#"invasive0252.png"],//10
[UIImage imageNamed:#"invasive0253.png"],//11
[UIImage imageNamed:#"invasive0254.png"],//12
[UIImage imageNamed:#"invasive0255.png"],//13
[UIImage imageNamed:#"invasive0256.png"],//14
[UIImage imageNamed:#"invasive0257.png"],//15
[UIImage imageNamed:#"invasive0258.png"],//16
[UIImage imageNamed:#"invasive0259.png"],//17
[UIImage imageNamed:#"invasive0260.png"],//18
[UIImage imageNamed:#"invasive0261.png"],//9
[UIImage imageNamed:#"invasive0262.png"],//10
[UIImage imageNamed:#"invasive0263.png"],//11
[UIImage imageNamed:#"invasive0264.png"],//12
[UIImage imageNamed:#"invasive0265.png"],//13
[UIImage imageNamed:#"invasive0266.png"],//14
[UIImage imageNamed:#"invasive0267.png"],//15
[UIImage imageNamed:#"invasive0268.png"],//16
[UIImage imageNamed:#"invasive0269.png"],//17
[UIImage imageNamed:#"invasive0270.png"],//19
[UIImage imageNamed:#"invasive0271.png"],//20
[UIImage imageNamed:#"invasive0272.png"],//1
[UIImage imageNamed:#"invasive0273.png"],//2
[UIImage imageNamed:#"invasive0274.png"],//3
[UIImage imageNamed:#"invasive0275.png"],//4
[UIImage imageNamed:#"invasive0276.png"],//5
[UIImage imageNamed:#"invasive0277.png"],//6
[UIImage imageNamed:#"invasive0278.png"],//7
[UIImage imageNamed:#"invasive0279.png"],//8
[UIImage imageNamed:#"invasive0280.png"],//9
[UIImage imageNamed:#"invasive0281.png"],//10
[UIImage imageNamed:#"invasive0282.png"],//11
[UIImage imageNamed:#"invasive0283.png"],//12
[UIImage imageNamed:#"invasive0284.png"],//13
[UIImage imageNamed:#"invasive0285.png"],//14
[UIImage imageNamed:#"invasive0286.png"],//15
[UIImage imageNamed:#"invasive0287.png"],//16
nil];
//1.5
self.imageView.animationDuration = 30.0;
self.imageView.animationRepeatCount=0;
}
//button to press to start animation
-(IBAction)buttonStart:(id)sender{
[imageView startAnimating];
}
Thanks All!
** Update: I tried running it on the device however it crashes when I press the play button, it works fine on the emulator, could this be that there are too many images?
Thanks again.
Try loading the image files like below. You may need to fiddle with the loop and leading zeros.
NSMutableArray *array = [NSMutableArray array];
for (int i=2; i < 288; i++) {
NSString *name = [NSString stringWithFormat:#"invasive%04d.png", i];
UIImage *image = [UIImage imageNamed:name];
if (!image) {
NSLog(#"Could not load: %#", name);
}
else {
[array addObject:image];
}
}
self.imageView.animationImages = array;
Another problem is that you are specifying a frame rate of over 700 frames per second, with 287 frames and a duration of 0.4. Try not setting self.imageView.animationDuration, allowing the UIImageView to calculate a default value that will give a 30 frames per second animation. I personally always leave it at the default, only changing it if I want a lower frame rate (longer animation).
I need to access an NSArray and get a value from it with:
-(IBAction) randClicked:(id)sender
{
int randNum;
//when user clicks the button
//create random number, 1 - 32
randNum = arc4random() %32;
As you see, I can display my random number here:
///using this code for testing random number when button is clicked
NSString *randValText = [[NSString alloc]initWithFormat:#"%d", randNum];
//Output random number to label
labelRandText.text = randValText;
//Call method seeImage with SEL methodSelector
SEL methodSelector = #selector(seeImage);
///set image opacity to 0
wisdomView.alpha = 0.0;
//Use NSTimer to call method
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:methodSelector userInfo:nil repeats:NO];
[randValText release];
}
But now, I need to compare the value of randNum and access an NSArray of images for it's position in the array so I can display an image in an UIImageView.
Having a difficult time trying to figure this one out. All help is greatly appreciated. Thank you for your time.
Here is the current code that I have implemented with help from Josh as of 11:13pm 7/6/2001. I think I almost have it...but something I'm doing is not working. I keep getting the warning "Method '-objectAtIndex not found (return type defaults to 'id')
//Call method seeImage with SEL methodSelector
SEL methodSelector = #selector(seeImage);
//Use NSTimer to call method
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:methodSelector userInfo:[NSNumber numberWithInt:randNum] repeats:NO];
[randValText release];
}
////This is the method to make the graphic choosen by randNum to display at the top of the screen
-(IBAction) seeImage: (NSTimer *)tim
{
CGContextRef imageContext = UIGraphicsGetCurrentContext();
int randNum = [(NSNumber *)[tim userInfo] intValue];
UIImage *imgToDisplay = [wisdomView objectAtindex:randNum];
wisdomView.image = imgToDisplay;
wisdomView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"0.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
[UIImage imageNamed:#"13.png"],
[UIImage imageNamed:#"14.png"],
[UIImage imageNamed:#"15.png"],
[UIImage imageNamed:#"16.png"],
[UIImage imageNamed:#"17.png"],
[UIImage imageNamed:#"18.png"],
[UIImage imageNamed:#"19.png"],
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"21.png"],
[UIImage imageNamed:#"22.png"],
[UIImage imageNamed:#"23.png"],
[UIImage imageNamed:#"24.png"],
[UIImage imageNamed:#"25.png"],
[UIImage imageNamed:#"26.png"],
[UIImage imageNamed:#"27.png"],
[UIImage imageNamed:#"28.png"],
[UIImage imageNamed:#"29.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"31.png"],nil];
wisdomView.alpha = 0;
[UIView beginAnimations:nil context:imageContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:4];
[UIView setAnimationDelegate:self];
wisdomView.alpha = 1;
[UIView commitAnimations];
SEL methodSelector = #selector(hideImage);
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:methodSelector userInfo:nil repeats:NO];
}
I appreciate the help. Thank you for your time.
Here is the changed code that still doesn't work....I'm not understanding something. 1:19PM 7/7/2011
-(IBAction) seeImage:(NSTimer *)tim
{
NSArray *wisdom;
wisdom = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"0.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
[UIImage imageNamed:#"13.png"],
[UIImage imageNamed:#"14.png"],
[UIImage imageNamed:#"15.png"],
[UIImage imageNamed:#"16.png"],
[UIImage imageNamed:#"17.png"],
[UIImage imageNamed:#"18.png"],
[UIImage imageNamed:#"19.png"],
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"21.png"],
[UIImage imageNamed:#"22.png"],
[UIImage imageNamed:#"23.png"],
[UIImage imageNamed:#"24.png"],
[UIImage imageNamed:#"25.png"],
[UIImage imageNamed:#"26.png"],
[UIImage imageNamed:#"27.png"],
[UIImage imageNamed:#"28.png"],
[UIImage imageNamed:#"29.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"31.png"],nil];
int randNum = [(NSNumber *)[tim userInfo] intValue];
CGContextRef imageContext = UIGraphicsGetCurrentContext();
UIImage *imgToDisplay = [wisdom objectAtIndex:randNum];
wisdomView.image = imgToDisplay;
Still getting the Method '-objectAtIndex:' not found warning and now '' may not respond to '' 'NSArray' may not respond to '-objectAtIndex'. Arrrrgggg.
Thank you for your time.
Tuck the generated number into the timer's user info slot:
[NSTimer scheduledTimerWithTimeInterval:0
target:self
selector:#selector(seeImage:)
userInfo:[NSNumber numberWithInt:randNum]
repeats:NO];
and then get it back out in the timer's method. By the way, timer methods are supposed to have one parameter.* The timer passes itself to the method for just such a case as this, i.e., when you need to send some info along to be used when it fires.
- (void) seeImage: (NSTimer *)tim {
// Retrieve the NSNumber, using a cast because userInfo returns id
int randNum = [(NSNumber *)[tim userInfo] intValue];
NSImage * imgToDisplay = [arrayOfImgs objectAtIndex:randNum];
// and so on...
}
*Although they do work with any number of parameters, including 0.
Try:
[imageArray objectAtIndex:randNum]
EDIT: based on the additional info in the comments, I think #Josh Caswell's response better answers the question.
//create random number, 1 - 32
randNum = arc4random() %32;
that would give you 0 - 31. check your array bounds.
I fixed it! Thanks Josh for the assist!
-(IBAction) seeImage
{
int randNum;
//when user clicks the button
//create random number, 0 - 31
randNum = arc4random() %32;
NSString *randValText = [[NSString alloc]initWithFormat:#"%d", randNum];
//Output random number to label - testing code
//labelRandText.text = randValText;
///set image opacity to 0
wisdomView.alpha = 0.0;
wisdom = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"0.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
[UIImage imageNamed:#"13.png"],
[UIImage imageNamed:#"14.png"],
[UIImage imageNamed:#"15.png"],
[UIImage imageNamed:#"16.png"],
[UIImage imageNamed:#"17.png"],
[UIImage imageNamed:#"18.png"],
[UIImage imageNamed:#"19.png"],
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"21.png"],
[UIImage imageNamed:#"22.png"],
[UIImage imageNamed:#"23.png"],
[UIImage imageNamed:#"24.png"],
[UIImage imageNamed:#"25.png"],
[UIImage imageNamed:#"26.png"],
[UIImage imageNamed:#"27.png"],
[UIImage imageNamed:#"28.png"],
[UIImage imageNamed:#"29.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"31.png"],nil];
CGContextRef imageContext = UIGraphicsGetCurrentContext();
//pick image randNum index position
wisdomView.image = [wisdom objectAtIndex:randNum];
wisdomView.alpha = 0.0;
[UIView beginAnimations:nil context:imageContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:4];
[UIView setAnimationDelegate:self];
wisdomView.alpha = 1;
[UIView commitAnimations];
SEL methodSelector = #selector(hideImage);
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:methodSelector userInfo:nil repeats:NO];
[randValText release];
}
Thank you for your time!