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
}];
Related
Im trying to fade in and out of a light node I have created I just dont know where to call the function accordingly so it will continously fade in and out. Never tried animating anything here is what I have.
- (void)fadeOutIn:(UIView *)view duration:(NSTimeInterval)duration
{
[self enumerateChildNodesWithName:#"//*" usingBlock:^(SKNode *node, BOOL *stop) {
if ([node.name isEqualToString:#"light1"]) {
[UIView animateKeyframesWithDuration:5 delay:2 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationCurveEaseInOut animations:^{
node.alpha = 0;
} completion:^(BOOL finished) {
}];
}
}];
}
where should I call this function?that is fades in and out continously?
Should it be called within an action?
Thank You
Use this for Continue fade in out.
-(void)viewDidAppear:(BOOL)animated{
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction animations:^{
node.alpha = 0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 animations:^{
node.alpha = 1;
} completion:nil];
}];
}
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[objAnimate setAlpha:1.0];
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
[objAnimate setAlpha:0.0];
} completion:nil];
}
This will create continuous fade in and out for your object when it appears. If you want to create programmatically a custom object and then use this code then do it after your custom object is added in your view as below (example):
objAnimate = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 200, 40)];
objAnimate.backgroundColor = [UIColor lightGrayColor];
[objAnimate setAlpha:1.0];
[self.view addSubview:objAnimate];
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
[objAnimate setAlpha:0.0];
} completion:nil];
The view starts animating as soon as its added in your view.
I have a UIView which is initially hidden. I need to setHidden:NO (visible) with dropdown effect...
There's my simple code without effect
-(IBAction)btnAbrirDestaquesClick:(id)sender {
[self.viewDestaques setHidden:NO];
}
A simpler alternative to UIDynamicAnimator in iOS 7 is Spring Animation (a new and powerful UIView block animation), which can give you nice bouncing effect with damping and velocity:
[UIView animateWithDuration:duration
delay:delay
usingSpringWithDamping:damping
initialSpringVelocity:velocity
options:options animations:^{
//Animations
[self.viewDestaques setHidden:NO];
}
completion:^(BOOL finished) {
//Completion Block
}];
If you simply want to animate it try something like this:
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.viewDestaques.frame = CGRectMake(0, 0, 320,30);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.viewDestaques.frame = CGRectMake(0, -30, 320,30);
} completion:^(BOOL finished) {
}];
}];
It worked for me:
-(IBAction)btnAbrirDestaquesClick:(id)sender {
[self.viewDestaques setTranslatesAutoresizingMaskIntoConstraints:YES]; //respeita o frame que eu setar, independentemente das constraints
[self.viewDestaques setFrame:CGRectMake(self.viewDestaques.frame.origin.x, self.viewDestaques.frame.origin.y, self.viewDestaques.frame.size.width, 0)];
[self.viewDestaques setHidden:NO];
while (self.viewDestaques.frame.size.height < self.frameViewDestaquesOriginal.size.height) {
[UIView animateWithDuration:2.0 animations:^{
[self.viewDestaques setFrame:CGRectMake(self.viewDestaques.frame.origin.x, self.viewDestaques.frame.origin.y, self.viewDestaques.frame.size.width, self.view.frame.size.height + 10)];
}completion: ^(BOOL completed){
}];
}
}
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.
in my main view controller the button action is
-(IBAction)ChangeImage
{
UIImage *guns = [UIImage imageNamed:#"guns.png"]; // load our image resource
imageView = [[UIImageView alloc] initWithImage:guns];
NSLog(#"kaam hua..??");
[self.view addSubview:imageView];
[UIView transitionFromView:self.view
toView:imageView
duration:3.0f
options:UIViewAnimationOptionTransitionCrossDissolve
completion:nil];
NSLog(#"hfhjlkhbjhdfcvkbjnlkhgj");
}
i want to view the animation in slide view
is there any method to do this
can i chnage the UIViewAnimationOptionTransitionCrossDissolve to another one to show slide view
You can use UIViewAnimationOptionCurveEaseIn in animation option,
Try this,
CGRect or=self.view.frame;
CGRect basketTopFrame = self.view.frame;
basketTopFrame.origin.x = basketTopFrame.size.width;
self.view.frame = basketTopFrame;
[UIView animateWithDuration:0.2
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
[self.view addSubview:imageView];
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
I have buttons on some viewController view. touchUpInside: methods present modal view controllers. I want to present them from center of tapped button frame. How can I achieve this result?
I tried:
webController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, self.buuton.center.x, self.button.center.y);
[self presentViewController:webController animated:NO completion:^{
// some code
}];
but it damaged my UI. I also tried to use [UIView beginAnimation], no success.
I have achieved something similar with a trick, adding destvc.view as subview to sourcevc.view, see below my functions to scale in and scale out.
You can use the same trick only as visual effect, and present your vc at the end, without animation:
+(void) animWithAlpha:(float)alpha view:(UIView*)view{
[UIView animateWithDuration:0.15
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
view.alpha = alpha;
}
completion:^(BOOL fin) {}];
}
+(void) zoomIN:(UIViewController*)sourceViewController destination:(UIViewController*)destinationViewController fromPoint:(CGPoint)point{
[sourceViewController.view addSubview:destinationViewController.view];
[destinationViewController.view setFrame:sourceViewController.view.window.frame];
[destinationViewController.view setTransform:CGAffineTransformMakeScale(0.15, 0.1)];
destinationViewController.view.center = point;
sourceViewController.view.userInteractionEnabled=NO;
destinationViewController.view.alpha = 0.5;
[self animWithAlpha:1 view:destinationViewController.view];
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[destinationViewController.view setTransform:CGAffineTransformMakeScale(1, 1)];
destinationViewController.view.center = sourceViewController.view.center;
}
completion:^(BOOL finished){
if(finished){
[destinationViewController.view setTransform:CGAffineTransformMakeScale(1, 1)];
destinationViewController.view.center = sourceViewController.view.center;
sourceViewController.view.userInteractionEnabled=YES;
}
}];
}
+(void) zoomOUT:(UIViewController*)sourceViewController destination:(UIView*)destination toPoint:(CGPoint)point{
[sourceViewController.view setTransform:CGAffineTransformMakeScale(1.0, 1.0)];
[sourceViewController.parentViewController.view setUserInteractionEnabled:NO];
[destination setUserInteractionEnabled:NO];
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[sourceViewController.view setTransform:CGAffineTransformMakeScale(0.01, 0.01)];
[sourceViewController.view setAlpha:1.0];
sourceViewController.view.center = point;
}
completion:^(BOOL finished){
if(finished){
[sourceViewController.view setTransform:CGAffineTransformMakeScale(1.0, 1.0)];
[sourceViewController.view removeFromSuperview];
sourceViewController.view.center = point;
[destination setUserInteractionEnabled:YES];
}
}
You have to do that through addSubview: method and doing something like:
- (void) appear: (UIView *) view fromPoint: (CGPoint) point
{
[view setBounds:self.view.bounds];
[view setCenter:point];
[view setAlpha:0.0];
[self addSubview:view];
[view setTransform:CGAffineTransformMakeScale(0, 0)];
[UIView animateWithDuration:0.3 animations:^{
[view setCenter:self.view.center];
[view setTransform:CGAffineTransformIdentity];
[view setAlpha:1.0];
}];
}
You have to create the view before call the method.
Good Luck!