Animation starts but does not seem to continue - iphone
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).
Related
How to show buttons after an animation?
I am using this piece of code for an animation in one of my views: animation.animationImages= [NSArray arrayWithObjects: [UIImage imageNamed:#"Frame0.png"], [UIImage imageNamed:#"Frame1.png"], [UIImage imageNamed:#"Frame2.png"], [UIImage imageNamed:#"Frame3.png"], [UIImage imageNamed:#"Frame4.png"], [UIImage imageNamed:#"Frame5.png"], [UIImage imageNamed:#"Frame6.png"], [UIImage imageNamed:#"Frame7.png"], [UIImage imageNamed:#"Frame8.png"], [UIImage imageNamed:#"Frame9.png"], [UIImage imageNamed:#"Frame10.png"], [UIImage imageNamed:#"Frame11.png"], [UIImage imageNamed:#"Frame12.png"], [UIImage imageNamed:#"Frame13.png"], [UIImage imageNamed:#"Frame14.png"], nil]; [animation setAnimationRepeatCount:1]; animation.animationDuration = 1; [animation startAnimating]; Now I need to state when the animation ends 2 buttons appear, 'menu' and 'replay' Do i use stopanimating or something else, please help and explain as I am a beginner, thanks for any help :)
Well your animation duration is 1 second so you do something like this: animation.animationImages= [NSArray arrayWithObjects: [UIImage imageNamed:#"Frame0.png"], [UIImage imageNamed:#"Frame1.png"], [UIImage imageNamed:#"Frame2.png"], [UIImage imageNamed:#"Frame3.png"], [UIImage imageNamed:#"Frame4.png"], [UIImage imageNamed:#"Frame5.png"], [UIImage imageNamed:#"Frame6.png"], [UIImage imageNamed:#"Frame7.png"], [UIImage imageNamed:#"Frame8.png"], [UIImage imageNamed:#"Frame9.png"], [UIImage imageNamed:#"Frame10.png"], [UIImage imageNamed:#"Frame11.png"], [UIImage imageNamed:#"Frame12.png"], [UIImage imageNamed:#"Frame13.png"], [UIImage imageNamed:#"Frame14.png"], nil]; [animation setAnimationRepeatCount:1]; animation.animationDuration = 1; [animation startAnimating]; [self performSelector:#selector(didFinishAnimating) withObject:nil afterDelay:1.0]; -(void) didFinishAnimating { //animation ended add some buttons }
You can just use an NSTimer to call another method once the animation finishes, buy using the same time interval for both the NSTimer and the animationDuration property. animation.animationImages= [NSArray arrayWithObjects: [UIImage imageNamed:#"Frame0.png"], [UIImage imageNamed:#"Frame1.png"], [UIImage imageNamed:#"Frame2.png"], [UIImage imageNamed:#"Frame3.png"], [UIImage imageNamed:#"Frame4.png"], [UIImage imageNamed:#"Frame5.png"], [UIImage imageNamed:#"Frame6.png"], [UIImage imageNamed:#"Frame7.png"], [UIImage imageNamed:#"Frame8.png"], [UIImage imageNamed:#"Frame9.png"], [UIImage imageNamed:#"Frame10.png"], [UIImage imageNamed:#"Frame11.png"], [UIImage imageNamed:#"Frame12.png"], [UIImage imageNamed:#"Frame13.png"], [UIImage imageNamed:#"Frame14.png"], nil]; [animation setAnimationRepeatCount:1]; animation.animationDuration = 1; [animation startAnimating]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showOtherButtonsAfterAnimation:) userInfo:nil repeats:NO]; The NSTimer will call the method you set in selector(showOtherButtonsAfterAnimation:) of the target object(self) after the selected interval(1 second). The showOtherButtonsAfterAnimation: method needs to have an NSTimer argument even if you never use it in the method. -(void)showOtherButtonsAfterAnimation:(NSTimer*)theTimer { .... whatever ..... }
Trouble accessing objects in NSArray using randNum
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!
how to distribute all image with poper tag
i try to create multiple image array and now i want to add proper tag with specific deck like(Hearts,Diamonds,Spades,Clubs). so in image name as C=dimond, R=Heart,K=spades,F=Clubs and image no as 11=Jack,12=Queen,13=King...so how to distribute it with proper tag so i can indentify specific image. ->>>>>>>> imagearr = [[NSArray alloc] initWithObjects: [UIImage imageNamed:#"C1.png"], [UIImage imageNamed:#"C2.png"], [UIImage imageNamed:#"C3.png"], [UIImage imageNamed:#"C4.png"], [UIImage imageNamed:#"C5.png"], [UIImage imageNamed:#"C6.png"], [UIImage imageNamed:#"C7.png"], [UIImage imageNamed:#"C8.png"], [UIImage imageNamed:#"C9.png"], [UIImage imageNamed:#"C10.png"], [UIImage imageNamed:#"C11.png"], [UIImage imageNamed:#"C12.png"], [UIImage imageNamed:#"C13.png"], [UIImage imageNamed:#"F1.png"], [UIImage imageNamed:#"F2.png"], [UIImage imageNamed:#"F3.png"], [UIImage imageNamed:#"F4.png"], [UIImage imageNamed:#"F5.png"], [UIImage imageNamed:#"F6.png"], [UIImage imageNamed:#"F7.png"], [UIImage imageNamed:#"F8.png"], [UIImage imageNamed:#"F9.png"], [UIImage imageNamed:#"F10.png"], [UIImage imageNamed:#"F11.png"], [UIImage imageNamed:#"F12.png"], [UIImage imageNamed:#"F13.png"], [UIImage imageNamed:#"K1.png"], [UIImage imageNamed:#"K2.png"], [UIImage imageNamed:#"K3.png"], [UIImage imageNamed:#"K4.png"], [UIImage imageNamed:#"K5.png"], [UIImage imageNamed:#"K6.png"], [UIImage imageNamed:#"K7.png"], [UIImage imageNamed:#"K8.png"], [UIImage imageNamed:#"K9.png"], [UIImage imageNamed:#"K10.png"], [UIImage imageNamed:#"K11.png"], [UIImage imageNamed:#"k12.png"], [UIImage imageNamed:#"K13.png"], [UIImage imageNamed:#"R1.png"], [UIImage imageNamed:#"R2.png"], [UIImage imageNamed:#"R3.png"], [UIImage imageNamed:#"R4.png"], [UIImage imageNamed:#"R5.png"], [UIImage imageNamed:#"R6.png"], [UIImage imageNamed:#"R7.png"], [UIImage imageNamed:#"R8.png"], [UIImage imageNamed:#"R9.png"], [UIImage imageNamed:#"R10.png"], [UIImage imageNamed:#"R11.png"], [UIImage imageNamed:#"R12.png"], [UIImage imageNamed:#"R13.png"],nil]; ..............
There's a better work around this issue rather than taking a bunch of images in the array. Initialize an Enum with all different deck categories and loading the image through dynamically generated name for e.g: // In a header file typedef enum { HEARTS, // Hearts DIAMONDS, // Diamonds SPADES, // Spades CLUBS // Club } DeckType; // In a source file NSString * const DeckType_toString[] = { #"H", #"D", #"S", #"C" }; - (UIImage *)getDeckImage:(DeckType)deckType withCardNo:(NSInteger)cardNo { return [UIImage imageNamed:[NSString stringWithFormat:#"%#%d.png", DeckType_toString[deckType], cardNo]]; } You can also take another enum with group of 1...13 cards and used them instead of cardNo
I am not sure,you could create a UIImage with tag .because it's inherited from NSObject NOT UIView which provide you an integer tag variable and used by the UIView class. But still you could do it by writing a wrapper for you UIView class, and should be like below use below as reference. #interface MyUIImageWrapper: NSObject { UIImage* iMyImage; int tag; } you could have some init function which take two argument one for image name and others for tag value Like below -(id) initWithTag:(NSString*) fileName withTag:(int) myTag ; And the implementation for the method. -(id) initWithTag:(NSString*) fileName withTag:(int) myTag { if(self = [super init]) { iMyImage = [UIImage imageNamed:fileName]; tag = myTag; } return self; }
To identify images in a collection I'm suggesting an NSDictionary instead of an NSArray so every image is associated with an NSString, e.g. [[NSDictionary alloc] initWithObjectsAndKeys:..., [UIImage imageNamed:#"R7.png"], #"R7", ...]; tagging works for UIImageViews (all UIView descendants) and is more proper for retrieving already added subviews...
gif image as a background
can we use a gif image as a background? i want to set an gif image as a background, like some birds flying .... can we use gif image to set as background.??? if yes than how? thanks and regards
Yes you can - I assume you want an animated gif? It's the same CSS rule as a static background. body { background: url(animated.gif); }
Here, i got the solution. UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; animatedImageView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:#"default1.jpg"], [UIImage imageNamed:#"default2.jpg"], [UIImage imageNamed:#"default3.jpg"], [UIImage imageNamed:#"default4.jpg"], [UIImage imageNamed:#"default5.jpg"], [UIImage imageNamed:#"default6.jpg"], [UIImage imageNamed:#"default7.jpg"], [UIImage imageNamed:#"default8.jpg"], [UIImage imageNamed:#"default9.jpg"], [UIImage imageNamed:#"default10.jpg"], [UIImage imageNamed:#"default11.jpg"], [UIImage imageNamed:#"default12.jpg"], [UIImage imageNamed:#"default13.jpg"], [UIImage imageNamed:#"default14.jpg"], [UIImage imageNamed:#"default15.jpg"], nil]; animatedImageView.animationDuration = 3.0f; animatedImageView.animationRepeatCount = 0; [animatedImageView startAnimating]; [self.view addSubview: animatedImageView];
Simple Animations [iPhone]
I have a few UIButtons that I'd like to trigger short 5-10 frame animations when pressed in the middle of the screen along with other nonrelated stuffs. Can I just drop one UIImageView in IB and update it, somehow, programmatically? It doesn't need to be overly complex, I don't even mind if it's hard coded, for now since it's such a small project. Thanks.
Here's an example of the easiest way you do an animation with just a few frames is: Declare an IBOutlet UIImageView *timerAnimation and link it to your UIIMageView in IB. Add some number of images to your resources, such as you see needed below. Then load the images into an array - see the code: timerAnimation.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:#"timer00.png"], [UIImage imageNamed:#"timer01.png"], [UIImage imageNamed:#"timer02.png"], [UIImage imageNamed:#"timer03.png"], [UIImage imageNamed:#"timer04.png"], [UIImage imageNamed:#"timer05.png"], [UIImage imageNamed:#"timer06.png"], [UIImage imageNamed:#"timer07.png"], [UIImage imageNamed:#"timer08.png"], [UIImage imageNamed:#"timer09.png"], [UIImage imageNamed:#"timer10.png"], [UIImage imageNamed:#"timer11.png"], [UIImage imageNamed:#"timer12.png"], [UIImage imageNamed:#"timer13.png"], [UIImage imageNamed:#"timer14.png"], [UIImage imageNamed:#"timer15.png"], [UIImage imageNamed:#"timer16.png"], [UIImage imageNamed:#"timer17.png"], [UIImage imageNamed:#"timer18.png"], [UIImage imageNamed:#"timer19.png"], [UIImage imageNamed:#"timer20.png"], [UIImage imageNamed:#"timer21.png"], [UIImage imageNamed:#"timer22.png"], [UIImage imageNamed:#"timer23.png"], [UIImage imageNamed:#"timer24.png"], [UIImage imageNamed:#"timer25.png"], [UIImage imageNamed:#"timer26.png"], [UIImage imageNamed:#"timer27.png"], [UIImage imageNamed:#"timer28.png"], [UIImage imageNamed:#"timer29.png"], [UIImage imageNamed:#"timer30.png"], [UIImage imageNamed:#"timer31.png"], [UIImage imageNamed:#"timer32.png"], [UIImage imageNamed:#"timer33.png"], [UIImage imageNamed:#"timer34.png"], [UIImage imageNamed:#"timer35.png"], // [UIImage imageNamed:#"timer36.png"], save last picture for "times up" nil]; timerAnimation.animationDuration = 5.0; timerAnimation.animationRepeatCount = 1; [timerAnimation startAnimating]; This will run your animation until you call [timerAnimation stopAnimating];
Yes. Create an IBOutlet to your UIImageView and you can update it from code. For example, use its image property to set the UIImage that should be displayed. Or you can add the image to your app's resources and associate it with the UIImageView in Interface Builder. Use .png files since the iPhone has been optimized for them. Since a UIImageView is a UIView, all the usual Core Animation methods can be used.