Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

Commit

Permalink
Add TSActionSheet
Browse files Browse the repository at this point in the history
  • Loading branch information
takashisite committed May 21, 2012
1 parent a95c217 commit 12d1bd8
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 4 deletions.
12 changes: 12 additions & 0 deletions Demo/TSPopoverDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
5D1B3B2D155A385D00156B09 /* TSPopoverController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D1B3B2C155A385D00156B09 /* TSPopoverController.m */; };
5D1B3B31155AA4A000156B09 /* TSPopoverTouchView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D1B3B30155AA4A000156B09 /* TSPopoverTouchView.m */; };
5D9EB0A9155BEB1B002A2E68 /* TSPopoverPopoverView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D9EB0A8155BEB1B002A2E68 /* TSPopoverPopoverView.m */; };
5DB5C6D0156A28090032A8E9 /* TSActionSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DB5C6CF156A28090032A8E9 /* TSActionSheet.m */; };
5DB5C6D8156A37810032A8E9 /* UIBarButtonItem+WEPopover.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DB5C6D7156A37800032A8E9 /* UIBarButtonItem+WEPopover.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -39,6 +41,10 @@
5D1B3B32155AA55F00156B09 /* TSPopoverTouchesDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = TSPopoverTouchesDelegate.h; path = ../../TSPopover/TSPopoverTouchesDelegate.h; sourceTree = "<group>"; };
5D9EB0A7155BEB1B002A2E68 /* TSPopoverPopoverView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TSPopoverPopoverView.h; path = ../../TSPopover/TSPopoverPopoverView.h; sourceTree = "<group>"; };
5D9EB0A8155BEB1B002A2E68 /* TSPopoverPopoverView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TSPopoverPopoverView.m; path = ../../TSPopover/TSPopoverPopoverView.m; sourceTree = "<group>"; };
5DB5C6CE156A28090032A8E9 /* TSActionSheet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TSActionSheet.h; path = ../../TSPopover/TSActionSheet.h; sourceTree = "<group>"; };
5DB5C6CF156A28090032A8E9 /* TSActionSheet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TSActionSheet.m; path = ../../TSPopover/TSActionSheet.m; sourceTree = "<group>"; };
5DB5C6D6156A37800032A8E9 /* UIBarButtonItem+WEPopover.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIBarButtonItem+WEPopover.h"; path = "../../TSPopover/UIBarButtonItem+WEPopover.h"; sourceTree = "<group>"; };
5DB5C6D7156A37800032A8E9 /* UIBarButtonItem+WEPopover.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIBarButtonItem+WEPopover.m"; path = "../../TSPopover/UIBarButtonItem+WEPopover.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -109,13 +115,17 @@
5D1B3B2A155A383200156B09 /* TSPopover */ = {
isa = PBXGroup;
children = (
5DB5C6D6156A37800032A8E9 /* UIBarButtonItem+WEPopover.h */,
5DB5C6D7156A37800032A8E9 /* UIBarButtonItem+WEPopover.m */,
5D1B3B2B155A385D00156B09 /* TSPopoverController.h */,
5D1B3B2C155A385D00156B09 /* TSPopoverController.m */,
5D1B3B2F155AA4A000156B09 /* TSPopoverTouchView.h */,
5D1B3B30155AA4A000156B09 /* TSPopoverTouchView.m */,
5D1B3B32155AA55F00156B09 /* TSPopoverTouchesDelegate.h */,
5D9EB0A7155BEB1B002A2E68 /* TSPopoverPopoverView.h */,
5D9EB0A8155BEB1B002A2E68 /* TSPopoverPopoverView.m */,
5DB5C6CE156A28090032A8E9 /* TSActionSheet.h */,
5DB5C6CF156A28090032A8E9 /* TSActionSheet.m */,
);
name = TSPopover;
path = TSPopoverDemo;
Expand Down Expand Up @@ -189,6 +199,8 @@
5D1B3B2D155A385D00156B09 /* TSPopoverController.m in Sources */,
5D1B3B31155AA4A000156B09 /* TSPopoverTouchView.m in Sources */,
5D9EB0A9155BEB1B002A2E68 /* TSPopoverPopoverView.m in Sources */,
5DB5C6D0156A28090032A8E9 /* TSActionSheet.m in Sources */,
5DB5C6D8156A37810032A8E9 /* UIBarButtonItem+WEPopover.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
19 changes: 18 additions & 1 deletion Demo/TSPopoverDemo/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "MainViewController.h"
#import "TSPopoverController.h"
#import "TSActionSheet.h"

@implementation MainViewController

Expand All @@ -24,7 +25,7 @@ - (void)viewDidLoad
{
[super viewDidLoad];

UIBarButtonItem * topRightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(showPopover:forEvent:)];
UIBarButtonItem * topRightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(showActionSheet:forEvent:)];
self.navigationItem.leftBarButtonItem = topRightButton;
self.navigationItem.rightBarButtonItem = topRightButton;
self.navigationController.toolbarHidden = NO;
Expand Down Expand Up @@ -113,4 +114,20 @@ -(void)showPopover:(id)sender forEvent:(UIEvent*)event

}

-(void) showActionSheet:(id)sender forEvent:(UIEvent*)event
{
TSActionSheet *actionSheet = [[TSActionSheet alloc] initWithTitle:@"action sheet"];
[actionSheet destructiveButtonWithTitle:@"hoge" block:nil];
[actionSheet addButtonWithTitle:@"hoge1" block:^{
NSLog(@"pushed hoge1 button");
}];
[actionSheet addButtonWithTitle:@"moge2" block:^{
NSLog(@"pushed hoge2 button");
}];
[actionSheet cancelButtonWithTitle:@"Cancel" block:nil];
actionSheet.cornerRadius = 5;

[actionSheet showWithTouch:event];
}

@end
7 changes: 7 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
TSPopover
Copyright (C) 2012 Takashi Saito

WEPopover
Copyright (c) 2010 Werner IT Consultancy

BlockAlertsAnd-ActionSheets
Copyright (C) 2012 by Arrived Inc.

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.
Expand Down
Binary file modified Screenshots/Screenshot_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions TSPopover/TSActionSheet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// TSActionSheet.h
// TSPopoverDemo
//
// Created by Saito Takashi on 5/21/12.
// Copyright (c) 2012 ar.ms. All rights reserved.
//

#import <Foundation/Foundation.h>

@class TSPopoverController;

@interface TSActionSheet : UIView
{
TSPopoverController *popoverController ;
NSMutableArray *buttonsMutableArray;
}

@property (strong, nonatomic) UIColor *titleColor;
@property (strong, nonatomic) UIFont *titleFont;
@property (strong, nonatomic) UIColor *popoverBaseColor;
@property (nonatomic) int cornerRadius;
@property (nonatomic) BOOL popoverGradient;
@property (nonatomic) BOOL buttonGradient;

- (id)initWithTitle:(NSString *)title;
- (void)cancelButtonWithTitle:(NSString *) title block:(void (^)()) block;
- (void)destructiveButtonWithTitle:(NSString *) title block:(void (^)()) block;
- (void)addButtonWithTitle:(NSString *) title block:(void (^)()) block;
- (void)addButtonWithTitle:(NSString *)title color:(UIColor*)color block:(void (^)())block;
- (void) showWithTouch:(UIEvent*)senderEvent;

@end
203 changes: 203 additions & 0 deletions TSPopover/TSActionSheet.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
//
// TSActionSheet.m
// TSPopoverDemo
//
// Created by Saito Takashi on 5/21/12.
// Copyright (c) 2012 ar.ms. All rights reserved.
//

#import "TSActionSheet.h"
#import "TSPopoverController.h"
#import "UIBarButtonItem+WEPopover.h"

#define CORNER_RADIUS 5
#define BORDER 5
#define TITLE_SHADOW_OFFSET CGSizeMake(0, -1)
#define BUTTON_HEIGHT 35

@implementation TSActionSheet

@synthesize cornerRadius = _cornerRadius;
@synthesize titleColor = _titleColor;
@synthesize titleFont = _titleFont;
@synthesize popoverBaseColor = _popoverBaseColor;
@synthesize popoverGradient = _popoverGradient;
@synthesize buttonGradient = _buttonGradient;


- (id)initWithTitle:(NSString *)title
{
if ((self = [super init]))
{
self.frame = CGRectMake(0,0, 200, 300);
buttonsMutableArray = [[NSMutableArray alloc] init];
self.cornerRadius = CORNER_RADIUS;
self.buttonGradient = YES;

popoverController = [[TSPopoverController alloc] init];
popoverController.titleText = title;
popoverController.titleColor = [UIColor whiteColor];
popoverController.titleFont = [UIFont boldSystemFontOfSize:14];
popoverController.popoverBaseColor = [UIColor blackColor];
popoverController.popoverGradient = YES;
}
return self;
}

- (void)addButtonWithTitle:(NSString *)title color:(UIColor*)color block:(void (^)())block
{
[buttonsMutableArray addObject:[NSArray arrayWithObjects:
block ? [block copy] : [NSNull null],
title,
color,
nil]];
}

- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block
{
[self addButtonWithTitle:title color:[UIColor grayColor] block:block];
}

- (void)destructiveButtonWithTitle:(NSString *)title block:(void (^)())block
{
[self addButtonWithTitle:title color:[UIColor redColor] block:block];
}

- (void)cancelButtonWithTitle:(NSString *)title block:(void (^)())block
{
[self addButtonWithTitle:title color:[UIColor blackColor] block:block];
}




//- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated
- (void) showWithTouch:(UIEvent*)senderEvent
{
NSUInteger i = 1;
NSUInteger buttonHeight = BUTTON_HEIGHT;
NSUInteger buttonY = BORDER;
for (NSArray *button in buttonsMutableArray)
{
NSString *title = [button objectAtIndex:1];
UIColor *color = [button objectAtIndex:2];

UIImage *image = [self buttonImage:color];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(BORDER, buttonY, self.bounds.size.width-BORDER*2, buttonHeight);
button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
button.titleLabel.minimumFontSize = 6;
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.textAlignment = UITextAlignmentCenter;
button.titleLabel.shadowOffset = TITLE_SHADOW_OFFSET;
button.backgroundColor = [UIColor clearColor];
button.tag = i++;

[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
button.accessibilityLabel = title;

[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:button];
buttonY += buttonHeight + BORDER;

}
CGRect frame = self.frame;
frame.size.height = buttonY;
self.frame = frame;

popoverController.contentView = self;
popoverController.cornerRadius = self.cornerRadius;
[popoverController showPopoverWithTouch:senderEvent];
}

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
if (buttonIndex >= 0 && buttonIndex < [buttonsMutableArray count])
{
id obj = [[buttonsMutableArray objectAtIndex: buttonIndex] objectAtIndex:0];
if (![obj isEqual:[NSNull null]])
{
((void (^)())obj)();
}
}

if (animated)
{
[popoverController dismissPopoverAnimatd:YES];
[self removeFromSuperview];
}
else
{
[popoverController dismissPopoverAnimatd:NO];
[self removeFromSuperview];
}
}

#pragma mark - Action

- (void)buttonClicked:(id)sender
{
/* Run the button's block */
int buttonIndex = [sender tag] - 1;
[self dismissWithClickedButtonIndex:buttonIndex animated:YES];
}

