【问题标题】:Preserve Ripple effect over UIImageView在 UIImageView 上保留波纹效果
【发布时间】:2019-11-22 20:59:34
【问题描述】:

我想在我的 UIImageView 上保留波纹效果。我知道我们可以为波纹效果制作图像动画,但要保留。换句话说,我想要一个波纹图像。

我知道我们可以使用动画图像

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:25.0];
[UIView setAnimationTransition:(UIViewAnimationTransition) 110 forView:imgRipple cache:NO]; 
[UIView commitAnimations];

但它有动画效果,不会保留波纹。我认为如果我们在动画结束之前暂停或停止动画,我们可以获得波纹图像。可能吗?我们如何暂停或停止 UIView 动画

如果此技巧有任何替代方法,请提及。

谢谢。

【问题讨论】:

    标签: iphone uiviewanimation


    【解决方案1】:

    我有我的解决方案,

    使用了这个代码

    CFTimeInterval pausedTime = [imgRipple.layer convertTime:CACurrentMediaTime() fromLayer:nil];
         imgRipple.layer.speed = 0.0;
         imgRipple.layer.timeOffset = pausedTime;
    

    它暂停了

    【讨论】:

      【解决方案2】:

      我不确定它是否能给你想要的,但你可以使用这些来停止动画:

      #import <QuartzCore/QuartzCore.h>
      
      [CATransaction begin];
      [myView.layer removeAllAnimations];
      [CATransaction commit];
      

      您可以使用presentationLayer在停止动画之前获取当前状态:

      CALayer* myPreLayer = [myView.layer presentationLayer];
      CATransform3D currentTransform = [myPreLayer transform];
      //if you need some specific info, you can use key-value pairs
      float currentAngle = [[myPreLayer valueForKeyPath:@"transform.rotation.z"] floatValue];
      

      【讨论】:

      • 对不起,它会停止动画而不是暂停它。在调用时它会将图像重置为其原始状态。
      • 可以从presentationLayer获取当前状态,手动设置。
      • presentationLayer ,它是一个属性吗?如何访问它,知道吗?
      • 感谢@erkanyildiz 的帮助。但我从其他地方得到了我的解决方案。
      【解决方案3】:

      斯威夫特 5

      Imageview 设置波纹动画效果很好......

      @IBOutlet weak var imageview: UIImageView!
      
      override func viewDidLoad() {
              super.viewDidLoad()
      
              imageview.layer.cornerRadius = imageview.bounds.width / 2
              self.animateImage()
      
          }
          func animateImage() {
                  addRippleEffect(to: viewAnimation)
              }
      
              func addRippleEffect(to referenceView: UIView) {
                  /*! Creates a circular path around the view*/
                  let path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: referenceView.bounds.size.width, height: referenceView.bounds.size.height))
                  /*! Position where the shape layer should be */
                  let shapePosition = CGPoint(x: referenceView.bounds.size.width / 2.0, y: referenceView.bounds.size.height / 2.0)
                  let rippleShape = CAShapeLayer()
                  rippleShape.bounds = CGRect(x: 0, y: 0, width: referenceView.bounds.size.width, height: referenceView.bounds.size.height)
                  rippleShape.path = path.cgPath
                  rippleShape.fillColor = UIColor.clear.cgColor
                  rippleShape.strokeColor = UIColor.black.cgColor
                  rippleShape.lineWidth = 5
                  rippleShape.position = shapePosition
                  rippleShape.opacity = 0
      
                  /*! Add the ripple layer as the sublayer of the reference view */
                  referenceView.layer.addSublayer(rippleShape)
                  /*! Create scale animation of the ripples */
                  let scaleAnim = CABasicAnimation(keyPath: "transform.scale")
                  scaleAnim.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
                  scaleAnim.toValue = NSValue(caTransform3D: CATransform3DMakeScale(2, 2, 1))
                  /*! Create animation for opacity of the ripples */
                  let opacityAnim = CABasicAnimation(keyPath: "opacity")
                  opacityAnim.fromValue = 1
                  opacityAnim.toValue = 0
                  /*! Group the opacity and scale animations */
                  let animation = CAAnimationGroup()
                  animation.animations = [scaleAnim, opacityAnim]
                  animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
                  animation.duration = CFTimeInterval(1.0)
                  animation.repeatCount = .infinity
                  animation.isRemovedOnCompletion = true
                  rippleShape.add(animation, forKey: "rippleEffect")
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-12
        • 1970-01-01
        相关资源
        最近更新 更多