Moving CALayer's mask on scroll in a Scrollview
Asked Answered
G

2

11

I am experimenting with iOS SDK and I have the following UIView structure:

  • UIView
    • UIImageView - only a background image
    • UIImageView (with a CALayer mask)
    • UIScrollView
      • Label

Very simple structure, UIScrollView is transparent layer and second UIImageView has a mask on it. What I am trying to do is that CALayer mask would move its position according to position of the Content in the scroll view. If user scrolls, mask's position should be updated. I already solved this problem by using UIScrollView's delegate:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint contentOffset = scrollView.contentOffset;
    contentOffset.y = -contentOffset.y;
    
    self.overlayImageView.viewlayer.mask.position = contentOffset;
}

Mask is created in viewDidLoad and does not change during view controller's lifecycle.

The problem is that the mask position updating is too slow. That way it looks the mask is following the scroll view's content, not scrolling with it. The scrollViewDidScroll delegate method is called correctly.

For you to better understand the problem, I am attaching a video I made in iOS simulator. http://www.youtube.com/watch?v=w3xRl3LTngY

So the question is:

Is there a way to make mask updating faster or this is the limit of iOS?

Gamboge answered 11/7, 2013 at 11:32 Comment(0)
S
17

CALayer are implicit animated for some properties like position try to disable them:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

[CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.y = -contentOffset.y;

self.overlayImageView.viewlayer.mask.position = contentOffset;
[CATransaction commit];

}
Sculptor answered 11/7, 2013 at 12:27 Comment(1)
Great, fixes the problem. Thank you very much. Mask position changes are much more fluid now.Gamboge
A
0

For me solution was as following:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        layerGradient?.frame = scrollView.safeAreaLayoutGuide.layoutFrame
        CATransaction.commit()
}
Alasteir answered 4/12, 2022 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.