PQHeartAnimation.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // PQHeartAnimation.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/6/11.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. public class PQHeartAnimation: NSObject {
  10. static let angleArr: [CGFloat] = [CGFloat.pi / 4.0, -CGFloat.pi / 4.0, 0.0]
  11. var isRepeat: Bool = false
  12. static public func showAnimation(isRepeat: Bool = false, point: CGPoint, size: CGFloat = 80.0, baseView: UIView, completeHander: @escaping (_ isFinised: Bool) -> Void) {
  13. if isRepeat, baseView.viewWithTag(cHeartTag) != nil {
  14. return
  15. }
  16. let imgV = UIImageView(frame: CGRect(x: point.x - size / 2.0, y: point.y - size / 2.0, width: size, height: size))
  17. imgV.tag = cHeartTag
  18. imgV.image = UIImage.init().BF_Image(named: "ic_heart")
  19. imgV.contentMode = .scaleAspectFill
  20. baseView.addSubview(imgV)
  21. // 偏移角度
  22. var num = 2
  23. if !isRepeat {
  24. num = Int(arc4random_uniform(3))
  25. }
  26. // BFLog(message: "num = \(num)")
  27. imgV.transform = CGAffineTransform(rotationAngle: angleArr[num])
  28. // 放大动画
  29. let animation = CAKeyframeAnimation(keyPath: "transform.scale")
  30. animation.duration = 0.5
  31. animation.calculationMode = CAAnimationCalculationMode.cubic
  32. animation.values = [1.3, 0.8, 1.0]
  33. imgV.layer.add(animation, forKey: "transform.scale")
  34. UIView.animate(withDuration: 1, delay: 0.5, options: .layoutSubviews, animations: {
  35. imgV.alpha = 0.0
  36. var newFrame = imgV.frame
  37. newFrame.origin.x -= (isRepeat ? -cDefaultMargin * 2 : cDefaultMargin)
  38. newFrame.origin.y -= 45.0
  39. newFrame.size.height += 10.0
  40. newFrame.size.width += 10.0
  41. imgV.frame = newFrame
  42. }) { isOK in
  43. imgV.layer.removeAllAnimations()
  44. imgV.removeFromSuperview()
  45. completeHander(isOK)
  46. }
  47. }
  48. }