Skip to content

Instantly share code, notes, and snippets.

@vanbungkring
Created October 31, 2012 10:52
Show Gist options
  • Save vanbungkring/3986409 to your computer and use it in GitHub Desktop.
Save vanbungkring/3986409 to your computer and use it in GitHub Desktop.
UIviewController
//
// KGModal.m
// KGModal
//
// Created by David Keegan on 10/5/12.
// Copyright (c) 2012 David Keegan. All rights reserved.
//
#import "KGModal.h"
#import <QuartzCore/QuartzCore.h>
CGFloat const kFadeInAnimationDuration = 0.3;
CGFloat const kTransformPart1AnimationDuration = 0.2;
CGFloat const kTransformPart2AnimationDuration = 0.1;
NSString *const KGModalGradientViewTapped = @"KGModalGradientViewTapped";
@interface KGModalGradientView : UIView
@end
@interface KGModalContainerView : UIView
@property (retain, nonatomic) CALayer *styleLayer;
@property (retain, nonatomic) UIColor *modalBackgroundColor;
@end
@interface KGModalCloseButton : UIButton
@end
@interface KGModalViewController : UIViewController
@property (strong, nonatomic) KGModalGradientView *styleView;
@end
@interface KGModal()
@property (retain, nonatomic) UIWindow *window;
@property (retain, nonatomic) KGModalViewController *viewController;
@property (retain, nonatomic) KGModalContainerView *containerView;
@property (retain, nonatomic) KGModalCloseButton *closeButton;
@property (retain, nonatomic) UIView *contentView;
@end
@implementation KGModal
+ (id)sharedInstance{
static id sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[[self class] alloc] init];
});
return sharedInstance;
}
- (id)init{
if(!(self = [super init])){
return nil;
}
self.tapOutsideToDismiss = YES;
self.animateWhenDismissed = YES;
self.showCloseButton = YES;
self.modalBackgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
return self;
}
- (void)setShowCloseButton:(BOOL)showCloseButton{
if(_showCloseButton != showCloseButton){
_showCloseButton = showCloseButton;
[self.closeButton setHidden:!self.showCloseButton];
}
}
- (void)showWithContentView:(UIView *)contentView{
[self showWithContentView:contentView andAnimated:YES];
}
- (void)showWithContentView:(UIView *)contentView andAnimated:(BOOL)animated{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.window.opaque = NO;
KGModalViewController *viewController = self.viewController = [[KGModalViewController alloc] init];
self.window.rootViewController = viewController;
CGFloat padding = 17;
CGRect containerViewRect = CGRectInset(contentView.bounds, -padding, -padding);
containerViewRect.origin.x = containerViewRect.origin.y = 0;
containerViewRect.origin.x = round(CGRectGetMidX(self.window.bounds)-CGRectGetMidX(containerViewRect));
containerViewRect.origin.y = round(CGRectGetMidY(self.window.bounds)-CGRectGetMidY(containerViewRect));
KGModalContainerView *containerView = self.containerView = [[KGModalContainerView alloc] initWithFrame:containerViewRect];
containerView.modalBackgroundColor = self.modalBackgroundColor;
containerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|
UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
containerView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
contentView.frame = (CGRect){padding, padding, contentView.bounds.size};
[containerView addSubview:contentView];
[viewController.view addSubview:containerView];
KGModalCloseButton *closeButton = self.closeButton = [[KGModalCloseButton alloc] init];
[closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
[closeButton setHidden:!self.showCloseButton];
[containerView addSubview:closeButton];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tapCloseAction:)
name:KGModalGradientViewTapped object:nil];
// The window has to be un-hidden on the main thread
// This will cause the window to display
dispatch_async(dispatch_get_main_queue(), ^{
[self.window makeKeyAndVisible];
if(animated){
viewController.styleView.alpha = 0;
[UIView animateWithDuration:kFadeInAnimationDuration animations:^{
viewController.styleView.alpha = 1;
}];
containerView.alpha = 0;
containerView.layer.shouldRasterize = YES;
containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
[UIView animateWithDuration:kTransformPart1AnimationDuration animations:^{
containerView.alpha = 1;
containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:kTransformPart2AnimationDuration delay:0 options:UIViewAnimationCurveEaseOut animations:^{
containerView.alpha = 1;
containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
} completion:^(BOOL finished2) {
containerView.layer.shouldRasterize = NO;
}];
}];
}
});
}
- (void)closeAction:(id)sender{
[self hideAnimated:self.animateWhenDismissed];
}
- (void)tapCloseAction:(id)sender{
if(self.tapOutsideToDismiss){
[self hideAnimated:self.animateWhenDismissed];
}
}
- (void)hide{
[self hideAnimated:YES];
}
- (void)hideWithCompletionBlock:(void(^)())completion{
[self hideAnimated:YES withCompletionBlock:completion];
}
- (void)hideAnimated:(BOOL)animated{
[self hideAnimated:animated withCompletionBlock:nil];
}
- (void)hideAnimated:(BOOL)animated withCompletionBlock:(void(^)())completion{
if(!animated){
[self cleanup];
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:kFadeInAnimationDuration animations:^{
self.viewController.styleView.alpha = 0;
}];
self.containerView.layer.shouldRasterize = YES;
[UIView animateWithDuration:kTransformPart2AnimationDuration animations:^{
self.containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished){
[UIView animateWithDuration:kTransformPart1AnimationDuration delay:0 options:UIViewAnimationCurveEaseOut animations:^{
self.containerView.alpha = 0;
self.containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
} completion:^(BOOL finished2){
[self cleanup];
if(completion){
completion();
}
}];
}];
});
}
- (void)cleanup{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.containerView removeFromSuperview];
[self.window removeFromSuperview];
self.window = nil;
}
- (void)setModalBackgroundColor:(UIColor *)modalBackgroundColor{
if(_modalBackgroundColor != modalBackgroundColor){
_modalBackgroundColor = modalBackgroundColor;
self.containerView.modalBackgroundColor = modalBackgroundColor;
}
}
@end
@implementation KGModalViewController
- (void)loadView{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
KGModalGradientView *styleView = self.styleView = [[KGModalGradientView alloc] initWithFrame:self.view.bounds];
styleView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
styleView.opaque = NO;
[self.view addSubview:styleView];
}
@end
@implementation KGModalContainerView
- (id)initWithFrame:(CGRect)frame{
if(!(self = [super initWithFrame:frame])){
return nil;
}
CALayer *styleLayer = self.styleLayer = [[CALayer alloc] init];
styleLayer.cornerRadius = 4;
styleLayer.shadowColor= [[UIColor blackColor] CGColor];
styleLayer.shadowOffset = CGSizeMake(0, 0);
styleLayer.shadowOpacity = 0.5;
styleLayer.borderWidth = 1;
styleLayer.borderColor = [[UIColor whiteColor] CGColor];
styleLayer.frame = CGRectInset(self.bounds, 12, 12);
[self.layer addSublayer:styleLayer];
return self;
}
- (void)setModalBackgroundColor:(UIColor *)modalBackgroundColor{
if(_modalBackgroundColor != modalBackgroundColor){
_modalBackgroundColor = modalBackgroundColor;
self.styleLayer.backgroundColor = [modalBackgroundColor CGColor];
}
}
@end
@implementation KGModalGradientView
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[[NSNotificationCenter defaultCenter] postNotificationName:KGModalGradientViewTapped object:nil];
}
- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
if([[KGModal sharedInstance] backgroundDisplayStyle] == KGModalBackgroundDisplayStyleSolid){
[[UIColor colorWithWhite:0 alpha:0.55] set];
CGContextFillRect(context, self.bounds);
}else{
CGContextSaveGState(context);
size_t gradLocationsNum = 2;
CGFloat gradLocations[2] = {0.0f, 1.0f};
CGFloat gradColors[8] = {0, 0, 0, 0.3, 0, 0, 0, 0.8};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);
CGColorSpaceRelease(colorSpace), colorSpace = NULL;
CGPoint gradCenter= CGPointMake(round(CGRectGetMidX(self.bounds)), round(CGRectGetMidY(self.bounds)));
CGFloat gradRadius = MAX(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
CGContextDrawRadialGradient(context, gradient, gradCenter, 0, gradCenter, gradRadius, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient), gradient = NULL;
CGContextRestoreGState(context);
}
}
@end
@implementation KGModalCloseButton
- (id)init{
if(!(self = [super initWithFrame:(CGRect){0, 0, 32, 32}])){
return nil;
}
static UIImage *closeButtonImage;
static dispatch_once_t once;
dispatch_once(&once, ^{
closeButtonImage = [self closeButtonImage];
});
[self setBackgroundImage:closeButtonImage forState:UIControlStateNormal];
return self;
}
- (UIImage *)closeButtonImage{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor *topGradient = [UIColor colorWithRed:0.21 green:0.21 blue:0.21 alpha:0.9];
UIColor *bottomGradient = [UIColor colorWithRed:0.03 green:0.03 blue:0.03 alpha:0.9];
//// Gradient Declarations
NSArray *gradientColors = @[(id)topGradient.CGColor,
(id)bottomGradient.CGColor];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
//// Shadow Declarations
CGColorRef shadow = [UIColor blackColor].CGColor;
CGSize shadowOffset = CGSizeMake(0, 1);
CGFloat shadowBlurRadius = 3;
CGColorRef shadow2 = [UIColor blackColor].CGColor;
CGSize shadow2Offset = CGSizeMake(0, 1);
CGFloat shadow2BlurRadius = 0;
//// Oval Drawing
UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(4, 3, 24, 24)];
CGContextSaveGState(context);
[ovalPath addClip];
CGContextDrawLinearGradient(context, gradient, CGPointMake(16, 3), CGPointMake(16, 27), 0);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow);
[[UIColor whiteColor] setStroke];
ovalPath.lineWidth = 2;
[ovalPath stroke];
CGContextRestoreGState(context);
//// Bezier Drawing
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(22.36, 11.46)];
[bezierPath addLineToPoint:CGPointMake(18.83, 15)];
[bezierPath addLineToPoint:CGPointMake(22.36, 18.54)];
[bezierPath addLineToPoint:CGPointMake(19.54, 21.36)];
[bezierPath addLineToPoint:CGPointMake(16, 17.83)];
[bezierPath addLineToPoint:CGPointMake(12.46, 21.36)];
[bezierPath addLineToPoint:CGPointMake(9.64, 18.54)];
[bezierPath addLineToPoint:CGPointMake(13.17, 15)];
[bezierPath addLineToPoint:CGPointMake(9.64, 11.46)];
[bezierPath addLineToPoint:CGPointMake(12.46, 8.64)];
[bezierPath addLineToPoint:CGPointMake(16, 12.17)];
[bezierPath addLineToPoint:CGPointMake(19.54, 8.64)];
[bezierPath addLineToPoint:CGPointMake(22.36, 11.46)];
[bezierPath closePath];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadow2Offset, shadow2BlurRadius, shadow2);
[[UIColor whiteColor] setFill];
[bezierPath fill];
CGContextRestoreGState(context);
//// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
//
// ViewController.m
//
#import "ViewController.h"
#import "NetraCell.h"
#import <QuartzCore/QuartzCore.h>
#import "NetraDealsObject.h"
#import "AFNetworking.h"
#import "BlockAlertView.h"
#import "KGModal.h"
//#import "UIViewController+MJPopupViewController.h"
//#import "MJDetailViewController.h"
////define base url
NSString const *url=@"someurl";
NSString const *url_setting=@"someurl";
NSString *test;
@interface ViewController ()
@property(nonatomic,retain) NSMutableArray *deals_data_json;
-(void) fetchDeals;
-(void) loadFirst;
-(void)loadHandsoff;
@end
@implementation ViewController
@synthesize deals_data_json=_deals_data_json;
//@synthesize detailViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadFirst];
self.deals_data_json=[NSMutableArray array];
}
#pragma mark - load first
- (void)loadFirst{
NSLog(@"load-first");
current_page=1;
[self fetchDeals];
}
- (void)viewWillAppear:(BOOL)animated
{
///set hud
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
//Set Apps navbar
self.title=@"Trip Planer";
UINavigationBar *navBar= self.navigationController.navigationBar;
//[navBar setTintColor:[UIColor colorWithRed:0.102 green:0.498 blue:0.812 alpha:1] /*#1a7fcf*/];
UIImage *imagebar = [UIImage imageNamed:@"Menu-Bar"];
[navBar setBackgroundImage:imagebar forBarMetrics:UIBarMetricsDefault];
////set rightbutton menus
UIButton *rightButton = [[UIButton alloc] init];
rightButton.frame=CGRectMake(0,100.0f,23,23);
[rightButton setBackgroundImage:[UIImage imageNamed: @"Filters"] forState:UIControlStateNormal];
[rightButton addTarget:self action:nil forControlEvents:UIControlEventTouchDown];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
[rightButton release];
}
//////////////////////////tableview//////
-(int)numberOfTableviewSection : (UITableView *) tableView
{
return 1;
}
////set length uitableview
-(int) tableView :(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(current_page<total_page){
return [self.deals_data_json count]+1;
}
return [self.deals_data_json count];
}
///
-(UITableViewCell *)DealsCellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"DealsCell";
NetraCell *cell=[mainTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
cell=[[[NetraCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier ]autorelease];
}
else {
}
NetraDealsObject *dataObject=[self.deals_data_json objectAtIndex:indexPath.row];
cell.backgroundView=[ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.imageView setImageWithURL:[NSURL URLWithString:dataObject.thumbnail]
placeholderImage:[UIImage imageNamed:@"Default-picture-alt"]];
if(dataObject.formatted!=NULL){
cell.NetraLabelForPrice.text=dataObject.formatted;
}
else{
cell.NetraLabelForPrice.text=@"Call";
}
///set if null
if(dataObject.provider!=[NSNull null]){
cell.NetraProvider.text=dataObject.provider;
}
else{
cell.NetraProvider.text=@"Featured";
}
/////
if([dataObject.deal_type isEqualToString:@"hotels"])
{
cell.NetraDealsType.image=[UIImage imageNamed:@"round-Hotel"];
}
else if([dataObject.deal_type isEqualToString:@"flights"]){
cell.NetraDealsType.image=[UIImage imageNamed:@"round-Flights"];
}
else if([dataObject.deal_type isEqualToString:@"packages"]){
cell.NetraDealsType.image=[UIImage imageNamed:@"round-Package"];
}
else if ([dataObject.deal_type isEqualToString:@"-"]){
cell.NetraDealsType.image=[UIImage imageNamed:@"round-Premium"];
}
cell.NetraLocation.text=dataObject.location;
cell.NetraHeadline.text=dataObject.headline;
[cell setNeedsLayout];
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row<self.deals_data_json.count){
return [self DealsCellForRowAtIndexPath:indexPath];
}
else{
return [self loadingCell];
}
}
-(UITableViewCell *) loadingCell{
UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
UIActivityIndicatorView *activityIndicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center=cell.center;
[cell addSubview:activityIndicator];
[activityIndicator release];
[activityIndicator startAnimating];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y == scrollView.contentSize.height - scrollView.frame.size.height) {
current_page++;
[self fetchDeals];
return;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 165;
}
////
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 200)];
CGRect welcomeLabelRect = contentView.bounds;
welcomeLabelRect.origin.y = 20;
welcomeLabelRect.size.height = 20;
UIFont *welcomeLabelFont = [UIFont boldSystemFontOfSize:17];
UILabel *welcomeLabel = [[UILabel alloc] initWithFrame:welcomeLabelRect];
welcomeLabel.text = @"Welcome to KGModal!";
welcomeLabel.font = welcomeLabelFont;
welcomeLabel.textColor = [UIColor whiteColor];
welcomeLabel.textAlignment = NSTextAlignmentCenter;
welcomeLabel.backgroundColor = [UIColor clearColor];
welcomeLabel.shadowColor = [UIColor blackColor];
welcomeLabel.shadowOffset = CGSizeMake(0, 1);
[contentView addSubview:welcomeLabel];
CGRect infoLabelRect = CGRectInset(contentView.bounds, 5, 5);
infoLabelRect.origin.y = CGRectGetMaxY(welcomeLabelRect)+5;
infoLabelRect.size.height -= CGRectGetMinY(infoLabelRect);
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
infoLabel.text = @"KGModal is an easy drop in control that allows you to display any view "
"in a modal popup. The modal will automatically scale to fit the content view "
"and center it on screen with nice animations!";
infoLabel.numberOfLines = 6;
infoLabel.textColor = [UIColor whiteColor];
infoLabel.textAlignment = NSTextAlignmentCenter;
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.shadowColor = [UIColor blackColor];
infoLabel.shadowOffset = CGSizeMake(0, 1);
[contentView addSubview:infoLabel];
[[KGModal sharedInstance] showWithContentView:contentView andAnimated:YES];
}
-(void) loadHandsoff{
}
#pragma mark -fetchDeals
-(void) fetchDeals{
NSString *baseUrl=[NSString stringWithFormat:@"%@&page=%d",url,current_page];
NSURL *URL=[NSURL URLWithString:baseUrl];
NSURLRequest *request=[[NSURLRequest alloc] initWithURL:URL];
AFJSONRequestOperation *operation=[[AFJSONRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//calculate total page
total_deals=[[responseObject objectForKey:@"total"]intValue];
total_page=(total_deals/10)+1;
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
for(id dealsDictionary in [responseObject objectForKey:@"deals"]){
NetraDealsObject *deals_data=[[NetraDealsObject alloc] initWithDictionary:dealsDictionary];
[self.deals_data_json addObject:deals_data];
[deals_data release];
}
[mainTableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
[self loadSearchBox];
}];
[operation start];
[operation release];
}
#pragma mark - load searchbox
-(void) loadSearchBox{
NSLog(@"searchBox Loaded");
}
-(void) dealloc{
[super dealloc];
[_deals_data_json release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment