Ala'a Al Hallaq
Ala'a Al Hallaq

Reputation: 505

Animating CALayer [filters, compositionFilter, backFilters]

I need to animate one of [filters, compositionFilter, backFilters] on a CALayer in video composition I tried the following code on NSView layer to test if the code works in the first place

    view.wantsLayer = true
    view.layerUsesCoreImageFilters = true
    if let filter = CIFilter.init(name: "CIGaussianBlur", parameters: ["inputRadius": 50])  {
        let animation = CABasicAnimation.init(keyPath: "filters")


        animation.isRemovedOnCompletion = false
        animation.fillMode = .forwards

        animation.duration = 2 //for testing

        animation.autoreverses = false

        animation.fromValue = NSArray.init(array: [])
        animation.toValue = NSArray.init(array: [filter])

        view.layer.add(animation, forKey: "CIGaussianBlurAnimation")
    }

but nothing happens, so I need to know what is the proper way to animate one/all of [filters, compositionFilter, backFilters] properties of CALayer, and whether or not it is supported on AVVideoComposition(on iOS 'I know it is not supported on iOS UIView CALayers')

Upvotes: 2

Views: 877

Answers (1)

Ala'a Al Hallaq
Ala'a Al Hallaq

Reputation: 505

I found 1/2 the solution BUT the main question remains: "is there any way to enable CIFilter on CALayer in video composition on iOS"

it is indicated in the documentation, to update any attribute of the filter after is applied to the layer, you should use

layer.setValue(1, forKeyPath: "backgroundFilters.myFilter.inputRadius")

So it means to animate any filter, we should update the filter properties directly in the animation, so the following should be done

for compositingFilter property:

let animation = CABasicAnimation(keyPath: "compositingFilter.inputRadius")

and for a filter in filters property:

filter.name = "filterName"
layer.filters = [someFilters, filter, otherFilters]
let animation = CABasicAnimation(keyPath: "filters.filterName.inputRadius")

and for a filter in backgroundFilters property:

filter.name = "filterName"
layer.backgroundFilters = [someFilters, filter, otherFilters]
let animation = CABasicAnimation(keyPath: "backgroundFilters.filterName.inputRadius")

and set the animation properties, then add it to the layer layer.add(animation, forKey: "animationKey")

NOTE: animationKey could be any value, even a nil value, but reusing the value will remove the previous animation; even when is running

Upvotes: 3

Related Questions