Move UIImage around the screen - ios

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];

Related

How to change UIImageView with some Transition

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];
}];
}

How to make an image fade out by itself in a few seconds?

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];

iOS: Changing a UIImageView by pressing an UIButton

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.

UIViewAnimation Flip not working

In a UIView subclass I have this:
self.frontImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"front"]];
self.backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"back"]];
[self addSubview:self.backImageView];
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[self.backImageView removeFromSuperview];
[self addSubview:self.frontImageView];}
completion:^(BOOL finished){
//nothing
}];
But I don't get the flip animation. The front image view just appears immediately. What am I doing wrong?
You are making a silly mistake using
animateWithDuration:delay:options:animations:completion:
instead of
transitionFromView:toView:duration:options:completion:
Your code should be:
[UIView transitionFromView:backImageView
toView:frontImageView
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished) {
// animation completed
}];

block animation delay is not working

When this method is first called, it works good.
On second call, the delay does not work.
The other code works good except for the delay.
How do I solve this problem?
-(void)makeParticle:(id)sender{
Bubble *bubble=(Bubble*)sender;
CGPoint center=bubble.center;
NSInteger radian=arc4random()%360;
for (int i=1; i<=13; i++) {
NSInteger distance=20+arc4random()%80;
radian+=10+arc4random()%40;
UIImageView *iv=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"star%i.png",i]]]autorelease];
NSInteger size=20+arc4random()%20;
iv.frame=CGRectMake(0, 0, size, size);
iv.center=center;
iv.alpha=0;
[self addSubview:iv];
[iv release];
[UIView animateWithDuration:0.4 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction animations:^(void) {
iv.alpha=0.5;
iv.center=CGPointMake(center.x+distance*cos(radian), center.y+distance*sin(radian));
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction animations:^(void) {
iv.center=CGPointMake(600, 100);
iv.alpha=0;
} completion:^(BOOL finished) {
[iv removeFromSuperview];
}];
}];
}
}

Resources