I'm trying to fade from one image to another, using a SINGLE UIView (Yes! have seen Q&As on stackoverflow, that use 2 UIViews - on top of each other).
I have a current UIView image, would like to fade it out and replace it with another image, and fade the new image in.
I have tried the following: The fading of the first , alone, works.
Attempt #1)
// Fade original image - image already set in Storyboard
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5.0];
[UIView setAnimationDelegate:self];
[_imgMirror setAlpha:0.0];
[UIView commitAnimations];
// Fade in new image
[_imgMirror setImage:[UIImage imageNamed:#"NewImage"]];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5.0];
[_imgMirror setAlpha:100.0];
[UIView commitAnimations];
This fades in new image... Old image does not fade. Also tried 'pausing' between the 2 groups of code.
Attempt #2)
UIImage * toImage = [UIImage imageNamed:#"NewImage"];
[UIView transitionWithView:self.imgMirror
duration:0.33f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.imgMirror.image = toImage;
} completion:NULL];
This shows the new image immediately, never fades or 'CrossDissolves'.
Attempt #3)
// Fade original image - image already set in Storyboard
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5.0];
[UIView setAnimationDelegate:self];
[_imgMirror setAlpha:0.0];
[UIView commitAnimations];
UIImage * toImage = [UIImage imageNamed:#"NewImage"];
[UIView transitionWithView:self.imgMirror
duration:0.33f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.imgMirror.image = toImage;
} completion:NULL];
This has the same result as #2.
Any idea what is wrong?
Try this. It fades the image out, swaps the image without an animation, then fades in with another animation.
CGFloat fadeDuration = 0.5f;
[UIView animateWithDuration:fadeDuration animations:^{
[_imgMirror setAlpha:0.0];
} completion:^(BOOL finished) {
UIImage * toImage = [UIImage imageNamed:#"NewImage"];
_imgMirror.image = toImage;
[UIView animateWithDuration:fadeDuration animations:^{
[_imgMirror setAlpha:1.0];
}];
}];
You cannot "cross fade" from one UIImage to another by setting the .image property of your image view.
You can do it with an "intermediary" image view though.
Assuming you have a UIImageView connected via IBOutlet, and a button connected via IBAction:
//
// CrossFadeViewController.m
//
// Created by Don Mag on 3/7/18.
//
#import "CrossFadeViewController.h"
#interface CrossFadeViewController ()
#property (strong, nonatomic) IBOutlet UIImageView *imgMirror;
#end
#implementation CrossFadeViewController
- (IBAction)didTap:(id)sender {
[self crossFadeTo:#"NewImage"];
}
- (void) crossFadeTo:(NSString *)newImageName {
UIImageView *toImageView;
// create a new UIImageView with the new Image
toImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:newImageName]];
// set the new image view's frame to match the existing one
toImageView.frame = _imgMirror.frame;
// cross-fade / dissolve from the existing image view to the new one
[UIView transitionFromView:_imgMirror
toView:toImageView
duration:0.33
options:UIViewAnimationOptionTransitionCrossDissolve
completion:^(BOOL finished) {
// when animation is finished
// remove the "old" image view from its superview
[_imgMirror removeFromSuperview];
// assign the new image view to the IBOutlet property
_imgMirror = toImageView;
}];
}
#end
Related
I want to make an Menu Animation just as shown below.
I need to have UIButtons inside the menu.
How can i achieve this.
for this animation you should set anchor point by default the anchor point position is (0.5,0.5), so can change the anchor point position:
All geometric manipulations to the view occur about the specified point. For example, applying a rotation transform to a layer with the default anchor point causes the layer to rotate around its center. Changing the anchor point to a different location would cause the layer to rotate around that new point.
self.view.layer.anchorPoint = CGPointMake(0,0);
self.view.transform = CGAffineTransformMakeScale(0.001,0.001);
[UIView animateWithDuration:1.0 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
Please add the follow code on button click:
[UIView animateWithDuration:1
animations:^{
myView.transform = CGAffineTransformMakeScale(1.5, 1.5); }
completion:^(BOOL finished) {
[UIView animateWithDuration:1
animations:^{
myView.transform = CGAffineTransformIdentity;
}]; }];
It will work for you.
Create Super class SuperViewController.m
Add this method
- (void)viewDidLoad {
[super viewDidLoad];
//Here u can set image also in navigation bar
UIBarButtonItem *buttonC = [[UIBarButtonItem alloc]initWithTitle:#"Word" style:UIBarButtonItemStylePlain target:self action:#selector(GeTVAlue)];
[self.navigationItem setLeftBarButtonItem:buttonC];
}
-(void)GeTVAlue{
UIView *paintView=[[UIView alloc]initWithFrame:CGRectMake(-350, -350, 350, 350)];
[paintView setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:paintView];
[UIView animateWithDuration:1
animations:^{
paintView.transform = CGAffineTransformMakeScale(2.5, 2.5); }
completion:^(BOOL finished) {
[UIView animateWithDuration:1
animations:^{
paintView.transform = CGAffineTransformIdentity;
}]; }];
}
Then Inherit your Super class in your class
#interface TheerdViewController : SuperViewController
If u have any question fill free ask me.
Thnak you
I have set the animation like seen in below image. In which UIbutton move from left to right and then top to bottom. Animation work correctly but after completion of animation UIButton comes to its original place before the segue perform. so, it's not look good. I want to set that after the completion of animation UIButton can't come to it's own place before segue .
Here is my try with Image.
//Move button Left to Right
- (IBAction)btnEasy:(id)sender {
Easy=YES;
NSLog(#"your x is: %f ", self.btnEasy.frame.origin.x);
NSLog(#"your y is: %f ", self.btnEasy.frame.origin.y);
x1=self.btnEasy.frame.origin.x;
y1=self.btnEasy.frame.origin.y;
CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView animateWithDuration:1.150 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.btnEasy.frame = CGRectMake(screenRect.size.width/1.80, self.btnEasy.frame.origin.y, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height);
[self performSelector:#selector(btneasyanimation) withObject:self afterDelay:1.160 ];}
completion:^(BOOL finished) {
}];
//Move Button top to Bottom
if (Easy==YES) {
if (isiPad2 || isiPadAir || isiPadRatina) {
//[self.view layoutIfNeeded];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:1.0];
[_btnEasy setFrame:CGRectMake(self.view.frame.origin.x+290, self.view.frame.origin.y+900, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height)];
[UIView commitAnimations];
}
else if (isiPhone4s) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:1.0];
[_btnEasy setFrame:CGRectMake(self.view.frame.origin.x+92, self.view.frame.origin.y+428, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height)];
[UIView commitAnimations];
}
}
[self performSelector:#selector(segueeMethod) withObject:self afterDelay:1.160 ];
Image :-
If you are not placing directly the button given it the frame, thus using autolayout (autoresize will end with the same effect). You need to explicitly use autolayout, retain a reference to your constraints and update them (you can search here how to do that) and then set the UIView animation block [button layoutIfNeeded]
You can see a detailed answer about it in this answer
There is an easy and quick way that will work with your current implementation. Provided you know exactly where you want the view to be once the animation is done, you can do the following:
in UIView animateWithDuration: ... assign the final transform (position & orientation) of the view in the completion block, i.e.:
completion:^(BOOL finished) {
// Assign views transform and appearance here, for when the animation completes
}
Hope that helps.
I am doing an image transition using an animation block like this
[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.songTitleLabel.text = currentSong.songTitle;
self.artistNameLabel.text = currentSong.songArtist;
self.songImageView.image = currentSong.songArtwork;
}completion:^(BOOL finished){
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentSong.songLocation error:nil];
[self.player setVolume:1.0];
[self.player play];
}];
I don't understand why I don't have a fade transition, I have tried several different UIAnimationOptionTransition... and every time I just get an abrupt jump to the next image (this happens to the text as well)
Can someone help me out?
You can't animate the changing of an image or text that way. I've done it like this,
-(IBAction)doStuff:(id)sender {
[UIView transitionWithView:self.imageView
duration:1.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.imageView setImage:[UIImage imageNamed:#"IMG_0081.JPG"]];
} completion:nil];
[UIView transitionWithView:self.label2
duration:1.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.label2 setText:#"New Text"];
} completion:nil];
}
If you're changing multiple labels or image views, it might be better to create a subclass, and override setText: and setImage:. For example, like this for setImage:,
-(void)setImage:(UIImage *)image {
[UIView transitionWithView:self
duration:1.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[super setImage:image];
} completion:nil];
}
You can then just use,
self.imageView.image = ...
for any image view that's your subclass, and it will cross fade the images when you change them.
I have a UIButton in a UIViewController that I currently have "fading in" to view when the app is opened. I would like to get the UIButton to slide in from left to right instead though.
I can't figure out how to get it to slide in from left to right and my attempts have failed, even though I've got the "fade in" thing down solid.
Any help you could give me? Thanks! I can add any more code as needed-
ViewController.h
#property (weak, nonatomic) IBOutlet UIButton *buttonOne;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSTimer *timermovebutton = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(movebutton) userInfo:nil repeats:NO];
[timermovebutton fire];
}
-(void) movebuttontwo
{
buttonTwo.alpha = 0;
[UIView beginAnimations:Nil context:Nil];
[UIView setAnimationDelay:0.5];
[UIView setAnimationCurve:UIViewAnimationTransitionCurlUp];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0];
buttonTwo.alpha = 1.0;
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
You need to animate the buttons frame, not use an animation curve.
Since iOS 4, we've had UIView animation blocks:
// first set the UIButton frame to be outside of your view:
// we are only concerned about it's location on the X-axis so let's keep all properties the same
[buttonTwo setFrame:CGRectMake(CGRectGetMaxX(self.view.frame), CGRectGetMinY(buttonTwo.frame), CGRectGetWidth(buttonTwo.frame), CGRectGetHeight(buttonTwo.frame))];
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationCurveEaseOut
animations:^{
// set the new frame
[buttonTwo setFrame:CGRectMake(0, CGRectGetMinY(buttonTwo.frame), CGRectGetWidth(buttonTwo.frame), CGRectGetHeight(buttonTwo.frame))];
}
completion:^(BOOL finished){
NSLog(#"Done!");
}
];
There's a good tutorial over at Ray Wenderlich you can check out to learn more.
//Set your button off the screen to the left
yourButton.frame = CGRectMake(-yourButton.frame.size.width, yourButton.frame.origin.y, CGRectGetWidth(yourButton.frame), CGRectGetHeight(yourButton.frame));
//Create the ending frame or where you want it to end up on screen
CGRect newFrm = yourButton.frame;
newFrm.origin.x = 100;
//Animate it in
[UIView animateWithDuration:2.0f animations:^{
yourButton.frame = newFrm;
}];
I need my app to move an image when the user clicks on a certain button. This works perfectly:
ViewController.m
#import "ndpViewController.h"
#interface ndpViewController ()
#property (nonatomic, retain) IBOutlet UIImageView *image;
-(void)movetheball;
#end
#implementation ndpViewController
#synthesize image;
int ballx, bally;
-(void)movetheball {
[UIView beginAnimations: #"MovingTheBallAround" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
image.frame = CGRectMake(ballx,bally,image.frame.size.width,image.frame.size.height);
[UIView commitAnimations];
}
- (void)viewDidLoad {
}
- (IBAction)calculatePush:(id)sender {
ballx = 600;
bally = 800;
[self movetheball];
}
#end
But as soon as the app has to do something else (when user clicks the button), like adding a text to a label, which I do in this way:
_lizfwLabel.text=[NSString stringWithFormat:#"%.2f",lizfw];
then the app will move the image only upon clicking the button a second time.
Any idea on why is this happening? Thank you!
Try to use another one method to animate UIView's in iOS. Apple advices to use [UIView animate...] rather then old style [UIView begin/commitAnimations];.
In your case you should replace movetheball method code to that one:
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect frame = image.frame;
frame.origin.x = ballx;
frame.origin.y = bally;
image.frame = frame;
} completion:nil];