在Scrollview中滚动CALayer的蒙版 [英] Moving CALayer's mask on scroll in a Scrollview
问题描述
我正在尝试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 imageUIImageView
(with aCALayer
mask)UIScrollView
Label
非常简单的结构,
UIScrollView
是透明层,第二个UIImageView
上面有一个掩码。我想要做的是CALayer
掩码会根据滚动视图中内容的位置移动它的位置。如果用户滚动,则应更新蒙版的位置。我已经通过使用UIScrollView
的委托解决了这个问题:Very simple structure,
UIScrollView
is transparent layer and secondUIImageView
has a mask on it. What I am trying to do is thatCALayer
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 usingUIScrollView
's delegate:- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint contentOffset = scrollView.contentOffset; contentOffset.y = -contentOffset.y; self.overlayImageView.viewlayer.mask.position = contentOffset; }
掩码是在
viewDidLoad $ c $中创建的c>并且在视图控制器的生命周期中不会改变。
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=w3xRl3LTngYFor 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屋!
-
-