0

Just started Objective-C - trying to make 4 images fadein/fadeout continuously.

The code below is not working. I know the button is working because the text of my label changes, however no pictures animate. No errors either ...

h.

#import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UIImageView *viewer; } @property (nonatomic, retain) UIImageView *viewer; @property (nonatomic,retain) IBOutlet UILabel *label1; @end 

m.

-(IBAction)startImageViewAnimation { label1.text = @"CHECK?"; NSArray *animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"backgrounds_app1-01.png"], [UIImage imageNamed:@"backgrounds_app2-02.png"], [UIImage imageNamed:@"backgrounds_app3-03.png"], [UIImage imageNamed:@"backgrounds_app4-04.png"], nil]; viewer = [[UIImageView alloc] initWithFrame:self.view.frame]; viewer.animationImages = animationImages ; viewer.animationRepeatCount = 2; viewer.animationDuration= 4.0; [viewer startAnimating]; [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(animationDone:) userInfo:nil repeats:NO]; } -(void)animationDone:(NSTimer*)inTimer { [inTimer invalidate]; inTimer = nil; NSLog(@"animationDone "); } 
3
  • change the alloc line to: viewer = [[UIImageView alloc] initWithFrame:self.view.bounds] Commented Apr 10, 2014 at 0:41
  • @rounak did it - still nothing. Commented Apr 10, 2014 at 0:45
  • Does it matter that I set the image of the UIImageView? Or does it have to be blank? Commented Apr 10, 2014 at 0:45

1 Answer 1

2

After initialising the image view, you'll have to add it as a subview:

viewer = [[UIImageView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:viewer]; 
Sign up to request clarification or add additional context in comments.

8 Comments

Perfect! - 2 questions: a) what does that do? is it applicable to other situations?
b) can the animation be smoother?
a: yes, without adding the subview to the view it exists, but will not show up. This counts for any view.
b:if you want to create a fade-in/out animation, consider using [UIView animateWithDuration] and change the alpha value of the image accordingly
Doubt the alpha of the image itself can be changed. You'll need to have at least two UIImageViews and change the alpha of that view.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.