Repeat Fade In / Fade Out effects to switch between three views - iphone

I've created one view containing three subviews.
I would like to fade in / out the three subviews and repeat the effect indefinitely.
I try something like :
+ (void)fadeIn:(UIView *)view withDuration:(float)duration
{
[UIView animateWithDuration:duration animations:^{
view.alpha = 1.0;
}];
}
+ (void)fadeOut:(UIView *)view withDuration:(float)duration
{
[UIView animateWithDuration:duration animations:^{
view.alpha = 0.0;
}];
}
[UIView animateWithDuration:5.0
delay:2.0
options:UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^
{
[Utilities fadeOut:headSubtitle_1 withDuration:1];
[Utilities fadeIn:headSubtitle_2 withDuration:1];
[Utilities fadeOut:headSubtitle_2 withDuration:1];
[Utilities fadeIn:headSubtitle_3 withDuration:1];
[Utilities fadeOut:headSubtitle_3 withDuration:1];
[Utilities fadeIn:headSubtitle_1 withDuration:1];
}
completion:^(BOOL finished)
{}];
But it doesn't work well.
The effect that I would obtain is :
Display headSubtitle_1 for 2 seconds
FadeOut headSubtitle_1 in 1 second
FadeIn headSubstitle_2 in 1 second
Display headSubstitle_2 for 2 seconds
FadeOut headSubtitle_2 in 1 second
FadeIn headSubstitle_3 in 1 second
Display headSubstitle_3 for 2 seconds
FadeOut headSubtitle_3 in 1 second
FadeIn headSubtitle_1 in 1 second
Repeat to 1.
Thanks a lot !
EDIT
Finally, I try something like this :
+ (void)runAnimationSubtitleOrdersWithSub1:(UITextView *)headSubtitle_1 andSub2:(UITextView *)headSubtitle_2 andSub3:(UITextView *)headSubtitle_3
{
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
headSubtitle_1.alpha = 0;
} completion:^(BOOL finished) {
if (finished)
{
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
headSubtitle_2.alpha = 1;
} completion:^(BOOL finished) {
if (finished)
{
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
headSubtitle_2.alpha = 0;
} completion:^(BOOL finished) {
if (finished)
{
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
headSubtitle_3.alpha = 1;
} completion:^(BOOL finished) {
if (finished)
{
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
headSubtitle_3.alpha = 0;
} completion:^(BOOL finished) {
if (finished)
{
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
headSubtitle_1.alpha = 1;
} completion:^(BOOL finished) {
[FormBuilder runAnimationSubtitleOrdersWithSub1:headSubtitle_1 andSub2:headSubtitle_2 andSub3:headSubtitle_3];
}];
}
}];
}
}];
}
}];
}
}];
}
}];
}
It's really ugly ! If some body knows a better way to do this.

Maybe you can play with several animation with something like this (but, it's not beautiful) :
_imageView.alpha = 0.0f;
_imageView2.alpha = 0.0f;
_imageView3.alpha = 0.0f;
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
_imageView.alpha = 1.0f;
[UIView animateWithDuration:1
delay:1
options:UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
_imageView2.alpha = 1.0f;
} completion:^(BOOL finished) {
_imageView2.alpha = 0.0f;
}
];
} completion:^(BOOL finished) {
_imageView.alpha = 0.0f;
}
];

Take a look at [UIView beginAnimations:(NSString *) context:(void *)].

Related

How to add multiply animation to the view in objective c?

I want to add multiple animations to the view. I just can add one,... I do not want to concatenate
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kAnimationDuration];
//self.square.bounds = CGRectMake(0, 0, self.square.bounds.size.height, 200);
//self.transform = CGAffineTransformMakeScale(2, 1);
CGAffineTransform scaleTrans1 = CGAffineTransformMakeScale(2, 1);
self.transform = scaleTrans1;
[UIView commitAnimations];
You can use animateWithDuration (in any of its variations) from UIView, as long as the properties you are trying to animate are actually animatable (view UIView / Animations).
For example:
[UIView animateWithDuration:1.0f
animations: ^ {
self.square.bounds = CGRectMake(0, 0, self.square.bounds.size.height, 200);
self.transform = CGAffineTransformMakeScale(2, 1);
}];
Hope it helps!
I did like this and its working
- (void)drawRect:(CGRect)rect
{
[UIView animateWithDuration:4.0f animations:^{
self.transform = CGAffineTransformMakeScale(2, 1);
} completion:^(BOOL finished){
[UIView animateWithDuration:4.0f animations:^{
self.transform = CGAffineTransformMakeScale(0.75, 1);
} completion:^(BOOL finished){
[UIView animateWithDuration:4.0f animations:^{
self.transform = CGAffineTransformMakeScale(2, 1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:4.0f animations:^{
self.transform = CGAffineTransformMakeScale(0.75, 1);
}];
}];
}];
}];
}

Make uibutton pulsate.

I am trying to make a uibutton that has an image in it pulsate slowly by changing the alpha level back and forth once the view loads...
currently I am doing this but it does nothing....
-(void)loopdyloop
{
while ( myCount < 1000 )
{
[UIView animateWithDuration:3.0
delay:0.0f
options:UIViewAnimationCurveEaseOut
animations:^{
iconButton.alpha = 0.0;
} completion:^(BOOL finished) {
if (finished) {
NSLog(#"animated");
}
}];
[UIView animateWithDuration:3.0
delay:0.0f
options:UIViewAnimationCurveEaseOut
animations:^{
iconButton.alpha = 1.0;
} completion:^(BOOL finished) {
if (finished) {
NSLog(#"animated");
}
}];
myCount++;
}
}
this method is called from the viewdidload method,
pretty crude I know but its my best attempt, if you have any idea on how I could achieve this it would be greatly appreciated.
How about applying this in your button layer in viewDidLoad:
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:#"transform.scale"];
pulseAnimation.duration = .5;
pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.autoreverses = YES;
pulseAnimation.repeatCount = FLT_MAX;
[YOURButton.layer addAnimation:pulseAnimation forKey:nil];
and you can experiment with it.
If you want it to repeat forever you could add the animation options to auto reverse and repeat, like this:
[UIView animateWithDuration:3.0
delay:0.0f
options:UIViewAnimationCurveEaseOut |
UIViewAnimationOptionRepeat |
UIViewAnimationOptionAutoreverse
animations:^{
iconButton.alpha = 0.0;
}
completion:^(BOOL finished) {
// You could do something here
}];
It will cause the alpha to first animate to 0.0, then back to the original (1.0) and then do those two things over and over and over...

delay in getting complete boolean callback of UIView block animation (animateWithDuration)

i am doing block animation, my project is using opengl for gameplay and uiview for menu like stuff.
i am animating uiview using animateWithDuration method, bt facing issue at getting delay complete animation boolean callback. I tried the same code in UIKit app and it works very smoothly.
Below is my code:
-(void) playAnimation:(UIButton *)_button atPosition:(CGRect)_rect withDuration:(CGFloat)_duration andDelay:(CGFloat)_delay andDiection:(NSInteger)_direction
{
[UIView animateWithDuration:_duration
delay:_delay
options:UIViewAnimationCurveLinear
animations:^{
_button.frame = _rect;
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^{
CGRect rect = _rect;
rect.origin.x = _rect.origin.x + (10 * _direction);
_button.frame = rect;
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^{
_button.frame = _rect;
}
completion:^(BOOL finished){
}];
}];
}];
}

How to scale UIImageView properly with UIView animateWithDuration?

I'm scaling UIImageView and then reverting it using UIView animateWithDuration with UIViewAnimationOptionAutoreverse option. The problem is the image animation is a little bit jaggy (twitching) at the end of the animation. Here's my code:
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionAutoreverse
animations:^{
myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);}
completion:^(BOOL finished){if (finished){
myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);}}];
I know that I'm missing something but don't know where to start :(
UPDATE1: I'm performing nested 6 animations where each next animation performs after previous one. For that I'm using block animations and performing each next animation in complete block.
UPDATE2: I have tried with UIViewAnimationOptionRepeat option but there's still some flash effect after each scale animation.
UIViewAnimationOptionAutoreverse does nothing without also using the UIViewAnimationOptionRepeat option. Your animation as written will shrink the image to 90%, then snap back to 100% in the completion block. Maybe you can follow this animation with another that scales back to 100% over a quarter second. Something like this:
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^{
myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);}
completion:^(BOOL finished){if (finished){
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^{
myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);}
completion:NULL];}}];
.h file declerd.
NSInteger imgint;
.m file in code and try.
-(void)
{
[NSTimer scheduledTimerWithTimeInterval:(0.85f)target:self selector:#selector(play_btn_animation) userInfo:nil repeats:YES];
}
-(void)play_btn_animation
{
if(imgint == 0)
{
[UIView animateWithDuration:0.8
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
img.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);
}
completion:^(BOOL finished) {
img = 1;
}];
}
else
{
[UIView animateWithDuration:0.8
delay:0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
img.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
}
completion:^(BOOL finished) {
imgint = 0;
}];
}
}

Nested animation block caused "Segmentation fault"

I try to show three images, one after one, with the following code:
image_1.alpha = 0.0;
image_2.alpha = 0.0;
image_3.alpha = 0.0;
[UIView animateWithDuration:0.25
animations:^{
image_1.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.25
animations:^{
image_2.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.25
animations:^{
image_3.alpha = 1.0;
} completion:^(BOOL finished) {
;
}];
}];
}];
gives: internal compiler error: Segmentation fault
There is no error if the nesting contains two animation blocks. Is nested animation block no allowed or comes with some limitation?