简体   繁体   English

按顺序对视图和子视图进行动画处理

[英]Animating Views and subviews in sequence

Im struck with Animation. 我对动画感到震惊。 I would like to animate in below sequence as shown in picture. 我想按以下顺序制作动画,如图所示。

Please click here for Image 请点击这里查看图片

All are views ie, outerView, dot1, dot2, dot3 . 全部都是视图,即outerView,dot1,dot2,dot3。 I've implemented code to animate dots but need your help to animate outerview and adding everything in sequence 我已经实现了对点进行动画处理的代码,但是需要您的帮助来对outerview进行动画处理并按顺序添加所有内容

 let transition = CATransition()
    transition.duration = 2;
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)
    transition.speed = 1.0

    dot3?.layer.add(transition, forKey: nil)
    transition.beginTime = CACurrentMediaTime() + 0.11
    dot2?.layer.add(transition, forKey: nil)
    transition.beginTime = CACurrentMediaTime() + 0.22
    dot1?.layer.add(transition, forKey: nil)

Please help me animating in sequence - outerView starting, dots and closing outerView like shown 请帮助我按顺序进行动画制作-如图所示,outerView的开始,点和结束outerView

It very easy to implement 实施起来很容易

animatedImage(with:duration:)

or 要么

var animationImages: [UIImage]?

example: 例:

UIImageView.animationImages = [image1, image2, image3, image4,...]
    UIImageView.animationDuration = 5
    UIImageView.startAnimating()

You will get ordered animation with couple of lines only 您将只获得几行的有序动画

You're on the right path, except obviously there will be a lot more animations than the few that you've shown in the snippet. 您走在正确的道路上,除了明显的动画比片段中显示的动画要多得多。 There's no reason why you can't continue building this animation using the CAAnimation classes, but I suspect that using the newer UIViewPropertyAnimator classes (will need to target iOS10) will be useful because they allow you to 'scrub' the steps in the animation which will be useful debugging. 没有理由不能继续使用CAAnimation类来构建此动画,但是我怀疑使用较新的UIViewPropertyAnimator类(将需要定位到iOS10)会很有用,因为它们允许您“清理”动画中的步骤,将是有用的调试。 Here's a good intro: dzone.com/articles/ios-10-day-by-day-uiviewpropertyanimator 这是一个很好的介绍:dzone.com/articles/ios-10-day-by-day-uiviewpropertyanimator

Expanding on this comment to a proper answer... 将此评论扩展到适当的答案...

Using animateWithKeyframes is a pretty decent solution to create this animation in code. 使用animateWithKeyframes是在代码中创建此动画的相当不错的解决方案。 Here's a snippet of what this could look like: 这是一个如下所示的代码片段:

let capsule: UIView // ... the container view
let capsuleDots [UIView] //... the three dots
let capsuleFrameWide, capsuleFrameNarrow: CGRect //.. frames for the capsule
let offstageLeft, offstageRight: CGAffineTransform // transforms to move dots to left or right

let animator = UIViewPropertyAnimator(duration: 2, curve: .easeIn)

// the actual animation occurs in 4 steps
animator.addAnimations {

    UIView.animateKeyframes(withDuration: 2, delay: 0, options: [.calculationModeLinear], animations: {

        // step 1: make the capsule grow to large size
        UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.1) {
            capsule.bounds = capsuleFrameWide
        }

        // step 2: move the dots to their default positions, and fade in
        UIView.addKeyframe(withRelativeStartTime: 0.1, relativeDuration: 0.1) {
            capsuleDots.forEach({ dot in
                dot.transform = .identity
                dot.alpha = 1.0
            })
        }

        // step 3: fade out dots and translate to the right
        UIView.addKeyframe(withRelativeStartTime: 0.8, relativeDuration: 0.1) {
            capsuleDots.forEach({ dot in
                dot.alpha = 0.0
                dot.transform = offstageRight

            })
        }

        // step4: make capsure move to narrow width
        UIView.addKeyframe(withRelativeStartTime: 0.9, relativeDuration: 0.1) {
            capsule.bounds = capsuleFrameNarrow
        }
    })
}

Wrapping the keyframes in a UIViewPropertyAnimator makes it easy to scrub the animation (among other things). 将关键帧包装在UIViewPropertyAnimator可以很容易地UIViewPropertyAnimator动画(以及其他操作)。

在此处输入图片说明

In case it's useful for anyone, I've pushed a small project to GitHub that allows you to jump in an explore/refine/debug animations with UIViewPropertyAnimator. 如果它对任何人都有用,我已将一个小项目推送到GitHub ,该项目可让您使用UIViewPropertyAnimator跳入浏览/优化/调试动画。 It includes boilerplate for connecting the UISlider to the animation so all you have to focus on is the animation itself. 它包括将UISlider连接到动画的样板,因此您只需要关注动画本身。

This is all for debugging the animation, for production of course you'll probably want to remove hard coded sizes so it can be potentially reused at different scales etc. 这全部是为了调试动画,为了制作动画,您当然可能希望删除硬编码的大小,以便可以按不同的比例等方式重复使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM