美文网首页
iOS UIViewKeyframeAnimationOptio

iOS UIViewKeyframeAnimationOptio

作者: MQ_Twist | 来源:发表于2020-10-27 10:36 被阅读0次

    前车之鉴,后车之师。

    前言

    动画在App中还是比较常见,iOS上的动画流畅度以及效果还是很哇塞的。所以,动画是iOSer必备的技术之一,不管什么样的动画咱们都得会写。实在不会,也不慌,有Lottie、SVGA。不多哔哔,切入正题。

    正文

    简单的动画咱们可以使用UIView封装好的方法实现,如:

    + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;
    

    如果在动画过程中添加交互,简单,用:

    + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
    

    设置optionsUIViewAnimationOptionAllowUserInteraction就可以了。

    动画比较复杂时,这时候就可以用到关键帧动画,系统封装的也有配套API:

    + (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion API_AVAILABLE(ios(7.0));
    + (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations API_AVAILABLE(ios(7.0)); // start time and duration are values between 0.0 and 1.0 specifying time and duration relative to the overall time of the keyframe animation
    

    动画过程中如果需要交互,设置optionsUIViewKeyframeAnimationOptionAllowUserInteraction
    嗯!完美!一切就是那么简单!一运行,你会惊奇的发现,UIViewKeyframeAnimationOptionAllowUserInteraction并没有用。
    查看枚举

    typedef NS_OPTIONS(NSUInteger, UIViewKeyframeAnimationOptions) {
        UIViewKeyframeAnimationOptionLayoutSubviews            = UIViewAnimationOptionLayoutSubviews,
        UIViewKeyframeAnimationOptionAllowUserInteraction      = UIViewAnimationOptionAllowUserInteraction, // turn on user interaction while animating
        UIViewKeyframeAnimationOptionBeginFromCurrentState     = UIViewAnimationOptionBeginFromCurrentState, // start all views from current value, not initial value
        UIViewKeyframeAnimationOptionRepeat                    = UIViewAnimationOptionRepeat, // repeat animation indefinitely
        UIViewKeyframeAnimationOptionAutoreverse               = UIViewAnimationOptionAutoreverse, // if repeat, run animation back and forth
        UIViewKeyframeAnimationOptionOverrideInheritedDuration = UIViewAnimationOptionOverrideInheritedDuration, // ignore nested duration
        UIViewKeyframeAnimationOptionOverrideInheritedOptions  = UIViewAnimationOptionOverrideInheritedOptions, // do not inherit any options or animation type
        
        UIViewKeyframeAnimationOptionCalculationModeLinear     = 0 << 10, // default
        UIViewKeyframeAnimationOptionCalculationModeDiscrete   = 1 << 10,
        UIViewKeyframeAnimationOptionCalculationModePaced      = 2 << 10,
        UIViewKeyframeAnimationOptionCalculationModeCubic      = 3 << 10,
        UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10
    } API_AVAILABLE(ios(7.0));
    

    没有问题!这里百撕不得姐,希望知道的大佬留言,万分感谢!
    如果就是想用UIView的关键帧这种方式写动画,就是想交互,可以从事件的传递与响应下手。

    方案一

    我们都知道做动画的其实CALayer,那么就用到了CALayer的一个重要的属性presentationLayer
    我们现在要做的就是,在事件的响应链上下手。如果对事件传递与响应有点模糊的话,可以看这篇文章

    • 在动画View的其父视图上重写touchesBegan方法
    /** 解决重点,在beforeBtn、afterBtn的【父视图】上重写 */
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
        CGPoint point = [[touches anyObject] locationInView:self.view];
        CGRect afterFrame = self.afterBtn.layer.presentationLayer.frame;
        if (CGRectContainsPoint(afterFrame, point)) {
            [self afterAnimationAction];
        }
    }
    

    这样关键帧动画的视图在运动过程中就能响应点击事件了。

    Demo传送门

    后记

    这个UIViewKeyframeAnimationOptionAllowUserInteraction为啥设置了不起作用,还是值得研究一下。有兴趣的大佬多多留言呀~

    相关文章

      网友评论

          本文标题:iOS UIViewKeyframeAnimationOptio

          本文链接:https://www.haomeiwen.com/subject/ybhamktx.html