Skip to content

Instantly share code, notes, and snippets.

@AlexRezit
Last active December 10, 2015 05:28
Show Gist options
  • Save AlexRezit/4387482 to your computer and use it in GitHub Desktop.
Save AlexRezit/4387482 to your computer and use it in GitHub Desktop.
//
// MainViewController.m
// VMovier
//
// Created by Alex Rezit on 17/12/2012.
// Copyright (c) 2012 Seymour Dev. All rights reserved.
//
#pragma mark - View control
- (void)resizeSubviews
{
// Side bar view
// Scroll view
CGRect viewBounds = self.view.bounds;
CGPoint sideBarOrigin = CGPointMake(viewBounds.origin.x + viewBounds.size.width, viewBounds.origin.y + viewBounds.size.height);
CGPoint scrollViewOrigin = CGPointMake(viewBounds.origin.x, viewBounds.origin.y);
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
self.sideBarView.frame = CGRectMake(sideBarOrigin.x - kSideBarViewLandscapeMarginRight - kSideBarViewLandscapeWidth,
sideBarOrigin.y - kSideBarViewLandscapeMarginBottom - kSideBarViewLandscapeHeight,
kSideBarViewLandscapeWidth,
kSideBarViewLandscapeHeight);
self.scrollView.frame = CGRectMake(scrollViewOrigin.x + kMainScrollViewLandscapeMarginLeft,
scrollViewOrigin.y + kMainScrollViewLandscapeMarginTop,
kMainScrollViewLandscapeWidth,
kMainScrollViewLandscapeHeight);
} else {
self.sideBarView.frame = CGRectMake(sideBarOrigin.x - kSideBarViewPortraitMarginRight - kSideBarViewPortraitWidth,
sideBarOrigin.y - kSideBarViewPortraitMarginBottom - kSideBarViewPortraitHeight,
kSideBarViewPortraitWidth,
kSideBarViewPortraitHeight);
self.scrollView.frame = CGRectMake(kMainScrollViewPortraitMarginLeft,
kMainScrollViewPortraitMarginTop,
kMainScrollViewPortraitWidth,
kMainScrollViewPortraitHeight);
}
// Page control
CGRect scrollViewFrame = self.scrollView.frame;
self.pageControl.frame = CGRectMake(scrollViewFrame.origin.x,
scrollViewFrame.origin.y + scrollViewFrame.size.height,
scrollViewFrame.size.width,
36);
}
#pragma mark - Interface orientation
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (self.sideBarPopoverController) {
[self.sideBarPopoverController dismissPopoverAnimated:YES];
}
NSInteger page = self.scrollView.currentPage;
[self resizeSubviews];
[self.scrollView handleRotationForInterfaceOrientation:toInterfaceOrientation onPage:page withDuration:duration];
}
//
// MainScrollView.m
// VMovier
//
// Created by Alex Rezit on 17/12/2012.
// Copyright (c) 2012 Seymour Dev. All rights reserved.
//
#pragma mark - View control
- (void)handleRotationForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation onPage:(NSInteger)page withDuration:(NSTimeInterval)duration
{
[self resizeContentViewForInterfaceOrientation:interfaceOrientation];
[self resizeItemsForInterfaceOrientation:interfaceOrientation withDuration:duration];
[self scrollToPage:page forInterfaceOrientation:interfaceOrientation animated:NO];
}
- (void)scrollToPage:(NSInteger)page forInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation animated:(BOOL)animated
{
dispatch_async(dispatch_get_main_queue(), ^{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
[self scrollRectToVisible:CGRectMake(kMainScrollViewLandscapeWidth * page,
0,
kMainScrollViewLandscapeWidth,
kMainScrollViewLandscapeHeight) animated:animated];
} else {
[self scrollRectToVisible:CGRectMake(kMainScrollViewPortraitWidth * page,
0,
kMainScrollViewPortraitWidth,
kMainScrollViewPortraitHeight) animated:animated];
}
});
}
- (void)resizeContentViewForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
dispatch_async(dispatch_get_main_queue(), ^{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
self.contentSize = CGSizeMake(kMainScrollViewLandscapeWidth * self.numberOfPages,
kMainScrollViewLandscapeHeight);
} else {
self.contentSize = CGSizeMake(kMainScrollViewPortraitWidth * self.numberOfPages,
kMainScrollViewPortraitHeight);
}
});
}
- (void)resizeItemsForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation withDuration:(NSTimeInterval)duration
{
[UIView animateWithDuration:duration animations:^{
for (MainGridItemView *gridItem in self.loadedGridItems) {
gridItem.frame = [self frameForGridItemAtIndexPath:gridItem.representedIndexPath];
}
}];
}
#pragma mark - View status
// Count
- (NSInteger)numberOfPages
{
NSInteger numberOfPages = 1;
if ([self.dataSource respondsToSelector:@selector(numberOfPagesInMainScrollView:)]) {
numberOfPages = [self.dataSource numberOfPagesInMainScrollView:self];
}
return numberOfPages;
}
- (NSInteger)numberOfGridItemsInPage:(NSInteger)page
{
NSInteger numberOfGridItems = 0;
if ([self.dataSource respondsToSelector:@selector(mainScrollView:numberOfGridItemsInPage:)]) {
numberOfGridItems = [self.dataSource mainScrollView:self numberOfGridItemsInPage:page];
}
return numberOfGridItems;
}
// Orientation
- (NSUInteger)numberOfRowsForCurrentInterfaceOrientation
{
return UIInterfaceOrientationIsLandscape(self.interfaceOrientation)?3:4;
}
- (NSUInteger)numberOfColumnsForCurrentInterfaceOrientation
{
return UIInterfaceOrientationIsLandscape(self.interfaceOrientation)?4:3;
}
- (UIInterfaceOrientation)interfaceOrientation
{
UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationLandscapeRight;
if ([self.delegate respondsToSelector:@selector(interfaceOrientationForMainScrollView:)]) {
interfaceOrientation = [self.delegate interfaceOrientationForMainScrollView:self];
}
return interfaceOrientation;
}
// Frame
- (CGRect)frameForGridItemAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger page = indexPath.page;
NSInteger itemIndex = indexPath.itemIndex;
NSInteger columnCount = self.numberOfColumnsForCurrentInterfaceOrientation;
NSInteger row = itemIndex / columnCount;
NSInteger column = itemIndex % columnCount;
CGFloat x = self.bounds.size.width * page + kMainScrollViewPageMarginLeft + (kMainScrollViewGridItemWidth + kMainScrollViewGridItemMarginX) * column;
CGFloat y = kMainScrollViewPageMarginTop + (kMainScrollViewGridItemHeight + kMainScrollViewGridItemMarginY) * row;
return CGRectMake(x, y, kMainScrollViewGridItemWidth, kMainScrollViewGridItemHeight);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment