animation blocks infinite loop - iphone - iphone

I'm having difficulty working with the animation blocks. I'm trying to have the first animation change the alpha of an image so that it flashes, then after 10 seconds disappears. the firstAnimation then moves to secondAnimation which does the same thing only to a different image. the process then repeats itself infinitely.
I've been working w/ this for a while now and cannot make it repeat infinitely. Another problem I had was having this animation process delay - so that it begins after 20 seconds, enough time for an audio file to play.
- (void) firstAnimation {
UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear;
[UIView animateWithDuration:1.0 delay:10.0 options:options animations:^
{
myImage.alpha = 0.0;
myImage.alpha = 1.0;
}
completion:^(BOOL finished){
[self secondAnimation];
}
];
}
thanks as always, for any help. I've searched for example code and tutorials but all I found is basically what I'm doing in the code above.

I think you have to set the initial value of your alpha outside the animation block and only the value you want it to animate to inside the block.
UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear;
myImage.alpha = 0.0;
[UIView animateWithDuration:1.0 delay:10.0 options:options animations:^
{
myImage.alpha = 1.0;
}

animateImageWithCount:(int)aCount{
if( aCount >= [imageArray count]){
NSLog(#"Done all images");
return;
}
UIImage *myImage = [ImageArray objectAtIndex:aCount];
UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear;
myImage.alpha = 0.0;
[UIView animateWithDuration:1.0 delay:10.0 options:options animations:^{
myImage.alpha = 1.0;
}
completion:^(BOOL finished){
[self animateImageWithCount:(aCount+1)];
}];
}

Related

How to stop and restart animation in UIView block animation

I have checked and tried variety of answers . But still it didn't work for me.
Here is my code,
-(void)animate {
[img1 startAnimating];
[UIView animateWithDuration:10
animations:^{
img1.transform = CGAffineTransformMakeTranslation(0.0, -1000);}];
[img2 startAnimating];
[UIView animateWithDuration:13
animations:^{
img2.transform = CGAffineTransformMakeTranslation(0.0, -1000);}];
}
I want to get animation started when i press one button and stop when i press another. It works fine for start and stop . but problem comes when i restart it by pressing on start button which calls this method again. Nothing happens when i call second time ??????
Transformation unable to perform twice
Hi you need to use the completion block to perform this animation,
Initialize like this some where else....
#interface YourClass()
{
CGAffineTransform trans1 ;
CGAffineTransform trans2 ;
}
#End
....
-(void)animate {
[_img1 startAnimating];
[UIView animateWithDuration:10
animations:^{
trans1 = _img1.transform; //
_img1.transform = CGAffineTransformMakeTranslation(0.0, -1000);} completion:^(BOOL finished) {
_img1.transform = trans1;
}];
[_img2 startAnimating];
[UIView animateWithDuration:13
animations:^{
trans2 = _img2.transform;
_img2.transform = CGAffineTransformMakeTranslation(0.0, -1000);} completion:^(BOOL finished) {
_img2.transform = trans2;
}];
}

Custom UIView doesn't get removed from superview when loaded again while animating

The question explains it all actually.
I load a custom UIView on top of my current view, animated, using this code:
- (void)showView
{
self.blurView.alpha = 0.f;
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
self.blurView.alpha = 1.f;
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
self.blurView.alpha = 0.f;
} completion:nil];
}];
}
It works, but when I perform -(void)showView again while it is still animating, the custom view doesn't get removed from the screen.
It just stays like this:
I figured out what the problem was.
blurView is a public variable and every time I did -(void)showView it used the same variable, setting the alpha to 0.f just once, thus never changing the alpha of the first show.
I declared the variable blurView in the method itself and now it works because every blurView gets it's own pointer.
-(void)someMethod
{
//Create a live blur view
FXBlurView *blurView = [[FXBlurView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
blurView.center = CGPointMake(self.view.window.frame.size.width / 2, self.view.window.frame.size.height / 2);
blurView.layer.cornerRadius = 20.f;
[self.view.window addSubview:blurView];
[self showView:(blurView)];
}
- (void)showView:(FXBlurView *)blurView
{
//Make full transparent before showing
blurView.alpha = 0.f;
//Start animation
[FXBlurView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
blurView.alpha = 1.f;
} completion:^(BOOL finished) {
if (finished)
{
//End animation
[FXBlurView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
blurView.alpha = 0.f;
} completion:nil];
}
}];
}
You should add to the top of your function this line:
[self.view.layer removeAllAnimations];
It will remove all of the active animations.
It doesn't work because you have implemented a delay, use animation block with delay - it will create animation keys that could be removed with this function above.
[UIView animateWithDuration:0.3 delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^
{
// ...
} completion:NULL];

animateWithDuration not working correctly

I have written some code that I had hoped would randomly spin a block into view on the screen, pause, then spin it out. The action should repeat itself in a semi random fashion. This kinda of works in that it shows the block, spins out, then shows itself again after some time however:
It does not spin in (it only spins out)
It never changes position
What vital thing am I missing from this code?
-(void)randomlyShowBlock{
int randomTime = 1 + arc4random() % (3 - 1);
[RandomBlock setNeedsDisplay];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, randomTime * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
int randomX = 10 + arc4random() % ((int)self.view.frame.size.width - 10);
int randomY = 10 + arc4random() % ((int)self.view.frame.size.height - 10);
RandomBlock.frame = CGRectMake(randomX, randomY, RandomBlock.frame.size.width, RandomBlock.frame.size.height);
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
RandomBlock.transform = CGAffineTransformMakeRotation(DegreesToRadians(-90));
RandomBlock.backgroundColor = [[UIColor alloc] initWithRed:50.0/255.0 green:124.0/255.0 blue:203.0/255.0 alpha:1.0];
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.4 delay:1 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
RandomBlock.transform = CGAffineTransformMakeRotation(DegreesToRadians(0));
RandomBlock.backgroundColor = [[UIColor alloc] initWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:0];
}
completion:^(BOOL finished){
NSLog(#"finished animation");
//model
[self randomlyShowBlock];
}];
}];
});
}
You're not changing the frame in the animation, so it's not surprising that it's not moving.
As for the spinning, my guess is that the transform is not initially an identity transform, but rather a nil one, and iOS doesn't know how to animate from nothing to something. Try setting it, before the animation block, to CGAffineTransformIdentity.

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...

alpha value of uitabbaritem

I am trying to figure out how to set my tabbaritem icon(image) to 0 so I can give it a fade animation once it loads onto the screen... but I just don't know how to do it..
this is how I am adding the images to the tabbar..
- (void)viewDidAppear:(BOOL)animated
{
self.button0.image = [UIImage imageNamed:#"icon1.png"];
self.button1.image = [UIImage imageNamed:#"icon2.png"];
self.button2.image = [UIImage imageNamed:#"icon3.png"];
self.button3.image = [UIImage imageNamed:#"icon4.png"];
self.button4.image = [UIImage imageNamed:#"icon5.png"];
self.button5.image = [UIImage imageNamed:#"icon1.png"];
}
I will animate them with a method that looks something like this
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
// Do your animations here
}
completion:^(BOOL finished){
if (finished) {
// Do your method here after your animation.
}
}];
any help would be greatly appreciated.
Update this is my latest attempt thats not working yet..
buttonImage0.image = [UIImage imageNamed:#"icon1.png"];
buttonImage0.alpha = 0.0;
self.button0.image = buttonImage0.image;
[UIView animateWithDuration:0.1f
delay:0.0f
options:UIViewAnimationOptionTransitionNone
animations:^{
// Do your animations here.
buttonImage0.alpha = 1.0;
}
completion:^(BOOL finished){
if (finished) {
// Do your method here after your animation.
}
}];
UIButton is a subclass of UIView. UIView has an alpha property.
Try:
self.button0.alpha = 0.0;
self.button1.alpha = 0.0;
self.button2.alpha = 0.0;
self.button3.alpha = 0.0;
self.button4.alpha = 0.0;
self.button5.alpha = 0.0;
in your viewDidAppear;
Then put your easing function in the animateWithDuration routine using the same alpha property!