I am hew here and need help.
I work with storyboard and want make a automatically fade in fade out photo show.
The problem is that i have 10 images and don't now how to make this Programmatically.
I hope somebody can help me.
mainImageView = [[UIImageView alloc]init];
NSArray *animationArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"],[UIImage imageNamed:#"third.png"],[UIImage imageNamed:#"fourth.png"],[UIImage imageNamed:#"fifth.png"],[UIImage imageNamed:#"sixth.png"],[UIImage imageNamed:#"seventh.png"],[UIImage imageNamed:#"eight.png"],[UIImage imageNamed:#"nine.png"],[UIImage imageNamed:#"ten.png"], nil]; //add your images here
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(crossfade) userInfo:nil repeats:YES];
[mainImageView setFrame:CGRectMake(50,50,100,100)];
mainImageView.animationImages = animationArray; //mainImageView is imageview
mainImageView.animationDuration = 10;
mainImageView.animationRepeatCount = 0;
[mainImageView startAnimating];
[self.view addSubview:mainImageView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)crossfade {
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
mainImageView.alpha = !mainImageView.alpha;
}completion:^(BOOL done){
//
}];
}
Related
I need to animate a UIImageView inside my application, and cyclically change the UIImage inside it, making it look like an animated photo slideshow.
Right now I'm using an NSTimer to fire every N seconds the UIImage change and the animation itself:
- (void)viewDidLoad {
// NSArray initialization
NSTimer *timer = [NSTimer timerWithTimeInterval:16
target:self
selector:#selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
[super viewDidLoad];
}
This is the onTimer selector code:
- (void) onTimer {
// cycle through max 9 images
if(imageIndex > 8)
imageIndex = 0;
// set the image
[_imageContainer setImage:[images objectAtIndex:imageIndex]];
// reset width and height of the UIImage frame
CGRect frame = [_imageContainer frame];
frame.size.width -= 170.0f;
frame.size.height -= 100.0f;
[_imageContainer setFrame:frame];
// fade in
[UIView animateKeyframesWithDuration:2.0f delay:0.0f options:0 animations:^{
[_imageContainer setAlpha:1];
} completion:^(BOOL finished) {
// image movement
[UIView animateKeyframesWithDuration:12.0f delay:0.0f options:0 animations:^{
CGRect frame = [_imageContainer frame];
frame.size.width += 170.0f;
frame.size.height += 100.0f;
[_imageContainer setFrame:frame];
} completion:^(BOOL finished) {
// fade out
[UIView animateKeyframesWithDuration:2.0f delay:0.0f options:0 animations:^{
[_imageContainer setAlpha:0];
} completion:nil];
}];
}];
imageIndex++;
}
This seem a very raw but "working" way to achieve what I want but I recognize it might not be the ideal way.
Is there any better method to achieve what I'm looking for?
Updated Answer For Fade Animation
- (void)viewDidLoad
{
[super viewDidLoad];
// self.animationImages is your Image Array
self.animationImages = #[[UIImage imageNamed:#"Image1"], [UIImage imageNamed:#"Image2"]];
// make the first call
[self animateImages];
}
- (void)animateImages
{
static int count = 0;
UIImage *image = [self.animationImages objectAtIndex:(count % [animationImages count])];
[UIView transitionWithView:self.animationImageView
duration:1.0f // animation duration
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.animationImageView.image = image; // change to other image
} completion:^(BOOL finished) {
[self animateImages]; // once finished, repeat again
count++; // this is to keep the reference of which image should be loaded next
}];
}
- (IBAction)press:(id)sender {
UIImage *bomb = [UIImage imageNamed:#"bom"];
UIImageView *bom =[[UIImageView alloc] initWithImage:bomb];
[bom setFrame: CGRectMake(83,115,35,35)];
[self.view addSubview:bom];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
[bom setFrame:CGRectMake(149,47,35,35)];
}
completion:^(BOOL finished)
{
[bom setFrame:CGRectMake(134,40,60,55)];
[bom setImage:[UIImage imageNamed:#"splash"]];
hits++;
_hit.text=[NSString stringWithFormat:#"%i",hits];
}];
}
Here is my code! The object bom here handles the imageview for splash.png image and i want that image to be appeared only for 0.5 sec when each time the button is pressed
You could set up an NSTimer that calls a method which hides the image. You would have to keep a reference to the bomb.
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(hideImage) userInfo:nil repeats:NO];
-(void)hideImage {
self.bomb.hidden = YES;
}
I have an imageView in top of my screen, and what i want is - to switch images with specific time interval, and to make it with animation (I'm not specify what really animation should be, that could be fade for example). I have suggested to add that code snippet:
// load all the frames of our animation
self.imageSlideshow.animationImages = [NSArray arrayWithObjects:
self.firstImageIps, self.secondImageIps, self.thirdImageIps, self.fourthImageIps, self.fifthImageIps, nil];
// all frames will execute in 1.75 seconds
self.imageSlideshow.animationDuration = 1.75;
// repeat the animation forever
self.imageSlideshow.animationRepeatCount = 0;
// start animating
NSLog(#"that one called?");
[self.imageSlideshow startAnimating];
But images here only switching, without any animation.
And here is more serious issues - if i set duration to more, then 3 seconds, it start act weird (not of all images show, first image show more often then second and third. It look like it can't achieve a full cycle and move to first image after some short time again and again).
How to achieve switching images with animation, with timer interval, for example 5-7 seconds?
Any advice would be appreciated, thanks!
Try this one:
- (void)viewDidLoad {
[super viewDidLoad];
self.imgArr = [[NSArray alloc] initWithObjects:#"DSC06715.JPG",#"Snapshot_20121113_18.JPG",#"IMAG1689.jpg",#"IMAG1720.jpg",#"IMAG1396.jpg", nil];
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:#selector(performTransition) userInfo:nil repeats:YES];
}
-(void)performTransition
{
int randImg = arc4random()%self.imgArr.count;
[self.imageView1 setImage:[UIImage imageNamed:[self.imgArr objectAtIndex:randImg]]];
CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
NSString *types[4] = {kCATransitionFade};
transition.type = types[1];
transition.delegate = self;
[self.imageView1.layer addAnimation:transition forKey:nil];
}
-(void)animateImages
{
count++;
[UIView transitionWithView:imageSlideshow
duration:2.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
imageSlideshow.image = [imagesArray objectAtIndex: count % [imagesArray count]];
} completion:^(BOOL finished) {
[self animateImages];
}];
}
This code snippet will help you to add any type of animation.
One of the simplest ways to do animation.
Code example :
[UIView animateWithDuration:0.3 animations:^{
//change alpha with animation.
self.friendsTableView.alpha = 1.f;
} completion:^(BOOL finished) {
}];
This animation could be set with delay and optionsas well. As far as i know it is the simplest way to set proper animation with less afford.
To make animation between 2 view you may use spacial UIView method:
- (void)makeCrossDissolveAnimation {
NSArray *images = self.imageSlideshow.animationImages;
static int counter = 0;
UIImageView *startView = [[UIImageView alloc] initWithImage:images[counter]];
counter = (counter+1) % [images count];
UIImageView *endView = [[UIImageView alloc] initWithImage:images[counter]];
[UIView transitionFromView:startView toView:endView duration:ELAnimationDuration
options:UIViewAnimationOptionTransitionCrossDissolve
completion:^(BOOL finished) {
// Your completion block
}];
}
I am trying to get a button to move to random locations inside my view. However, the dot only moves at random when it is pressed. I also want it to fade to a different location if it is not pressed. I used CGRect because I need the dot to stay within the bounds of a specific view.
-(IBAction)randomRed:(id)sender
{
[self redDot];
CGRect senderFrame = [sender frame];
CGRect superBounds = [[sender superview ] bounds];
senderFrame.origin.x = (superBounds.size.width - senderFrame.size.width) * drand48();
senderFrame.origin.y = (superBounds.size.height - senderFrame.size.height) * drand48();
[sender setFrame:senderFrame];
counter = counter - 5;
scoreLabel.text = [NSString stringWithFormat:#"Points %i", counter];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Tapadot Red Dot Buzzer Short" ofType:#"mp3"];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
sound.numberOfLoops = 1;
[sound play];
[self subtractOne];
[self performSelector:#selector(showRedDot) withObject:nil afterDelay:1.0];
}
-(void)startRedDot
{
if (counter ==0)
redDot = [NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:#selector(showRedDot)
userInfo:nil
repeats:YES];
}
-(void)showRedDot
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[redButton setAlpha:1];
[UIView commitAnimations];
[self performSelector:#selector(redDot) withObject:nil afterDelay:1.0];
}
-(void)redDot
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[redButton setAlpha:0];
[UIView commitAnimations];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setUpGame];
[self sizing];
}
Currently your random positioning code is only called when the button is pressed.
To get it to move Independent of button presses we should put it in its own method, which can invoke itself after a set duration.
First we need to get a reference to the instance of the button you want to move so we can manipulate it.
Then lets separate the movement code into its own method.
- (void) moveButtonRandomly {
CGSize limits;
CGPoint newPosition;
// Get limits
limits.width = self.view.frame.size.width - button.frame.size.width;
limits.height = self.view.frame.size.height - button.frame.size.height;
// Calculate new Position
newPosition = (CGPoint){ limits.width * drand48(), limits.height * drand48() };
// Set new frame
button.frame = (CGRect){ newPosition, button.frame.size };
}
Change your startRedDot to this, which will construct, and start the timer.
- (void) startRedDot {
redDotTimer = [NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:#selector(moveButtonRandomly)
userInfo:nil
repeats:YES];
[redDotTimer fire];
}
The animation is rather simple to add just create a new method which invokes two animations; One which fades out the button with a completion that moves the button, and fires off the next animation to fade it back in.
- (void) moveButtonWithAnimation {
CGFloat fadeDurration;
if ( !button )
return; // only invoke the button if it exists
fadeDurration = 0.4;
//Fade Out
[UIView animateWithDuration: fadeDurration animations:^{
button.alpha = 0.0f;
} completion:^(BOOL finished) {
// Move the button while it is not visible
[self moveButtonRandomly];
// Fade in
[UIView animateWithDuration: fadeDurration animations:^{
button.alpha = 1.0f;
}];
}];
}
Finally Edit the timers selector to be the animation method.
- (void) startRedDot {
redDotTimer = [NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:#selector(moveButtonWithAnimation)
userInfo:nil
repeats:YES];
[redDotTimer fire];
}
I have a multiple images to show in imageview using cross dissolve animation and taping on image displays detail about image. Following is the code working but tap on imageview is failing when it's animating from one image to another.
Code for attaching single tap gesture recogniser to UIImageview and starting animation timer.
self.imageView.userInteractionEnabled = YES;
self.singleTapGestureRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showDetails)];
self.singleTapGestureRecogniser.numberOfTapsRequired = 1;
self.singleTapGestureRecogniser.cancelsTouchesInView = NO;
[self.imageView addGestureRecognizer:self.singleTapGestureRecogniser];
[self startAnimationTimer];
- (void)startAnimationTimer
{
if(!self.timer && ![self.timer isValid]) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(startAnimation)
userInfo:nil
repeats:YES];
[self.timer fire];
}
}
Single tap on imageview works fine unless following code is executing! I have to tap 3-4 times to open details about image.
- (void)startAnimation
{
[UIView transitionWithView:self.imageView duration:2.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^ {
currentImageIndex = ++currentImageIndex % self.images.count;
[self.imageView setImage:[[self.images objectAtIndex:currentImageIndex] image]];
} completion:^(BOOL finished) {
}
];
}
Could someone please suggest me how to get rid of this issue please.
set UIViewAnimationOptionAllowUserInteraction in the options like so
[UIView transitionWithView:self.imageView duration:2.0
options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction
animations:^ {
currentImageIndex = ++currentImageIndex % self.images.count;
[self.imageView setImage:[[self.images objectAtIndex:currentImageIndex] image]];
} completion:^(BOOL finished) {
}
];