Tyler Pfaff
Tyler Pfaff

Reputation: 5032

How to animate without an ease in iOS?

I'm trying to animate a red dot on a video recorder. I want the dot to appear and disappear suddenly, every 0.7 seconds. Every animation option I try has the dot easing in or out in some way. How can I just make it appear completely and disappear completely? It looks like UIViewAnimationOptionTransitionNone defaults to the same value as UIViewAnimationOptionCurveEaseInOut from UIView.h. How can I animate without any easing?

enum {
   UIViewAnimationOptionLayoutSubviews            = 1 <<  0,
   UIViewAnimationOptionAllowUserInteraction      = 1 <<  1,
   UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2,
   UIViewAnimationOptionRepeat                    = 1 <<  3,
   UIViewAnimationOptionAutoreverse               = 1 <<  4,
   UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5,
   UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6,
   UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7,
   UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8,
   UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9,

   UIViewAnimationOptionCurveEaseInOut            = 0 << 16,
   UIViewAnimationOptionCurveEaseIn               = 1 << 16,
   UIViewAnimationOptionCurveEaseOut              = 2 << 16,
   UIViewAnimationOptionCurveLinear               = 3 << 16,

   UIViewAnimationOptionTransitionNone            = 0 << 20,
   UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
   UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
   UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
   UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
   UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,
   UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,
   UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,
};

Now my code,

[UIView animateWithDuration:0.7
                          delay:0
                        options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionTransitionNone)
                     animations:^(void){
                         self.recordingAnimationImage.alpha=0.0;
                                        }
                     completion:nil
                     ];

Upvotes: 0

Views: 1119

Answers (3)

Septronic
Septronic

Reputation: 1176

If you're using Swift 3, you can use UIViewAnimationOptionCurveLinear (or .curveLinear for short) to animate the UIView without ease in or out:

UIView.animate(withDuration: 2, delay: 0, options: [.curveLinear], animations: { 
            //Do what you want
        }, completion: nil)

if you want completion:

    UIView.animate(withDuration: 2, delay: 0, options: [.curveLinear], animations: { 
        //Do what you want
    }) { (success: Bool) in
        //Completion here
    }

Upvotes: 1

Rob
Rob

Reputation: 437552

In iOS 7 and later, you can use keyframe animation.

[UIView animateKeyframesWithDuration:1.4 delay:0.0 options:UIViewKeyframeAnimationOptionRepeat animations:^{
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{
        recordingAnimationImage.alpha = 1.0;
    }];
    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.0 animations:^{
        recordingAnimationImage.alpha = 0.0;
    }];
} completion:nil];

By specifying a relative start time of 0.5 for the second state change, that means it takes place half way through the total 1.4 second duration. By specifying a relativeDuration of zero, that means that the transition between states is instantaneous.

Upvotes: 0

Josue Espinosa
Josue Espinosa

Reputation: 5089

Don't animate. If you want it to appear and disappear instantly, try something like this:

viewcontroller.m

@property (strong, nonatomic) NSTimer *timer;

- (void)viewDidLoad {
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(alternate) userInfo:nil repeats:YES];
    }
}

- (void)alternate {
        self.myDotView.hidden = !self.myDotView.hidden;
}

- (void)viewWillDisappear {
    [self.timer invalidate];
    self.timer = nil;
}

Upvotes: 3

Related Questions