Reputation: 8309
Here's a piece of code, currently driving me nuts. It's a UIView, with an AVPlayerLayer (as a sublayer) and a UITableView (as a subview). Longpressing on the movie should hide the movie and show the table. Longpressing on the table, again, should hide the table and show the movie back.
playerView3 = [[UIView alloc] init];
[playerView3 addGestureRecognizer:tap3];
[playerView3 addGestureRecognizer:longPress3];
[playerView3 setFrame:CGRectMake(682, 0, 341, 768)];
[self.view addSubview:playerView3];
[self table3:CGRectMake(682, 0, 341, 768)];
[playerView3 addSubview:table3];
[table3 setHidden:YES];
[table3 setTag:3];
NSURL *url3 = [[NSBundle mainBundle] URLForResource:@"3" withExtension:@"MOV"];
AVURLAsset *asset3 = [AVURLAsset URLAssetWithURL:url3 options:nil];
AVPlayerItem *playerItem3 = [AVPlayerItem playerItemWithAsset:asset3];
player3 = [AVPlayer playerWithPlayerItem:playerItem3];
[player3 setActionAtItemEnd:AVPlayerActionAtItemEndNone];
playerLayer3 = [AVPlayerLayer playerLayerWithPlayer:player3];
[playerView3.layer addSublayer:playerLayer3];
playerLayer3.frame = playerView3.layer.bounds;
playerLayer3.videoGravity = AVLayerVideoGravityResizeAspect;
Here's the action method for longpressing: if (!(sender.state == UIGestureRecognizerStateBegan)) return;
if([playerLayer3 isHidden])
{
[playerLayer3 setHidden:NO];
[table3 setHidden:YES];
}
else
{
[playerLayer3 setHidden:YES];
[table3 setHidden:NO];
}
Note that I am giving the same frame
values for both the table and the movie. But I get them in different areas of the screen! Can someone guide me what's going wrong?
Here are some screenshots:
The movie, before the longpress, at its expected location of (341, 0, 341, 768)
After the longpress, the table is here (at an unexpected location) DESPITE HAVING THE SAME FRAME VALUE as the movie (341, 0, 341, 768)
Now, If I am having a new frame value of (682, 0, 341, 768) for the movie:
The table is not even seen for this frame (682, 0, 341, 768) after the longpress:
Any help is greatly appreciated!
Upvotes: 0
Views: 328
Reputation: 450
The tableview is a subview of the playerview so its frame is based on the frame of the playerview. To get them to appear in the same space you need to either add the tableview as a subview of self.view or adjust the frame manually.
[self.view addSubview:table3];
Upvotes: 1
Reputation: 17186
Frames are with respect to its super view. You have added playerview3 to self.view and table3 to playerview3.
If you want to keep the frame same, you should add the table to self.view only. It means
[playerView3 addSubview:table3];
above line should be changed with
[self.view addSubview:table3];
And if you want to add table3 to playerView3 then, its frame should start with 0,0 and not with 682,0.
Hope it will help.
Upvotes: 2