如何屏蔽UIScrollView? [英] How to mask a UIScrollView?

查看:73
本文介绍了如何屏蔽UIScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个想用来遮罩UIScrollView的图像遮罩. 滚动视图包含1个UIImageView.

I currently have an image mask I want to use to mask a UIScrollView. The scrollview holds 1 UIImageView.

这是我目前在viewdidload中所做的:

Here is what I do at the moment in viewdidload:

CALayer *mask = [CALayer layer];
mask.contents = (id)[UIImage imageNamed:@"ScrollMask.png"].CGImage;
mask.frame = CGRectMake(0, 0, 512, 384);
[Scroll1.layer setMask:mask]; 

这在某种程度上起作用. 它会遮盖住滚动视图内的ImageView,而不是滚动视图本身.

This works to some degree. It masks the ImageView inside the scrollview but not the scrollview itself.

有没有办法掩盖scrollview的CALayer而不是imageview的图层?

Is there a way to mask the scrollview's CALayer and not imageview's layer?

推荐答案

您应将UIScrollView放入UIView,然后将掩码应用于UIView而不是UIScrollView.

You should put the UIScrollView into a UIView, and then apply the mask to the UIView instead of the UIScrollView.

CALayer *mask = [CALayer layer];
mask.contents = (id)[UIImage imageNamed:@"ScrollMask.png"].CGImage;
mask.frame = CGRectMake(0, 0, 512, 384);

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 512, 384)];
[containerView.layer setMask:mask];

[containerView addSubview:Scroll1];
[self.view addSubview:containerView];

这篇关于如何屏蔽UIScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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