简体   繁体   English

iOS-设置UIView阴影时偏移和插入之间的区别?

[英]iOS - Difference Between Offset And Inset When Setting UIView Shadow?

So I wanted to put a 5 pixel shadow on my UIView on the top, left, and right of it. 因此,我想在其UIView的顶部,左侧和右侧放置5个像素的阴影。 After much trial and error, this code seems to do the trick for me: 经过大量的反复试验,这段代码似乎为我解决了问题:

self.layer.shadowColor = [[UIColor purpleColor] CGColor];
self.layer.shadowOffset = CGSizeMake(0.0f,-5.0f);
self.layer.shadowOpacity = 1.0f;
self.layer.shadowRadius = 0.0f;
CGRect shadowRect = CGRectInset(self.bounds, -10, -5);  // inset top/bottom
self.layer.shadowPath = [[UIBezierPath bezierPathWithRect:shadowRect] CGPath];

It gives me this results, which seems correct: 它给了我这个结果,似乎是正确的:

在此处输入图片说明

Since this came with a lot of trial and error, I still don't quite understand how insets and offsets work. 由于这会带来很多尝试和错误,因此我仍然不太了解插图和偏移量是如何工作的。 I've looked on the internet, but all the definitions are so general (ex. offset is the amount of space by which something is offset...what?). 我在互联网上看过,但是所有定义都很笼统(例如,offset是指可以抵消某些东西的空间量……是什么?)。

Can someone explain to me the difference between insets and offsets when drawing shadows? 有人可以在绘制阴影时向我解释插图和偏移量之间的区别吗? I want to KNOW how it works so next time I won't have to spend so long with trial and error. 我想知道它是如何工作的,因此下次不必再花那么长时间进行反复试验了。 Thanks! 谢谢!

Offset is the amount by which the shadow is moved relative to the object that is being shadowed, so how far to the left or right, top or bottom the shadow is. 偏移量是阴影相对于要阴影的对象移动的量,即阴影到左侧或右侧,顶部或底部的距离。

The inset is the size of the shadow relative to the object. 插图是阴影相对于对象的大小。 So if you have an inset of (-10,-10) then the shadow will be 10 pixels wider and 10 pixels higher than the object. 因此,如果插图具有(-10,-10),则阴影将比对象宽10像素,高10像素。 Note than a negative inset makes the shadow larger. 请注意,负插图会使阴影变大。 A positive inset reduces the size of the shadow. 正插图会减小阴影的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM