我有一个包含3个组件的视图控制器:
1)表格视图
2)UIView(在Tableview上)
3)UIView中的AVPlayerLayer
滚动表格视图时,我想要调整UIView及其图层(AVPlayerLayer)的大小,而我设法做到了。
我的问题是视图的调整大小与图层的调整不同步,而我希望视图变小,甚至图层也会跟随视图自身的缩小。
以下是显示我的行为的视频:https://streamable.com/s/z4yc2/eakmvv
这是视图控制器的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.lastContentOffset = 0;
[self setupPlayer];
[self.table reloadData];
}
-(void)setupPlayer
{
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:@"testurlvideostreaming"]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer * avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
self.avPlayerLayer.frame = CGRectMake(0, 0, self.viewStreaming.frame.size.width, self.viewStreaming.frame.size.height);
[self.avPlayerLayer setBackgroundColor:[UIColor grayColor].CGColor];
[self.viewStreaming.layer addSublayer:self.avPlayerLayer];
[avPlayer play];
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView.indexPathsForVisibleRows indexOfObject:indexPath] == NSNotFound &&
indexPath.row >= 5 &&
self.lastContentOffset < tableView.contentOffset.y)
{
float percentage = .7f;
int width = 50;
int height = width;
int xPosition = self.viewStreaming.superview.frame.size.width - width;
int yPosition = self.viewStreaming.superview.frame.size.height * ((1 - percentage) / 2);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.viewStreaming.frame = CGRectMake(xPosition, yPosition, width, height);
self.avPlayerLayer.frame = CGRectMake(0, 0, self.viewStreaming.frame.size.width, self.viewStreaming.frame.size.height);
[UIView commitAnimations];
}
}