I have a UIImageView
displaing an image. This view's layer is masked with CAShapeLayer
in order to create circular "hole" in the image. To create the hole I use UIBezierPath
with .usesEvenOddFillRule = true
.
It works fine when static. But I need that hole to move with user finger. To do that I create new UIBezierPath
with even-odd rule each time user moves their finger. On smaller phones with smaller images it looks OK but on iPhone 6 Plus it is choppy.
Any ideas on how to make it smooth are very wellcome. I cannot just move the frame of masking CAShapeLayer
- it would move the hole bot also hide some edges of the image. So the only way is to change its .path
each time user moves finger and that is slow.
EDIT: matt's answer would work in some scenarios but not in my case: I am not displaying the whole image only a part of it defined by UIBezierPath
. This part is most often oval (but can be rectangular or rounded rectangle) and it has "hole" cut in it. While the hole is mowing with users finger the displayed part/shape of the image does not change - it is static.
The ineficient solution that was in place so far wa:
- Create
UIBezierPath
with boundary of displayed part of the image - Set 'even off fill rule' on it
- Add
UIBezierPath
of the hole to it - Set it as path of
CAShapeLayer
with some opaque fill color - Use that
CAShapeLayer
as mask of theUIImageView
This procedure was repeated each time a user moved their finger. I cannot simply move the whole mask layer as that would also change the part of the image being displayed. I what it to stay static and move only the hole in it.