【问题标题】:How to animate without an ease in iOS?如何在 iOS 中轻松制作动画?
【发布时间】:2024-01-20 00:17:01
【问题描述】:

我正在尝试为录像机上的红点设置动画。我希望点突然出现和消失,每 0.7 秒。我尝试的每个动画选项都有点以某种方式缓入或缓出。我怎样才能让它完全出现并完全消失?看起来 UIViewAnimationOptionTransitionNone 默认值与 UIView.h 中的 UIViewAnimationOptionCurveEaseInOut 相同。如何在没有任何缓动的情况下制作动画?

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,
};

现在是我的代码,

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

【问题讨论】:

    标签: ios objective-c animation uiview


    【解决方案1】:

    不要动画。如果您希望它立即出现和消失,请尝试以下操作:

    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;
    }
    

    【讨论】:

    • 另外,NB UIViewAnimationOptionCurveLinear 提供没有缓动功能的动画(在这种情况下不需要)。
    • @TylerPfaff 是的,它仍然有动画。缓动是指动画时序曲线。线性是一种平滑连续的动画。缓动(EaseIn、EaseOut 等)选项使用逐渐加速和/或减速的非线性曲线。缓动指的是加速度,而不是速度:)
    【解决方案2】:

    如果您使用的是 Swift 3,则可以使用 UIViewAnimationOptionCurveLinear(或简称为 .curveLinear)为 UIView 设置动画,轻松进出:

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

    如果你想完成:

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

    【讨论】:

    • 注意:这确实有效,如果它不适合你,你可能犯了同样的错误,并使用 UIViewAnimationCurveLinear 而不是 UIViewAnimationOptionCurveLinear
    【解决方案3】:

    在 iOS 7 及更高版本中,您可以使用关键帧动画。

    [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];
    

    通过为第二次状态更改指定0.5 的相对开始时间,这意味着它发生在整个1.4 秒持续时间的一半。通过将relativeDuration 指定为零,这意味着状态之间的转换是瞬时的。

    【讨论】:

      最近更新 更多