在Scrollview中滚动CALayer的蒙版 [英] Moving CALayer's mask on scroll in a Scrollview

查看:262
本文介绍了在Scrollview中滚动CALayer的蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试iOS SDK,我有以下 UIView 结构:

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


  • UIView

    • UIImageView - 只有背景图片

    • UIImageView (带 CALayer 面具)

    • UIScrollView

      • 标签

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

          非常简单的结构, UIScrollView 是透明层,第二个 UIImageView 上面有一个掩码。我想要做的是 CALayer 掩码会根据滚动视图中内容的位置移动它的位置。如果用户滚动,则应更新蒙版的位置。我已经通过使用 UIScrollView 的委托解决了这个问题:

          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 it's 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;
          }
          

          掩码是在 viewDidLoad 并且在视图控制器的生命周期中不会改变。

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

          问题是掩码位置更新太慢。这样它看起来像mask是跟随滚动视图的内容,而不是滚动它。正确调用 scrollViewDidScroll 委托方法。

          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.

          为了更好地理解问题,我附加了一个视频我在iOS模拟器中制作。
          http://www.youtube.com/watch?v=w3xRl3LTngY

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

          所以问题是:

          有没有办法让面具更新更快或者这是iOS的限制?

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

          推荐答案

          CALayer是隐式动画的某些属性,如位置尝试禁用它们:

          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];
          
          }
          

          这篇关于在Scrollview中滚动CALayer的蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