I’m having a problem placing a newly created view over a UICollectionView cell (and grow it to fill the screen). It works great when the collection view is scrolled to the top, however as soon as I scroll, I’m unable to properly place the new view, since the float values I’m gathering are below the window height.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UIView *picturesView = [[UIView alloc] init];
picturesView.frame = CGRectMake(CGRectGetMidX([[collectionView cellForItemAtIndexPath:indexPath] bounds]), CGRectGetMidY([[collectionView cellForItemAtIndexPath:indexPath] bounds]), 100, 100);
[self.view addSubview:picturesView];
[UIView animateWithDuration:0.3 animations:^{
picturesView.frame = CGRectMake(0, 0, windowWidth, windowHeight);
picturesView.alpha = 1.0;
}];
}
The issue here is that the values in the x and y paramaters of CGRectMake are larger than the available window area after scrolling, and the view appears below the scroll clipping (the newly create view appears to slide up from the bottom of the screen, which is not the desired animation).
Logic would leave me to calculate where these should be placed by positionY = ((totalScrollHeight – topScrollAreaClipped) + screenPositionOfCell). The only variable I can’t seem to get is the height of the UICollectionView being clipped by the scroll.
Unless there’s a better way to do this.
You need to compensate for the scroll location of the
UICollectionView. Check thescrollOffsettproperty, and subtract that value from your x/y location to give the correct frame for yourUIView.