Skip to content

Instantly share code, notes, and snippets.

@PaulSolt
Created May 14, 2011 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PaulSolt/972555 to your computer and use it in GitHub Desktop.
Save PaulSolt/972555 to your computer and use it in GitHub Desktop.
MyTransparentTableViewController
/*
* The MIT License
*
* Copyright (c) 2011 Paul Solt, PaulSolt@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#import <UIKit/UIKit.h>
@interface MyTransparentTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
CustomView *backgroundView;
UITableView *tableView;
}
@property (nonatomic, retain) PolaroidView *backgroundView;
@property (nonatomic, retain) UITableView *tableView;
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture;
- (void)toggleGUI:(id)sender;
/*
* The MIT License
*
* Copyright (c) 2011 Paul Solt, PaulSolt@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#import "MyTransparentTableViewController.h"
@implementation MyTransparentTableViewController
@synthesize backgroundView, tableView;
-(void)loadView {
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
backgroundView = [[CustomView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:backgroundView];
CGRect tableBounds = self.view.bounds;
// Note: Need to lower the content below the UINavigationbar if it's present
tableBounds.origin.y = self.navigationController.navigationBar.frame.size.height ;
tableBounds.size.height -= tableBounds.origin.y;
self.tableView = [[UITableView alloc] initWithFrame:tableBounds style:UITableViewStyleGrouped];
// Make the tableview transparent to see the background
[self.tableView setBackgroundColor:[UIColor clearColor]];
tableView.backgroundView = nil; // Must clear on iPad to get it to be transparent
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.cancelsTouchesInView = NO; // Must pass through the tap to the UITableView, otherwise we can't touch the rows
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
}
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
CGPoint location = [tapGesture locationInView:self.tableView];
if([tableView indexPathForRowAtPoint:location] && [tableView alpha] != 0) {
// Do nothing, tap handled by the UITableView delegate method or the custom background uiview
} else {
// Inside
[self toggleGUI:nil];
}
}
// Hide show the tableview
- (void)toggleGUI:(id)sender {
if(self.tableView.alpha == 1) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
[self.tableView setAlpha:0.0];
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
[self.tableView setAlpha:1.0];
[UIView commitAnimations];
}
}
// Clean up
- (void)viewDidUnload {
[tableView setDelegate:nil];
[tableView setDataSource:nil];
[tableView release];
tableView = nil;
[backgroundView release];
backgroundView = nil;
[super viewDidUnload];
}
- (void)dealloc {
[tableView setDelegate:nil];
[tableView setDataSource:nil];
[tableView release];
[backgroundView release];
[super dealloc];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment