From 8b98c1fea9e6ee90f6ca6523e963689a315b99d5 Mon Sep 17 00:00:00 2001 From: sonson Date: Fri, 1 Jul 2011 11:30:17 +0900 Subject: [PATCH] Supported to show a popup view from UIBarButtonItem. --- PopupViewTest/PopupViewTestViewController.h | 2 + PopupViewTest/PopupViewTestViewController.m | 77 +++---- PopupViewTest/Resources/MainWindow.xib | 236 +++++++++----------- SNPopupView.h | 1 + SNPopupView.m | 39 ++++ 5 files changed, 173 insertions(+), 182 deletions(-) diff --git a/PopupViewTest/PopupViewTestViewController.h b/PopupViewTest/PopupViewTestViewController.h index 0b5067e..43333be 100644 --- a/PopupViewTest/PopupViewTestViewController.h +++ b/PopupViewTest/PopupViewTestViewController.h @@ -38,5 +38,7 @@ int currentMessageIndex; } +- (IBAction)pushButton:(id)sender; + @end diff --git a/PopupViewTest/PopupViewTestViewController.m b/PopupViewTest/PopupViewTestViewController.m index e685ede..7c69a32 100644 --- a/PopupViewTest/PopupViewTestViewController.m +++ b/PopupViewTest/PopupViewTestViewController.m @@ -34,24 +34,29 @@ @implementation PopupViewTestViewController -- (void)viewWillAppear:(BOOL)animated { - messages = [NSArray arrayWithObjects: - @"hoge", - @"hoge", - @"hoge", - @"hoge", - @"abcdaaaaaaaaaefghij", - @"abcdaaaaaaaaaefghij", - @"abcdaaaaaaaaaefghij", - @"abcdaaaaaaaaaefghij", - @"pict", - @"pict", - @"pict", - @"pict", - nil]; - [messages retain]; +- (IBAction)pushButton:(id)sender { + DNSLogMethod - currentMessageIndex = 0; + if (popup == nil) { + if (currentMessageIndex == 0) { + popup = [[SNPopupView alloc] initWithImage:[UIImage imageNamed:@"2tchSmall.png"]]; + currentMessageIndex = 1; + } + else { + popup = [[SNPopupView alloc] initWithString:@"test message" withFontOfSize:12]; + currentMessageIndex = 0; + } + [popup showFromBarButtonItem:sender inView:self.view]; + [popup addTarget:self action:@selector(didTouchPopupView:)]; + [popup release]; + } + else { + [popup dismiss]; + popup = nil; + } +} + +- (void)viewWillAppear:(BOOL)animated { } - (void)didTouchPopupView:(SNPopupView*)sender { @@ -62,42 +67,22 @@ - (void)didTouchPopupView:(SNPopupView*)sender { - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; - NSString *nextTitle = [messages objectAtIndex:currentMessageIndex++]; - - if (currentMessageIndex >= [messages count]) - currentMessageIndex = 0; - if (popup == nil) { - if ([nextTitle isEqualToString:@"pict"]) + if (currentMessageIndex == 0) { popup = [[SNPopupView alloc] initWithImage:[UIImage imageNamed:@"2tchSmall.png"]]; - else - popup = [[SNPopupView alloc] initWithString:nextTitle withFontOfSize:12]; + currentMessageIndex = 1; + } + else { + popup = [[SNPopupView alloc] initWithString:@"test message" withFontOfSize:12]; + currentMessageIndex = 0; + } [popup showAtPoint:[touch locationInView:self.view] inView:self.view]; [popup addTarget:self action:@selector(didTouchPopupView:)]; [popup release]; } else { - NSString *currentTitle = popup.title; - - if ([currentTitle isEqualToString:nextTitle]) { - [popup showAtPoint:[touch locationInView:self.view] inView:self.view]; - } - else if (currentTitle == nil && [nextTitle isEqualToString:@"pict"]) { - [popup showAtPoint:[touch locationInView:self.view] inView:self.view]; - } - else { - [popup dismiss]; - popup = nil; - - if ([nextTitle isEqualToString:@"pict"]) - popup = [[SNPopupView alloc] initWithImage:[UIImage imageNamed:@"2tchSmall.png"]]; - else - popup = [[SNPopupView alloc] initWithString:nextTitle withFontOfSize:12]; - - [popup showAtPoint:[touch locationInView:self.view] inView:self.view]; - [popup addTarget:self action:@selector(didTouchPopupView:)]; - [popup release]; - } + [popup dismiss]; + popup = nil; } } diff --git a/PopupViewTest/Resources/MainWindow.xib b/PopupViewTest/Resources/MainWindow.xib index 71e587e..ad97288 100644 --- a/PopupViewTest/Resources/MainWindow.xib +++ b/PopupViewTest/Resources/MainWindow.xib @@ -51,7 +51,6 @@ 292 {320, 480} - 1 @@ -63,7 +62,7 @@ IBCocoaTouchFramework YES - + @@ -72,7 +71,7 @@ IBCocoaTouchFramework NO - + 256 {0, 0} @@ -81,77 +80,62 @@ YES IBCocoaTouchFramework - + 256 {{0, 436}, {320, 44}} - NO YES 4 YES IBCocoaTouchFramework - - YES - - IBCocoaTouchFramework - - 5 - - - b1 - IBCocoaTouchFramework - 1 - - - - IBCocoaTouchFramework - - 5 - - - b2 - IBCocoaTouchFramework - - - - IBCocoaTouchFramework - - 5 - - YES - + YES - - - - - + + IBCocoaTouchFramework + 5 + + + b1 + IBCocoaTouchFramework + 1 + + + IBCocoaTouchFramework + 5 + + + b2 + IBCocoaTouchFramework + + + IBCocoaTouchFramework + 5 + - - + Root View Controller - + left IBCocoaTouchFramework 1 - + - + right IBCocoaTouchFramework 1 - + IBCocoaTouchFramework - + 1 @@ -187,10 +171,42 @@ viewController - + 20 + + + pushButton: + + + + 29 + + + + pushButton: + + + + 30 + + + + pushButton: + + + + 31 + + + + pushButton: + + + + 32 + @@ -225,83 +241,83 @@ 16 - + YES - - - + + + 17 - - + + 18 - + YES - - - - - - + + + + + + - + 19 - + YES - - + + - + 21 - - + + 22 - - + + 23 - - + + 24 - - + + 25 - - + + 26 - - + + 27 - - + + 28 - - + + @@ -360,61 +376,9 @@ - 28 - - - - YES - - PopupViewTestAppDelegate - NSObject - - YES - - YES - viewController - window - - - YES - UINavigationController - UIWindow - - - - YES - - YES - viewController - window - - - YES - - viewController - UINavigationController - - - window - UIWindow - - - - - IBProjectSource - ./Classes/PopupViewTestAppDelegate.h - - - - PopupViewTestViewController - UIViewController - - IBProjectSource - ./Classes/PopupViewTestViewController.h - - - + 32 + 0 IBCocoaTouchFramework diff --git a/SNPopupView.h b/SNPopupView.h index afde0e8..9532e58 100644 --- a/SNPopupView.h +++ b/SNPopupView.h @@ -66,6 +66,7 @@ typedef enum { - (id) initWithString:(NSString*)newValue withFontOfSize:(float)newFontSize; - (id)initWithString:(NSString*)newValue; - (id)initWithImage:(UIImage*)newImage; +- (void)showFromBarButtonItem:(UIBarButtonItem*)barButtonItem inView:(UIView*)inView; - (void)showAtPoint:(CGPoint)p inView:(UIView*)inView; - (void)dismiss; - (void)addTarget:(id)target action:(SEL)action; diff --git a/SNPopupView.m b/SNPopupView.m index ae51975..8935ee7 100644 --- a/SNPopupView.m +++ b/SNPopupView.m @@ -45,6 +45,9 @@ #define ALPHA 0.6 +#define BAR_BUTTON_ITEM_UPPER_MARGIN 10 +#define BAR_BUTTON_ITEM_BOTTOM_MARGIN 5 + @interface SNPopupView(Private) - (void)popup; @end @@ -127,6 +130,42 @@ - (void)addTarget:(id)newTarget action:(SEL)newAction { #pragma mark - #pragma mark Animation +- (void)showFromBarButtonItem:(UIBarButtonItem*)barButtonItem inView:(UIView*)inView { + + if(![barButtonItem respondsToSelector:@selector(view)]) { + // error + return; + } + + UIView *targetView = (UIView *)[barButtonItem performSelector:@selector(view)]; + UIView *targetSuperview = [targetView superview]; + + BOOL isOnNavigationBar = YES; + + if ([targetSuperview isKindOfClass:[UINavigationBar class]]) { + isOnNavigationBar = YES; + } + else if ([targetSuperview isKindOfClass:[UIToolbar class]]) { + isOnNavigationBar = NO; + } + else { + // error + return; + } + + CGRect rect = [targetSuperview convertRect:targetView.frame toView:inView]; + + CGPoint p; + p.x = rect.origin.x + (int)rect.size.width/2; + + if (isOnNavigationBar) + p.y = rect.origin.y + rect.size.height + BAR_BUTTON_ITEM_UPPER_MARGIN; + else + p.y = rect.origin.y - BAR_BUTTON_ITEM_BOTTOM_MARGIN; + + [self showAtPoint:p inView:inView]; +} + - (void)showAtPoint:(CGPoint)p inView:(UIView*)inView { if ((p.y - contentBounds.size.height - POPUP_ROOT_SIZE.height - 2 * CONTENT_OFFSET.height - SHADOW_OFFSET.height) < 0) { direction = SNPopupViewDown;