简体   繁体   中英

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. 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?).

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. Note than a negative inset makes the shadow larger. A positive inset reduces the size of the shadow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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