在UIView中创建内部阴影 [英] Create inner shadow in UIView

查看:541
本文介绍了在UIView中创建内部阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPad上的 UIView 上创建一个内部阴影:

I would like create an inner shadow on my UIView on iPad like that :

UIView 可以改变大小,所以我不能使用简单的图像来创建这种阴影。

This UIView could change size so I can't use a simple image to create this kind of shadow.

我已经测试了 setShadow 等,但它只是一个创造的阴影。

I have tested with setShadow etc., but it's only a dropshadow that is created.

任何想法如何创造这种阴影?

Any idea how to create this kind of shadow?

推荐答案

将阴影创建为特定大小的透明图层,然后创建一个可伸缩的图像,如下所示:

Create the shadow as a transparent layer at a particular size, then create a stretchable image, like this:

UIImage *shadowImage = [UIImage imageNamed:@"shadow.png"];
shadowImage = [shadowImage stretchableImageWithLeftCapWidth:floorf(shadowImage.size.width/2) topCapHeight:floorf(shadowImage.size.height/2)];

然后您可以将其作为图像放在UIImageView中,并使用contentMode缩放以适应它并且它将起作用你想要的方式。

You can then put that as the image in a UIImageView with contentMode scale to fit and it will work the way you want.

让我们说你的观点被称为myView。你可以像这样添加阴影:

Lets say your view is called "myView". You can add the shadow like this:

UIImageView *shadowImageView = [[UIImageView alloc] initWithImage:shadowImage];
shadowImageView.contentMode = UIViewContentModeScaleToFill;
shadowImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
shadowImageView.frame = myView.bounds;
[myView addSubview:shadowImageView];
[shadowImageView release]; // only needed if you aren't using ARC

您还可以在界面构建器中执行大部分操作如果您愿意,只要您在代码中创建可伸缩图像。

You can also do most of this in interface builder if you prefer, as long as you create the stretchable image itself in code.

这篇关于在UIView中创建内部阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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