ios - 动画 : continuous heartbeat monitor lines

标签 ios swift uianimation heartbeat

我有一个象征心跳的图像

heartbeat

我正在尝试在我的 subview 中循环播放它,这样如果您理解我的意思,它就会不断出现在我的 View 中。到目前为止,我已经让它在我的 subview 上无限地出现和移动,但是它不是连续的,因为它只是在完全离开右侧的 View 后才从左侧再次出现,因为我希望它再次出现一旦它完全进入视野,它就象征着真正的心跳。我的代码在我的 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 并将此类设置为 super View 。调用开始动画并在需要时停止动画。

@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)
    }
}

关于ios - 动画 : continuous heartbeat monitor lines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43357647/

相关文章:

ios - 如何根据区域设置仅显示日期的日期和月份?

ios - 当将一行插入到SQL Server表中时,如何触发向移动设备的推送通知?

ios - UITextView 中的文本显示不正确,不可见,但占用空间

ios - 如何将用户数据存储到设备?

ios - navigationController 在执行 segue 时为零?

ios - UITableViewCell setEditing 截断标签

iphone - UIAnimation - 从 button.frame.origin.y 显示 UIView

objective-c - UI分段控制动画/翻译

ios - 将阴影添加到 UIImageView 的顶部和底部

iphone - 使用动画从相机中移除滤镜 (GPUImage)