Animate circular mask of UIImageView in iOS

前端 未结 3 869
挽巷
挽巷 2021-02-19 13:16

I am wondering how to animate the scale of a mask on a uiimageview. Example images attached. The gray boxes are the image background of my uiviewcontroller and is not part of th

3条回答
  •  萌比男神i
    2021-02-19 13:58

    This works for me for a circular mask animation on a map view, you should be able to replace out the map view with an image view, the mask is an oval around the bounds and animates between being the original size and being inset by 5 points repeatedly:

    func setupMapMask() {
        let largeCirclePath = UIBezierPath(ovalInRect: mapView.bounds).CGPath
    
        let mask = CAShapeLayer()
        mask.path = largeCirclePath
        mask.backgroundColor = UIColor.blackColor().CGColor
    
        mapView.layer.mask = mask
    
        let smallCirclePath = UIBezierPath(ovalInRect: CGRectInset(mapView.bounds, 5.0, 5.0)).CGPath
    
        let anim = CABasicAnimation(keyPath: "path")
        anim.toValue = smallCirclePath
        anim.duration = 0.5
        anim.repeatCount = Float.infinity
        anim.autoreverses = true
        mask.addAnimation(anim, forKey: "path")
    }
    

提交回复
热议问题