I am making an image editor application.
I am able to change the UIImage of a UIImageView but I want it to be more efficient and beautiful.
Currently I am changing UIImage as
if(image)
{
userImageView.image = image;
}
It directly changes the UIImage. How can I make a fadeIn-fadeOut effect?
UIImage *image = [UIImage imageWithData:imageData];
[UIView transitionWithView:YourUIImageView
duration:2.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
YourUIImageView.image = image;
} completion:nil];
And Do some googling before asking this question already has and answer.
Check THIs
try this...
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.fadeDuration=1;
}
return self;
}
-(void)setImage:(UIImage *)newImage{
if(!self.image||self.fadeDuration<=0){
super.image=newImage;
} else {
UIImageView *iv=[[UIImageView alloc] initWithFrame:self.bounds];
iv.contentMode=self.contentMode;
iv.image=super.image;
iv.alpha=1;
[self addSubview:iv];
super.image=newImage;
[UIView animateWithDuration:self.fadeDuration delay:0 options:UIViewAnimationCurveEaseInOut animations:^{
iv.alpha=0;
} completion:^(BOOL finished) {
[iv removeFromSuperview];
}];
}
}
Try something like this:
-(void)fade{
//fade out
[UIView animateWithDuration:1.0f animations:^{
[userImageView setAlpha:0.0f];
} completion:^(BOOL finished) {
//fade in
[UIView animateWithDuration:1.0f animations:^{
userImageView.image = image;
[userImageView setAlpha:1.0f];
} completion:nil];
}];
}
Related
i got a button to show an image in the UIImageView. what i need to do is to get this image to fade out by itself in 2 seconds, is it possible to do so?
here is my code on the button:
- (IBAction)A {
UIImage *Img = [UIImage imageNamed:#"AString.png"];
[ImageView setImage:Img];
}
thank you in advance...
Try this:
- (IBAction)A
{
UIImage *Img = [UIImage imageNamed:#"AString.png"];
[ImageView setImage:Img];
Img.hidden = NO;
Img.alpha = 1.0f;
[UIView animateWithDuration:2 delay:0 options:0 animations:^{
Img.alpha = 0.0f;
} completion:^(BOOL finished) {
Img.hidden = YES;
}];
}
Try this code :
yourImageView.hidden = YES;
[UIView transitionWithView:yourImageView
duration:2
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.view layoutIfNeeded]; // Called on parent view
}
completion:NULL];
I'm making a very simple game. I have a UIButton and an UIImageView. What I'm trying to do is that when the user presses the button, it will change the image to another image and then back to the original image, to simulate an animation (Character moving)
I have this code:
file.m
{
IBOutlet UIImageView *image1;
IBOutlet UIButton *button;
}
-(IBAction)button:(id)sender;
file.h
-(IBAction)button:(id)sender{
[UIView animateWithDuration:1.0f
animations:^{
image1.alpha = 0.1f;
} completion:^(BOOL finished) {
image1.image = [UIImage imageNamed:#"image2.png"];
[UIView animateWithDuration:0.2f
animations:^{
image1.alpha = 1.0f;
} completion:^ (BOOL finished) {
image1.image = [UIImage imageNamed:#"image1.png"];
}];
}];
}
When I open the iOS simulator and press the button, the image dissolves, then reappears and then does the animation. The problem is that I don't want it to dissolve, I just want to show the animation. How do I prevent this from happening? Is it because I'm using alpha properties?
Thank you!
I used the following code to do what I think is what you need (shown here: https://www.dropbox.com/s/vtcyw0n257tgihx/fade.mov?dl=0):
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(test)];
iv = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];
iv.image = [UIImage imageNamed:#"red"];
[self.view addSubview:iv];
}
- (void)test {
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 0.0f;
} completion:^(BOOL finished) {
iv.image = [UIImage imageNamed:#"blue"];
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 1.0f;
} completion:^ (BOOL finished) {
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 0.0f;
} completion:^ (BOOL finished) {
iv.image = [UIImage imageNamed:#"red"];
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 1.0f;
} completion:nil];
}];
}];
}];
}
You need to break it down into four parts. First you need to fade the original image out, change it to the second image, fade the second image in, fade the second image out, change back to the original image, then fade it back in.
I have this code:
UIImageView *_propLoading;
- (void)viewDidLoad
{
UIImage *image = [UIImage animatedImageNamed:#"loading" duration:.8f];
[_propLoading setImage:image];
}
This works perfectly, but i need move around screen at the same time
i try this code but not works.
-(void)viewDidAppear:(BOOL)animated{
[UIView animateWithDuration:2
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
_propLoading.frame = CGRectMake(22, 33, 134, 232);
}
completion:^(BOOL finished){
}];
}
Try:
[UIView transitionWithView:_propLoading
duration:2.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
_propLoading.frame = CGRectMake(22, 33, 134, 232);
[_propLoading setImage:image];
} completion:NULL];
I am trying to flip transition on the same ViewController, I have a button for trigger transition. I want to change theme color when i press to button via flip transition.
i used this code part:
- (IBAction)changeThemeButtonTapped:(id)sender
{
int tempThemeColor = [[NSUserDefaults standardUserDefaults]integerForKey:#"themeColor"];
if (tempThemeColor == 10) {
[self loadTheme];
[UIView transitionFromView:self.view
toView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished) {
NSLog(#"finished");
}];
}else{
[self loadTheme];
[UIView transitionFromView:self.view
toView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
completion:^(BOOL finished) {
NSLog(#"finished");
}];
}
}
when i press to button, view turns to black. Is there any way to do? What is my mistake?
Thank you for your answer and interest.
I found my question's answer.
if (tempThemeColor == 10) {
[self loadTheme];
[UIView transitionWithView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
} completion:^(BOOL finished) {
}];
}else{
[self loadTheme];
[UIView transitionWithView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
} completion:^(BOOL finished) {
}];
}
Before doing animation try to change the window/super view of the view's color
if (self.view.superview) {
self.view.superview.backgroundColor = [UIColor whiteColor];
}
self.view.window.backgroundColor = [UIColor whiteColor];
Ok So In My Xcode project I Have A button That I need to do the same method twice but with A Different Image Plz Help
Code:
UIImage * toImage = [UIImage imageNamed:#"CharacterBackwards.png"];
[UIView transitionWithView:self.view
duration:0.01f
options:UIViewAnimationOptionTransitionNone
animations:^{
self->Guy.image = toImage;
} completion:NULL];
sleep(0.4);
This is what methods are for. Create a method that takes an image name:
- (void)transitionToImage:(NSString *)imageName {
UIImage * toImage = [UIImage imageNamed:#"CharacterBackwards.png"];
[UIView transitionWithView:self.view
duration:0.01f
options:UIViewAnimationOptionTransitionNone
animations:^{
self->Guy.image = toImage;
} completion:NULL];
}
Now have your button handler call this twice:
- (IBAction)buttonHandler {
[self transitionToImage:#"CharacterBackwards.png"];
[self performSelector:#selector(transitionToImage:) withObject:#"CharacterForwards.png" afterDelay:0.4];
}