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

Added protocol TSPopoverControllerDelegate, Fix typo dismissPopoverAnimated, fix show from window #8

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion Demo/TSPopoverDemo/MainViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//

#import <UIKit/UIKit.h>
#import "TSPopoverController.h"

@interface MainViewController : UIViewController
@interface MainViewController : UIViewController <TSPopoverControllerDelegate>

@end
8 changes: 8 additions & 0 deletions Demo/TSPopoverDemo/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ -(void)showPopover:(id)sender forEvent:(UIEvent*)event
popoverController.titleText = @"change order";
popoverController.popoverBaseColor = [UIColor lightGrayColor];
popoverController.popoverGradient= NO;
popoverController.delegate=self;
// popoverController.arrowPosition = TSPopoverArrowPositionHorizontal;
[popoverController showPopoverWithTouch:event];

Expand All @@ -136,4 +137,11 @@ -(void) showActionSheet:(id)sender forEvent:(UIEvent*)event
[actionSheet showWithTouch:event];
}

- (void)popoverControllerDidDismissPopover:(TSPopoverController *)popoverController{
NSLog(@"Delegate: Popover did dismiss");
}
- (BOOL)popoverControllerShouldDismissPopover:(TSPopoverController *)popoverController{
NSLog(@"Delegate: Popover should dismiss?");
return YES;
}
@end
4 changes: 2 additions & 2 deletions TSPopover/TSActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)anim

if (animated)
{
[popoverController dismissPopoverAnimatd:YES];
[popoverController dismissPopoverAnimated:YES];
[self removeFromSuperview];
}
else
{
[popoverController dismissPopoverAnimatd:NO];
[popoverController dismissPopoverAnimated:NO];
[self removeFromSuperview];
}
}
Expand Down
12 changes: 11 additions & 1 deletion TSPopover/TSPopoverController.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ enum {
};
typedef NSUInteger TSPopoverArrowPosition;

@class TSPopoverController;
@protocol TSPopoverControllerDelegate<NSObject>

- (void)popoverControllerDidDismissPopover:(TSPopoverController *)popoverController;
- (BOOL)popoverControllerShouldDismissPopover:(TSPopoverController *)popoverController;

@end

@class TSPopoverPopoverView;

@interface TSPopoverController : UIViewController <TSPopoverTouchesDelegate>
Expand All @@ -32,6 +40,7 @@ typedef NSUInteger TSPopoverArrowPosition;
TSPopoverArrowDirection arrowDirection;
CGRect screenRect;
int titleLabelheight;
id <TSPopoverControllerDelegate> delegate;
}

@property (strong, nonatomic) UIViewController *contentViewController;
Expand All @@ -43,13 +52,14 @@ typedef NSUInteger TSPopoverArrowPosition;
@property (nonatomic) int cornerRadius;
@property (nonatomic, readwrite) TSPopoverArrowPosition arrowPosition;
@property (nonatomic) BOOL popoverGradient;
@property (nonatomic) id <TSPopoverControllerDelegate> delegate;

- (id)initWithContentViewController:(UIViewController*)viewController;
- (id)initWithView:(UIView*)view;
- (void) showPopoverWithTouch:(UIEvent*)senderEvent;
- (void) showPopoverWithCell:(UITableViewCell*)senderCell;
- (void) showPopoverWithRect:(CGRect)senderRect;
- (void) view:(UIView*)view touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
- (void) dismissPopoverAnimatd:(BOOL)animated;
- (void) dismissPopoverAnimated:(BOOL)animated;

@end
25 changes: 18 additions & 7 deletions TSPopover/TSPopoverController.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ @implementation TSPopoverController
@synthesize arrowPosition = _arrowPosition;
@synthesize popoverBaseColor = _popoverBaseColor;
@synthesize popoverGradient = _popoverGradient;
@synthesize delegate;

- (id)init {
if ((self = [super init])) {
Expand Down Expand Up @@ -184,14 +185,21 @@ - (void) showPopoverWithPoint:(CGPoint)senderPoint
layer.shadowOpacity = 0.5;

[self.view addSubview:popoverView];

self.view.layer.shadowOffset = CGSizeMake(0, 1);
self.view.layer.shadowColor = [[UIColor colorWithRed:0.37 green:0.37 blue:0.37 alpha:1.0] CGColor];
self.view.layer.shadowRadius = 0.5;
self.view.layer.shadowOpacity = 0.8;

UIWindow *appWindow = [[UIApplication sharedApplication] keyWindow];
//[appWindow addSubview:self.view];

[appWindow.rootViewController.view addSubview:self.view];
if ([appWindow.rootViewController.view isFirstResponder]) {
[appWindow.rootViewController.view addSubview:self.view];
} else {
[appWindow addSubview:self.view];
}


[UIView animateWithDuration:0.0
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionAllowAnimatedContent
animations:^{
Expand All @@ -205,13 +213,14 @@ - (void) showPopoverWithPoint:(CGPoint)senderPoint

- (void)view:(UIView*)view touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
[self dismissPopoverAnimatd:YES];
[self dismissPopoverAnimated:YES];
}


- (void) dismissPopoverAnimatd:(BOOL)animated
- (void) dismissPopoverAnimated:(BOOL)animated
{
if (self.view) {

if (self.view && (!delegate || [delegate popoverControllerShouldDismissPopover:self]) ) {
if(animated) {
[UIView animateWithDuration:0.2
delay:0.0
Expand All @@ -227,6 +236,7 @@ - (void) dismissPopoverAnimatd:(BOOL)animated
self.titleText = nil;
self.titleColor = nil;
self.titleFont = nil;
[delegate popoverControllerDidDismissPopover:self];
}
];
}else{
Expand All @@ -237,6 +247,7 @@ - (void) dismissPopoverAnimatd:(BOOL)animated
self.titleText = nil;
self.titleColor = nil;
self.titleFont = nil;
[delegate popoverControllerDidDismissPopover:self];
}

}
Expand Down
4 changes: 2 additions & 2 deletions TSPopover/TSPopoverPopoverView.m
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ -(UIImage*)backgroundImage
//// Polygon Drawing
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
if(self.arrowPosition == TSPopoverArrowPositionVertical){
arrowFirst = senderLocationInViewPoint.x-ARROW_SIZE/2;
arrowLast = senderLocationInViewPoint.x+ARROW_SIZE/2;
arrowFirst = senderLocationInViewPoint.x-ARROW_SIZE/1.5;
arrowLast = senderLocationInViewPoint.x+ARROW_SIZE/1.5;
if(arrowFirst < bgRectPositionX + (self.cornerRadius+MARGIN)){
arrowFirst = bgRectPositionX + (self.cornerRadius+MARGIN);
arrowLast = arrowFirst + ARROW_SIZE;
Expand Down