Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何隐藏UIView的子帧?_Iphone_Ios_Objective C_Uiview - Fatal编程技术网

Iphone 如何隐藏UIView的子帧?

Iphone 如何隐藏UIView的子帧?,iphone,ios,objective-c,uiview,Iphone,Ios,Objective C,Uiview,假设我有一个框架为(0,0100,30)的UIImageView 。已为该imageView分配了一个图像 什么是只显示部分图像的最简单方法 例如:仅显示在点30-60(宽度)和点0-30(高度)中的内容。这意味着应该隐藏图像的左边缘和右边缘 为了澄清,我不想移动视图或改变其大小,我只想隐藏其帧的一个子矩形。我认为掩蔽图像是最好的选择。但是,如果要旋转、变换、设置动画或想要清晰的背景,可以执行以下操作: 创建一个子视图,该子视图是要显示的图像的大小。确保将clipstobunds设置为true,

假设我有一个框架为(0,0100,30)的UIImageView 。已为该imageView分配了一个图像

什么是只显示部分图像的最简单方法

例如:仅显示在点30-60(宽度)和点0-30(高度)中的内容。这意味着应该隐藏图像的左边缘和右边缘


为了澄清,我不想移动视图或改变其大小,我只想隐藏其帧的一个子矩形。

我认为掩蔽图像是最好的选择。但是,如果要旋转、变换、设置动画或想要清晰的背景,可以执行以下操作:

创建一个子视图,该子视图是要显示的图像的大小。确保将
clipstobunds
设置为true,并相应地定位图像

UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];

//this is the part of the image you wish to see
UIView *imageWindow = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 30, 30)];
imageWindow.clipsToBounds = YES;

//your image view is the height and width of mainView and x and y is imageWindow - mainView. You can do this manually or put in calculations.
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageWindow.frame.origin.x - mainView.frame.origin.x, imageWindow.frame.origin.y - mainView.frame.origin.y, mainView.frame.size.width, mainView.frame.size.height)];
myImage.image = [UIImage imageNamed:@"1024x1024.png"];

[imageWindow addSubview:myImage];
[mainView addSubview:imageWindow];
[self.view addSubview:mainView];

查看我的代码,我认为没有必要使用
mainView
,您可以直接将
imageWindow
添加到self.view中。

您可以设置一个掩码

CALayer *maskLayer = [CALayer layer];
maskLayer.backgroundColor = [UIColor blackColor].CGColor;
maskLayer.frame = CGRectmake(30.0, 0.0, 30.0, 30.0);

view.layer.mask = maskLayer;

遮罩可以是任何类型的层,因此您甚至可以使用
CAShapeLayer
制作复杂的遮罩,并做一些非常酷的事情。

我发现这个解决方案适合我


在“CALayer*”类型的对象上找不到属性fillColor。我是否缺少导入?因此,将背景色设置为CGColor(而不是fillColor)就成功了@Ryan Poolos,编辑你的答案,我会接受的。哦,对不起
fillColor
CAShapeLayer
上的一个属性,我通常使用形状层作为遮罩。习惯的产物pi试过CALayer,但不适合我。所以我尝试了CAShaperLayer,它很有效。
func mask(withRect rect: CGRect, inverse: Bool = false) {
    let path = UIBezierPath(rect: rect)
    let maskLayer = CAShapeLayer()

    if inverse {
        path.append(UIBezierPath(rect: self.view.bounds))
        maskLayer.fillRule = kCAFillRuleEvenOdd
    }

    maskLayer.path = path.cgPath
    imageView?.layer.mask = maskLayer
}