Is it possible to check if an AVPlayerLayer which is added as a sublayer to a ViewController's subview's layer is visible or not? Basically if at any point of time, another view be laid over the said subview, I require to pause the AVPlayer, even if it is obscured partially. Note that I set the zPosition of the AVPlayerLayer to -1 when adding it as a sublayer. The implementation is in a framework that I'm working on so I have no control if the end developer puts another subview over the subview or inside the subview that contains the AVPlayerLayer.
Some things to consider are that someone(end developer using my framework) could:
Add multiple subviews over the the subview or inside the subview with my player
Add a sublayer to the subview with my player, thus obscuring the player
Any help is appreciated.
Related
Is there an equivalent of getGlobalVisibleRect in Objective-C ? I'm trying to get the visible rect of a subview that could be obscured by other views above it in the parent view hierarchy but it seems very tedious as some other similar question(s) on SO indicate. The desired implementation is to auto play some content in an AVPlayer view if a certain percentage of the AVPlayer view is visible on screen, similar to how Facebook auto-plays video media in one's feed. However this seems convoluted with having to firstly get the root view and recursively check with its subviews and their subviews and their subviews... and do a CGRectIntersectsRect with the player view & every subview above it. Surely there's got to be a simpler way to do this or am I headed down the path of recursion?
Hello Masters of iOS and Swift,
after two frustrating days, I desperately decided to ask here. The problem:
When adding a CAGradientLayer, the subviews aren't shown.
Details:
I have made a method to add a CAGradientLayer in an extension of
UIView
I simply call the method on any view and this itself works perfectly
But if I try to use this method for a UIView in a viewhierarchy (as a background) unfortunately all subviews aren't visible anymore, the
gradient seems to "overrender" all subviews
if I don't call the "addGradient" method on the container view, all subviews are shown properly
amazing detail: Although the subviews aren't visible, they are somehow present and "active" (e.g. a "invisible" UIButton fires")
I am using Autolayout
Any idea would be appreciated!
Thanks in advance.
LukeSideWalker,
Not sure but you can always try to add your layer below all other layer so it wont cover existing subViews. Try this, in your extension where you add layer to view
self.layer.insertSublayer(layer, atIndex: 0)
Tried adding ImageView as subView worked fine :) Should solve your problem as well.
I encountered a similar problem before and resolved the issue by adding a background view to my view. So the view hierarchy would be like:
view
backgroundView
someView
someViewInsideView
someOtherView
Top views alpha seems to affect subview's alpha too. So I use a view with a clear background colour and use backgroundView to give view's background color and other property's with alpha modifiers.
I'm trying to make my first game using SpriteKit + Swift.
The problem I'm trying to solve right now is that if I add to my main SKView a SUBVIEW without any buttons just with a background color and size and touch it, my main view handles this touch like there is no subview at all.
So the subView of type UIView like doesn't exist for UITapGestureRecognizer of parent view. And the only way I found to solve it, is to put a subView-sized button on the subview without text and handler. But this way looks creepy...
It could be that your subView (or one of its parents views) has the userInteractionEnabled set to false. UIView has the userInteractionEnabled set to true by default, but for example UIImageView (which is a subclass of UIView) has the userInteractionEnabled set to false by default.
When set to NO, user events—such as touch and keyboard—intended for
the view are ignored and removed from the event queue. When set to
YES, events are delivered to the view normally. The default value of
this property is YES.
(...)
Note
Some UIKit subclasses override this property and return a different
default value. See the documentation for that class to determine if it
returns a different value.
See Apple docs for more info.
It could be not your subView by itself, but one of its parents views too, because this setting is propagated down.
If set to NO, this view (along with its subviews) is excluded from
receiving touches. Touches on this view or one of its subviews "fall
through" to a view behind it.
source: Programming iOS by Matt Neuburg
For more, check On iOS, if a superview's userInteractionEnabled is NO, then all subviews are disabled as well?
Basically, I'm working on an app on the iPad that involves a line which the user can scroll horizontally through on the screen. It's set up so that there is a UIScrollView and a UIView on top that uses drawRect. I also have a UIView that's basically a ball which I can move around via touch, and also detects when it is within the UIScrollView and when it is on the line that is painted in the UIView.
Here's my main problem, though. Both the UIScrollView and the ball are subviews to the same view controller, and I want to make it so that, when the ball is dragged into the UIScrollView, that it will scroll along with it and even disappear if need be. How should I do this? should I just make the ball a subview of the UIScrollView when this happens, or should I do it some other way?
Use a container view. Put the ball in the transparent container subview and that is the view that moves around ( taking the ball with it). When you want the scroll view or line to move with the ball, insert them into the container at position 0 (under the ball).
I am building a word tetris kind of an app. Now I have to move a Uiview containing 8 uibuttons towards the bottom of the screen based on time and also track the position of uibuttons as the user taps specified button.
Am I suppose to use Block based animation or core animation to do the task.
Currently if I am animating frame and center of superview it seems like I have to do the same for the subviews as well inside the block.
Any input would be handy.
You can use UIView block animation to animate a view and it's subviews quite simply.
However, neither UIView animation nor core animation will let the user click on buttons as they are animating. Button actions don't work at all on "in flight" animations. There's no automatic way to do that. (At least none that I know of.)
Instead, you have to add a tap gesture recognizer to the parent view, and do hit testing on the presentation layer of your parent view to see which sublayer of the presentation layer is tapped.