2017-04-11 126 views
1

我有什麼象徵心跳的圖像動畫:連續心跳監視器線

heartbeat

,我試圖環在我的子視圖,因此不斷對我的看法,如果你瞭解什麼會出現我意思。到目前爲止,我已經看到它在我的子視圖中無限地移動,但它不是連續的,因爲它只在右側的視圖完全離開後再次出現,因爲我希望它再次出現爲一旦它完全進入視圖,它就象徵着一個實際的心跳。我的代碼看起來對我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) 
+0

你可以顯示你的心跳圖像和你的動畫截圖嗎? –

+0

我已經添加了一個截圖,你可以看到心跳從uiview中消失了,但是我希望它的另一個實例可以直接跟隨,因此看起來不像它的兩個不同的同一圖像實例 –

+0

@ UmarYaqub解決了這個問題嗎? –

回答

0

拿一個UIView並將此作爲類超視。調用開始動畫並在需要時停止動畫。

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