动画:连续心跳监测线

Animation: continuous heartbeat monitor lines

我有一个象征心跳的图像

并且我正在尝试在我的子视图中循环播放它,这样如果您理解我的意思,它就会不断出现在我的视图中。到目前为止,我已经让它在我的子视图上无限地出现和移动,但是它不是连续的,因为它只是在完全离开右侧的视图后才从左侧再次出现,因为我希望它再次出现一旦它完全进入视野,它就象征着真正的心跳。我的代码在 viewDidLoad:

上看起来像这样
self.heartbeatLoading.frame.origin = CGPoint(x: 0 - heartbeatLoading.frame.size.width, y: 10)    
let animation: UIViewAnimationOptions = [.repeat, .curveLinear]

UIView.animate(withDuration: 5.0, delay: 0, options: animation, animations: {
        self.heartbeatLoading.frame = self.heartbeatLoading.frame.offsetBy(dx: self.view.frame.width + self.heartbeatLoading.frame.width, dy: 0.0)
}, completion: nil)

取一个UIView并将这个class设置为超级视图。调用开始动画并在需要时停止动画。

@IBDesignable class Pulse : UIView
{
    var animatingView : UIView?
    @IBInspectable  var pulseImage: UIImage =  UIImage(named: "defaultImage")!{
        didSet {
                animatingView = UIView(frame: self.bounds)
                let imgView = UIImageView(frame : self.bounds)
                animatingView?.clipsToBounds = true
                animatingView?.addSubview(imgView)
                self.addSubview(animatingView!)
        }
    }
    func startAnimationWith(duration : Double = 0.5)
    {
        if animatingView != nil
        {
            animatingView?.frame = CGRect(x: 0, y: 0, width: 0, height: (animatingView?.frame.size.height)!)
            UIView.animate(withDuration: duration, delay: 0, options: .repeat, animations: {
                self.animatingView?.frame = CGRect(x: 0, y: 0, width: (self.animatingView?.frame.size.width)!, height: (self.animatingView?.frame.size.height)!)
            }, completion: { (isDone) in

            })
        }
    }
    func stopAnimation()
    {
        if animatingView != nil
        {
            self.animatingView!.layer.removeAllAnimations()
        }
    }
    override init(frame : CGRect) {
        super.init(frame : frame)
    }
    convenience init() {
        self.init(frame:CGRect.zero)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}