UIImage view - animation view logic error xCode 4.2 - ios

I am trying to make an animation that it will changes some pictures that i have already in the app.
After searching around i found something that works pretty well for me.
Code below:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1.jpg"],
[UIImage imageNamed:#"photo2.jpg"],
[UIImage imageNamed:#"photo3.jpg"], nil];
animationView.animationDuration = 5;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
So even this works great for me, it gives me a logic error (the yellow triangle sign) and it says
"Local declaration of 'animationView' hides instance variable"
Even tho i run the code and it does what i want it to do, i cant understand why i have this error.
Also to note that this takes place inside an UIImage that i have declared as an outlet
And one extra question: How can i add the fadein - fadeout effect ?
Thanks in advance

You must have declared UIImageView *animationView as either an instance variable or a property.
You can safely remove the UIImageView * part in your viewDidLoad as there is no need to re-declare animationView as UIImageView since compiler already knows about it and that's why it gives you the warning.
So it would look like this:
- (void)viewDidLoad
{
[super viewDidLoad];
animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1.jpg"],
[UIImage imageNamed:#"photo2.jpg"],
[UIImage imageNamed:#"photo3.jpg"], nil];
animationView.animationDuration = 5;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}

As for the fade effect you could do something like:
//Fade in
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[animationView setAlpha:1];
[UIView commitAnimations];
//Fade out
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[animationView setAlpha:0];
[UIView commitAnimations];

Related

UIView block animation not working

I have a custom view (which draws callouts/bubbles) that I currently add as a subview to a UIImageView. It works as intended but I would like to animate the operation with a spring-like effect. I am using the below code but it does not work (the block gets executed but without animation):
/* ViewController */
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
bubbleView.hidden = NO;
[self.view layoutIfNeeded]; // tried this
}
completion:nil];
I have tried to animate both the hidden property as well as the alpha properties but same result. I have also tried animating the addSubview method, no difference. I started out with a more simple animation as proof of concept but that didn't work either.
The above code is executed within a method which is called during the (void)viewDidAppear:(BOOL)animated cycle. Does this have anything to do with this in that the animation is being executed during the main thread? I have read something to that effect but unsure. Also, I am using auto layout for the UIImageView but didn't think that would matter in this case since the animation is being applied to a custom subview of the UIImageView.
Any help appreciated.
Better use alpha.
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.alpha = 0;
// bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
// bubbleView.hidden = NO;
bubbleView.alpha = 1;
//[self.view layoutIfNeeded]; // tried this
}
completion:nil];
Try with this.
UIButton *catchButton = (UIButton *)sender;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut//UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat |
animations:^{
catchButton.alpha = 0.4;
catchButton.enabled = NO;
}
completion:NULL];

Animation inside 'if' statement does not work

This is my issue:
I have a few animation blocks and they are all getting executed fine. But I also want to perform some animations, only if a specific BOOL value is true.
The BOOL is properly taking the TRUE value and the code enters the if statement because my log comments are getting printed. Only the animation does not seem to appear. I have tried with different CGPoint values and still the same.
Perhaps there is something I am missing out about the animations.
I have not been using them a lot.
Any help would be great!
Here is a code sample:
// line 4 - second particle
UIImageView* particle8 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"particle"]];
particle8.frame = CGRectMake(p8.x, p8.y, particleWidth, particleWidth);
[self addSubview:particle8];
[UIView animateWithDuration:2
delay:0
options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionRepeat
animations:^{
particle8.frame = CGRectMake(p7.x, p7.y, particleWidth, particleWidth); }
completion:^(BOOL finished){[particle8 removeFromSuperview]; }];
if (_isSmartGridOn) {
// line 2b - first particle
UIImageView* particle9 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"particle"]];
particle9.frame = CGRectMake(ep.x - 5, ep.y - 5, particleWidth, particleWidth);
[self addSubview:particle9];
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionRepeat
animations:^{
particle9.frame = CGRectMake(p10.x, p10.y, particleWidth, particleWidth); }
completion:^(BOOL finished){[particle9 removeFromSuperview]; }];
}
You just have a typo or a copy/paste error:
particle8.frame = CGRectMake(p10.x, p10.y, particleWidth, particleWidth);
^ should be 9 ^^ ^^ looks like these might be wrong too

How can i change view after an animation?

I need your help.
In my app i press a button and an animation starts. After this animation i want to change automatically view without pressing any other button.
How can i do?
Here my code, i think i'm in the correct way, but the animation doesn't start, changes only view and xcode doesn't give me an error message.
- (IBAction)button {
[UIView animateWithDuration:2.5f animations:^{
NSArray *playAnimation;
playAnimation= [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#“image1.png"],
[UIImage imageNamed:#“image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
[UIImage imageNamed:#"image5.png"],
[UIImage imageNamed:#"image6.png"],
[UIImage imageNamed:#"image7.png"],
Nil];
}
completion:^(BOOL finished) {
UIStoryboard *Scene1 = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *Sc1 = [Scene1 instantiateViewControllerWithIdentifier:#"ViewController"];
[self presentViewController:Sc1 animated:NO completion:nil];
}];
}
Many thanks for your help
Most of the animate* methods have sibling-forms that take a completion block. Use the completion to do whatever you'd like.
e.g.
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;
-- versus --
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
Depends on how you started the animation. Read up on chaining UIView animations for a good overview of the subject.
If you're using UIView animation, set your animation delegate and an animationDidStopSelector, e.g.:
[UIView beginAnimations:#"Grow" context:self.btn];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(doneAnimating:finished:context:)];
// Do stuff you want animated
[UIView commitAnimations];
If you're using CABasicAnimation, same thing, just a different interface - set the delegate and animationDidStop selector, e.g.:
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:#"transform.scale.y"];
anim.delegate = self;
anim.animationDidStop = #selector(doneAnimating:finished:context:)];
anim.fromValue = [NSNumber numberWithDouble:0.01];
anim.toValue = [NSNumber numberWithDouble:1.0];
anim.duration = 0.5f;
[layer addAnimation:anim forKey:#"grow"];
In either case, -doneAnimating will get called when the animation has finished:
-(void)doneAnimating:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
// Do whatever you want next
}
You can do basic UIView animations. These include completion blocks in which you can push/present views.
[UIView animateWithDuration:1.5f animations:^{
// Perform animation here.
// ex.
[label setFrame:CGRectMake(0, 0, 320, 50)];
} completion:^(BOOL finished) {
// Show new view.
[self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
}];

how to add images into UIView

I Have 4 images. I want to add images in UIView one by one.
please give me any idea. How to add images array in UIView.
It is not clear from your question but i am guessing you wan to show images in your view one by one after some time delays... If it is what you want you van get it by
[self performSelector:#selector(callFunction) withObject:nil afterDelay:0 ];
[self performSelector:#selector(callFunction2) withObject:nil afterDelay:k_AnimateTime ];
[self performSelector:#selector(callFunction3) withObject:nil afterDelay:2*k_AnimateTime ];
in the Function
imageview = [[[UIImageView alloc] initWithFrame:CGRectMake(x,y,w,h)]autorelease];
imageview.image = [[UIImage imageNamed: [NSString stringWithString:name]]autorelease];
[self.view addSubview:imageview];
imageview.frame = CGRectMake(imageview.frame.origin.x, (imageview.frame.origin.y), imageview.frame.size.width, imageview.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
//[UIView setAnimationWillStartSelector:#selector(Transform2)];
[UIView setAnimationDuration:k_AnimateTime];
[UIView setAnimationBeginsFromCurrentState:YES];
//[UIView setAnimationDidStopSelector:#selector(increaseCount)];
imageview.frame = CGRectMake(imageview.frame.origin.x - Ex, (imageview.frame.origin.y - Ey), imageview.frame.size.width, imageview.frame.size.height);
[UIView commitAnimations];
[self.view addSubview:imageview];
}

UIView transitionFromView

I'm pretty new on iOs programming. And I'm stuck at a (i'm sure) very simple issue.
Don't know what i'm doing wrong...
-My viewDidLoad:
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 768, 1024);
UIView *uno=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainView.image = [UIImage imageNamed:#"photo.jpg"];
[uno addSubview:mainView];
UIView *dos=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainViewDos = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainViewDos.image = [UIImage imageNamed:#"Default.png"];
[dos addSubview:mainViewDos];
//
[self.view addSubview:uno];
//
[self anima:uno:dos];
And my anima method:
-(void) anima:(UIView *)uno:(UIView *)dos{
[UIView transitionFromView:uno
toView:dos
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
It changes the view but without transition...
Thanks
You can't perform an animation within your viewDidLoad--the view changes you make in there are all executed before the view is actually displayed, which is what you're seeing.
Are you trying to show this animation when the view is first displayed? If so, you can get it to work by putting the animation on a timer. Note that with this approach, you'll also have to refactor your anima method a bit to take a single argument.
In your viewDidLoad:
NSDictionary *views = [NSDictionary dictionaryWithObjectsAndKeys:uno, #"uno", dos, #"dos", nil];
[self performSelector:#selector(anima) withObject:views afterDelay:0.1];
Then change your anima method to:
-(void) anima:(NSDictionary *)views {
[UIView transitionFromView:[views objectForKey:#"uno"]
toView:[views objectForKey:#"dos"]
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}

Resources