#pragma mark - UIImage

-(UIImage*)buttonImage:(UIColor*)color
{

//Size
float buttonWidth = self.frame.size.width - (BORDER*2);

UIGraphicsBeginImageContextWithOptions(CGSizeMake(buttonWidth, BUTTON_HEIGHT), NO, 0);

//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();

//// Gradient Declarations
NSArray* gradientColors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:1.0 alpha:0.4].CGColor,
(id)[UIColor colorWithWhite: 1.0 alpha: 0.3].CGColor,
(id)[UIColor colorWithWhite: 1.0 alpha: 0.2].CGColor,
(id)[UIColor clearColor].CGColor,
(id)[UIColor colorWithWhite: 1.0 alpha: 0.1].CGColor,
(id)[UIColor colorWithWhite:1.0 alpha:0.2].CGColor, nil];
CGFloat gradientLocations[] = {0, 0.1, 0.49, 0.5, 0.51, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);


//// Rounded Rectangle Drawing
UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, buttonWidth, BUTTON_HEIGHT) cornerRadius: self.cornerRadius];
[color setFill];
[roundedRectanglePath fill];


if(self.buttonGradient){
//// GradientPath Drawing
UIBezierPath* gradientPath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, buttonWidth, BUTTON_HEIGHT) cornerRadius: self.cornerRadius];
CGContextSaveGState(context);
[gradientPath addClip];
CGContextDrawLinearGradient(context, gradient, CGPointMake(buttonWidth/2, 0), CGPointMake(buttonWidth/2, BUTTON_HEIGHT), 0);
CGContextRestoreGState(context);
}


//// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);

UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


return output;
}

@end
1 change: 1 addition & 0 deletions TSPopover/TSPopoverController.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef NSUInteger TSPopoverArrowPosition;
- (id)initWithContentViewController:(UIViewController*)viewController;
- (id)initWithView:(UIView*)view;
- (void) showPopoverWithTouch:(UIEvent*)senderEvent;
- (void) showPopoverWithRect:(CGRect)senderRect;
- (void) view:(UIView*)view touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
- (void) dismissPopoverAnimatd:(BOOL)animated;

Expand Down
2 changes: 1 addition & 1 deletion TSPopover/TSPopoverController.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ - (void) showPopoverWithTouch:(UIEvent*)senderEvent
[self showPopoverWithPoint:senderPoint];
}

- (void) showPopoverWithFrame:(CGRect)senderFrame
- (void) showPopoverWithRect:(CGRect)senderRect
{

}
Expand Down
15 changes: 15 additions & 0 deletions TSPopover/UIBarButtonItem+WEPopover.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* UIBarButtonItem+WEPopover.h
* WEPopover
*
* Created by Werner Altewischer on 07/05/11.
* Copyright 2010 Werner IT Consultancy. All rights reserved.
*
*/

@interface UIBarButtonItem(WEPopover)

- (CGRect)frameInView:(UIView *)v;
- (UIView *)superview;

@end
Loading

0 comments on commit 12d1bd8

Please sign in to comment.