Using Block Completion Handler in iOS 4 for animation - objective-c

I would like to animate my subviews movement when rotating the device, changing the alpha to 0, move the view to the new position and reset the alpha to 1.
Using this code in didRotateFromInterfaceOrientation causes the view to flash and fade very quickly and then reappear. I would like to avoid this behaviour.
[UIView animateWithDuration:kAnimationDuration animations:^{
anObject.alpha = 0.0;
CGRect newFrame = anObject.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
anObject.frame = newFrame;
} completion:^ (BOOL finished){
if (finished) {
anObject.alpha = 1.0;
}
}];
Is there a way around this flashing?
Thanks

Maybe actually animate alpha on completition ? rather than flash it ? :)
[UIView animateWithDuration:kAnimationDuration animations:^{
anObject.alpha = 0.0;
CGRect newFrame = anObject.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
anObject.frame = newFrame;
} completion:^ (BOOL finished){
if (finished) {
[UIView animateWithDuration:kAnimationDuration
animations:^{
anObject.alpha = 1;}
}
}];
Cheers,
Krzysztof Zabłocki

Related

Keep the End Position of Animation

I tried everything, but everytime its the same problem:
i want to move a button for y = 276. when the animation ends, my buttons jumps back to the startposition, but i want that the endanimationposition is the new position, and the button has to stay there, until a new animation is called.
tried with
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
CGPoint p = _cell00.center;
p.x += 100;
_cell00.center = p;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
same issue like
[UIView animateWithDuration:1.0 animations:^{
_cell00.frame = CGRectOffset(_cell00.frame, 0, 20);
}];
or
[UIView animateWithDuration:0.5 animations:^{
_cell00.center = CGPointMake(0, 20.0);
}];
EDIT:
I call this Animation with my Button called statisticsBUttonPressed.
- (IBAction)statisticsButtonPressed:(id)sender {
if (statOrGame == 0) {
statOrGame = 1;
}else {
statOrGame = 0;
}
NSLog(#"statOrGame? %d", statOrGame);
[self animateView:(UIButton *)sender];
}
-(void) animateView:(UIButton *)sender {
sender.userInteractionEnabled = NO;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{ // The iOS device = iPhone or iPod Touch
NSLog(#"IS IPHONE 5");
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if (iOSDeviceScreenSize.height == 568)
{
if (statOrGame) {
[UIView animateWithDuration:1.0 animations:^{
_cell00.frame = CGRectOffset(_cell00.frame, 0, 20);
}];
}
}
}
}
The problem is that you're using auto layout. You can't (just) change the frame of a view that is positioned by auto layout, because what positions the view is its constraints. You need to change the constraints. You can do this at the end of the animation, or you can simply animate the change of constraints.

UILabel animating frame change disregarding delay

I have a UILabel that I show / hide. I want to show the label, pause for a few seconds and hide it. This code ISN'T working. It removes the view as soon as the animation finished (instantly)
-(void) show
{
[UIView animateWithDuration:.5f delay:0.0f
options:UIViewAnimationOptionCurveEaseIn animations:^{
CGRect newFrame = CGRectMake(0.0f, 64.0f, [UIScreen mainScreen].bounds.size.width, 44.0f);
self.frame = newFrame;
} completion:^(BOOL finished){
[self hideAndRemove];
}];
}
-(void) hideAndRemove
{
[UIView animateWithDuration:.5f delay:2.0f
options:UIViewAnimationOptionCurveEaseIn animations:^{
CGRect frame = CGRectMake(0.0f, 64.0f, [UIScreen mainScreen].bounds.size.width, 0.0f);
self.frame = frame;
} completion:^(BOOL finished){
// nothing
}];
}
However, if I try some other animation on the frame, the delay works and the frame change is animated:
-(void) show
{
[UIView animateWithDuration:.5f delay:0.0f
options:UIViewAnimationOptionCurveEaseIn animations:^{
CGRect newFrame = CGRectMake(0.0f, 64.0f, [UIScreen mainScreen].bounds.size.width, 44.0f);
self.frame = newFrame;
} completion:^(BOOL finished){
[self hideAndRemove];
}];
}
-(void) hideAndRemove
{
[UIView animateWithDuration:.5f delay:2.0f
options:UIViewAnimationOptionCurveEaseIn animations:^{
CGRect frame = CGRectMake(0.0f, 64.0f, [UIScreen mainScreen].bounds.size.width, 250.0f);
self.frame = frame;
} completion:^(BOOL finished){
// nothing
}];
}
I don't know what the problem might be, but I think it comes from the frame height who shouldn't be set to 0.
Why not using [UILabel setAlpha:0] to hide your label, and [UILabel setAlpha:1] to show it back ? It seems cleaner than changing its frame height to 0, and it animates nicely.

Some problems with View animation

I'm trying create animation on my View. Now I have any trouble. Animation is work only with _pageViewController
_pageViewController.view.frame = frame;
_pageViewController.view.alpha = alpha;
but _bottomImage.frame = frame1; doesn't work. I don't understand Why?
If I comment this lines
//_pageViewController.view.frame = frame;
//_pageViewController.view.alpha = alpha;
_bottomImage.frame = frame1 begin work.
What am I doing wrong?
- (IBAction)share:(id)sender {
CGRect frame =self.pageViewController.view.frame;
CGRect frame1 = _bottomImage.frame;
float alpha = 1.0;
if (frame.origin.y==65.0) {
frame.origin.y = 240;
frame1.origin.y = 650;
alpha = 0.5;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.5];
[UIView setAnimationDelay:0.1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
_pageViewController.view.frame = frame;
_pageViewController.view.alpha = alpha;
_bottomImage.frame = frame1;
[UIView commitAnimations];
}else{
[self closeShare];
}
}

How can I fade a UIView in while moving it up?

I have a UIView and I want to give it an effect that causes it to start with an alpha of 0 and then while moving up 50 pixels animated it also changes to an alpha of 1. How can this be done in Objective-C?
[UIView animateWithDuration:1.0 animations:^{
//here change position and alpha
CGRect newFrame = view.frame;
newFrame.origin.y += 50;
view.frame = newFrame;
view.alpha = 1.0;
} completion:^(BOOL finished) {
//called when animation did finish. do what you want
}];
https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/animatingviews/animatingviews.html
Everything about animations.
It's all right there.
//something similar to what you want:
- (IBAction)showHideView:(id)sender
{
// Fade out the view right away
[UIView animateWithDuration:1.0
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
thirdView.alpha = 0.0;
}
completion:^(BOOL finished){
// Wait one second and then fade in the view
[UIView animateWithDuration:1.0
delay: 1.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
thirdView.alpha = 1.0;
}
completion:nil];
}];
}
Something like this would do the trick:
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(self.view.center.x, self.view.center.y, 100, 50)];
box.backgroundColor = [UIColor blackColor];
box.alpha = 0.0f;
[self.view addSubview:box];
[UIView animateWithDuration:2.0f
animations:^{
box.alpha = 1.0f;
CGRect frame = CGRectMake(self.view.center.x, self.view.center.y - 100, 100, 50);
[box setFrame:frame];
}];

How to fade in and fade out a label. continuously

In my Xcode project I have put a label on an xib.
I want to fade the label in and out continuously until the user taps the screen. When this happens I want a new view to appear.
Can anyone suggest how to do the fade-in/out?
Instead of nesting blocks and manually restarting your animations, you can use the option pair UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat to tell Core Animation you want the label to fade in and out continuously.
- (void)startAnimatingLabel
{
self.label.alpha = 0;
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
self.label.alpha = 1;
} completion:nil];
}
To stop the animations from running, just remove them from the label's layer.
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
[self.label.layer removeAllAnimations];
self.label.alpha = 0;
[self presentNewView];
}
EDIT: A less abrupt way to finish would be to animate from the current view state to the final one (this will interrupt the current, repeating animation).
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.label.alpha = 0;
} completion:^(BOOL finished){
[self presentNewView];
}];
}
You could put a pair of chained animation in a loop or call a function that holds the chained animation everytime until you encounter a user tap.
By Chained animation, I mean something like this (You can set the animation duration to suit your needs):
myLabel.alpha = 0.0;
[UIView animateWithDuration:1.0
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
myLabel.alpha = 1.0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
myLabel.alpha = 0.0;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
}];
The above code will first fade in your label and then fade it out. You can put that in a function and call it until you encounter user tap.
Create a CABasicAnimation and add it to your label:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"opacity"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = #(1.0f);
animation.toValue = #(0.1f);
animation.repeatCount = INFINITY;
animation.duration = 0.75;
animation.autoreverses = YES;
[label.layer addAnimation:animation];
When you click on your button, just get a pointer to that label and remove all the animations:
[label.layer removeAllAnimations];
Try this it give you the ability to manage the animation repeat count if you replaced INFINITY with your repeat count
fadingLabel.alpha = 1.0;
[UIView beginAnimations:#"fadingLabel" context:nil];
[UIView setAnimationDuration:4.0];
[UIView setAnimationRepeatCount:INFINITY];
fadingLabel.alpha = 0.0;
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView commitAnimations];
Wayne Hartman's Answer in Swift 4
let animation = CABasicAnimation(keyPath: "opacity")
animation.fromValue = 1
animation.toValue = 0.1
animation.duration = 0.75
animation.repeatCount = .infinity
animation.autoreverses = true
label.layer.add(animation, forKey: nil)
and
label.layer.removeAllAnimations()