1

I am doing an animation practice from Apple's tutorial. The task is do build a UI of a music player. Each of control buttons has a "shadow" view underneath it, which appears when button is pressed:

enter image description here

Here is my code in viewDidLoad():

background.layer.cornerRadius = background.bounds.width / 2
background.backgroundColor = .clear

The thing is that tutorial insists to also add a clipsToBounds property for each of shadow views:

background.layer.cornerRadius = background.bounds.width / 2
background.clipsToBounds = true
background.alpha = 0.0

When I run my code, everything works fine, and shadow views appear as circles. So why use clipsToBounds here, considering that view is clipping to bounds of itself? Am I missing something here?

And while it's in the code - is using .alpha property a better practice than changing backgroundColor to .clear and back for animation purposes?

Denys Triasunov
  • 539
  • 1
  • 4
  • 24
  • Triasunov https://stackoverflow.com/questions/20449256/how-does-clipstobounds-work . It's better not to play with alpha property i suppose. – Tushar Sharma Oct 26 '17 at 20:20
  • In my understanding, clipsToBounds is just used for when the image might be bigger than the frame. It makes sure that the content of your object won't spill over outside of the objects bounds. – MSU_Bulldog Oct 26 '17 at 20:22
  • Yes, I've read this post, but in answer's example subview is clipping to bounds of a superview. In my case, tutorial suggests to clip to own bounds, which is confusing to me. It only has a background color - so how can that spill outside the view? – Denys Triasunov Oct 26 '17 at 20:23
  • I think that you are setting the shadow for the view layer with UIBezierPath, and the layer has its own maskToBounds property. – Francesco Deliro Oct 26 '17 at 21:56

0 Answers0