Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep frame integral #37

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
59 changes: 59 additions & 0 deletions Classes/Popover/ARCHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// ARC Helper
//
// Version 2.1
//
// Created by Nick Lockwood on 05/01/2012.
// Copyright 2012 Charcoal Design
//
// Distributed under the permissive zlib license
// Get the latest version from here:
//
// https://gist.github.com/1563325
//

#ifndef ah_retain
#if __has_feature(objc_arc)
#define ah_retain self
#define ah_dealloc self
#define release self
#define autorelease self
#else
#define ah_retain retain
#define ah_dealloc dealloc
#define __bridge
#endif
#endif

// Weak reference support

#import <Availability.h>
#if (!__has_feature(objc_arc)) || \
(defined __IPHONE_OS_VERSION_MIN_REQUIRED && \
__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0) || \
(defined __MAC_OS_X_VERSION_MIN_REQUIRED && \
__MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7)
#undef weak
#define weak unsafe_unretained
#undef __weak
#define __weak __unsafe_unretained
#endif

// Weak delegate support

#ifndef ah_weak
#import <Availability.h>
#if (__has_feature(objc_arc)) && \
((defined __IPHONE_OS_VERSION_MIN_REQUIRED && \
__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0) || \
(defined __MAC_OS_X_VERSION_MIN_REQUIRED && \
__MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_7))
#define ah_weak weak
#define __ah_weak __weak
#else
#define ah_weak unsafe_unretained
#define __ah_weak __unsafe_unretained
#endif
#endif

// ARC Helper ends
20 changes: 10 additions & 10 deletions Classes/Popover/WEPopoverContainerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ - (void)dealloc {
self.downArrowImageName = nil;
self.leftArrowImageName = nil;
self.rightArrowImageName = nil;
[super dealloc];
[super ah_dealloc];
}

@end
Expand Down Expand Up @@ -52,7 +52,7 @@ - (id)initWithSize:(CGSize)theSize
[self initFrame];
self.backgroundColor = [UIColor clearColor];
UIImage *theImage = [UIImage imageNamed:properties.bgImageName];
bgImage = [[theImage stretchableImageWithLeftCapWidth:properties.leftBgCapSize topCapHeight:properties.topBgCapSize] retain];
bgImage = [[theImage stretchableImageWithLeftCapWidth:properties.leftBgCapSize topCapHeight:properties.topBgCapSize] ah_retain];

self.clipsToBounds = YES;
self.userInteractionEnabled = YES;
Expand All @@ -65,7 +65,7 @@ - (void)dealloc {
[contentView release];
[bgImage release];
[arrowImage release];
[super dealloc];
[super ah_dealloc];
}

- (void)drawRect:(CGRect)rect {
Expand Down Expand Up @@ -103,7 +103,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- (void)setContentView:(UIView *)v {
if (v != contentView) {
[contentView release];
contentView = [v retain];
contentView = [v ah_retain];
contentView.frame = self.contentRect;
[self addSubview:contentView];
}
Expand All @@ -123,7 +123,7 @@ - (void)initFrame {
bgRect = CGRectOffset(bgRect, arrowOffset.x, arrowOffset.y);
arrowRect = CGRectOffset(arrowRect, arrowOffset.x, arrowOffset.y);

self.frame = theFrame;
self.frame = CGRectIntegral(theFrame);
}

- (CGSize)contentSize {
Expand All @@ -141,7 +141,7 @@ - (CGRect)contentRect {
- (void)setProperties:(WEPopoverContainerViewProperties *)props {
if (properties != props) {
[properties release];
properties = [props retain];
properties = [props ah_retain];
}
}

Expand Down Expand Up @@ -345,16 +345,16 @@ - (void)determineGeometryForSize:(CGSize)theSize anchorRect:(CGRect)anchorRect d

switch (arrowDirection) {
case UIPopoverArrowDirectionUp:
arrowImage = [upArrowImage retain];
arrowImage = [upArrowImage ah_retain];
break;
case UIPopoverArrowDirectionDown:
arrowImage = [downArrowImage retain];
arrowImage = [downArrowImage ah_retain];
break;
case UIPopoverArrowDirectionLeft:
arrowImage = [leftArrowImage retain];
arrowImage = [leftArrowImage ah_retain];
break;
case UIPopoverArrowDirectionRight:
arrowImage = [rightArrowImage retain];
arrowImage = [rightArrowImage ah_retain];
break;
}
}
Expand Down
6 changes: 4 additions & 2 deletions Classes/Popover/WEPopoverController.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

BOOL popoverVisible;
UIPopoverArrowDirection popoverArrowDirection;
id <WEPopoverControllerDelegate> delegate;
id <WEPopoverControllerDelegate> __ah_weak delegate;
CGSize popoverContentSize;
WEPopoverContainerViewProperties *containerViewProperties;
id <NSObject> context;
Expand All @@ -41,12 +41,14 @@
@property (nonatomic, readonly) UIView *view;
@property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible;
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;
@property (nonatomic, assign) id <WEPopoverControllerDelegate> delegate;
@property (nonatomic, ah_weak) id <WEPopoverControllerDelegate> delegate;
@property (nonatomic, assign) CGSize popoverContentSize;
@property (nonatomic, retain) WEPopoverContainerViewProperties *containerViewProperties;
@property (nonatomic, retain) id <NSObject> context;
@property (nonatomic, copy) NSArray *passthroughViews;

+ (void)setCustomKeyViewIndex:(NSUInteger)keyViewIndex;

- (id)initWithContentViewController:(UIViewController *)theContentViewController;

- (void)dismissPopoverAnimated:(BOOL)animated;
Expand Down
104 changes: 68 additions & 36 deletions Classes/Popover/WEPopoverController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
#import "WEPopoverParentView.h"
#import "UIBarButtonItem+WEPopover.h"

#define FADE_DURATION 0.25
#define FADE_DURATION 0.25
#define DIM_ALPHA 0.25

@interface WEPopoverController ()

@property (nonatomic, retain) UIView *dimView;

@end

@interface WEPopoverController(Private)

Expand All @@ -36,6 +43,13 @@ @implementation WEPopoverController
@synthesize context;
@synthesize passthroughViews;

static NSUInteger customKeyViewIndex;

+ (void)setCustomKeyViewIndex:(NSUInteger)keyViewIndex
{
customKeyViewIndex = keyViewIndex;
}

- (id)init {
if ((self = [super init])) {
}
Expand All @@ -55,13 +69,14 @@ - (void)dealloc {
[containerViewProperties release];
[passthroughViews release];
self.context = nil;
[super dealloc];
self.dimView = nil;
[super ah_dealloc];
}

- (void)setContentViewController:(UIViewController *)vc {
if (vc != contentViewController) {
[contentViewController release];
contentViewController = [vc retain];
contentViewController = [vc ah_retain];
popoverContentSize = CGSizeZero;
}
}
Expand All @@ -76,7 +91,7 @@ - (void)setPassthroughViews:(NSArray *)array {
[self updateBackgroundPassthroughViews];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)theContext {
- (void)fadeAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)theContext {

if ([animationID isEqual:@"FadeIn"]) {
self.view.userInteractionEnabled = YES;
Expand All @@ -91,7 +106,7 @@ - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished c
[backgroundView release];
backgroundView = nil;

BOOL userInitiatedDismissal = [(NSNumber *)theContext boolValue];
BOOL userInitiatedDismissal = [(__bridge NSNumber *)theContext boolValue];

if (userInitiatedDismissal) {
//Only send message to delegate in case the user initiated this event, which is if he touched outside the view
Expand Down Expand Up @@ -124,7 +139,7 @@ - (void)presentPopoverFromRect:(CGRect)rect
[self dismissPopoverAnimated:NO];

//First force a load view for the contentViewController so the popoverContentSize is properly initialized
contentViewController.view;
contentViewController.view = contentViewController.view;

if (CGSizeEqualToSize(popoverContentSize, CGSizeZero)) {
popoverContentSize = contentViewController.contentSizeForViewInPopover;
Expand Down Expand Up @@ -165,21 +180,30 @@ - (void)presentPopoverFromRect:(CGRect)rect
[contentViewController viewWillAppear:animated];

[self.view becomeFirstResponder];

[_dimView removeFromSuperview];
self.dimView = [[[UIView alloc] initWithFrame:keyView.bounds] autorelease];
_dimView.backgroundColor = [UIColor blackColor];
[keyView insertSubview:self.dimView belowSubview:backgroundView];

if (animated) {
self.view.alpha = 0.0;
_dimView.alpha = 0.0;

[UIView beginAnimations:@"FadeIn" context:nil];

[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationDidStopSelector:@selector(fadeAnimationDidStop:finished:context:)];
[UIView setAnimationDuration:FADE_DURATION];

self.view.alpha = 1.0;
_dimView.alpha = DIM_ALPHA;

[UIView commitAnimations];
} else {
popoverVisible = YES;
_dimView.alpha = DIM_ALPHA;

[contentViewController viewDidAppear:animated];
}
}
Expand Down Expand Up @@ -217,7 +241,7 @@ @implementation WEPopoverController(Private)
- (UIView *)keyView {
UIWindow *w = [[UIApplication sharedApplication] keyWindow];
if (w.subviews.count > 0) {
return [w.subviews objectAtIndex:0];
return [w.subviews objectAtIndex:customKeyViewIndex];
} else {
return w;
}
Expand All @@ -226,7 +250,7 @@ - (UIView *)keyView {
- (void)setView:(UIView *)v {
if (view != v) {
[view release];
view = [v retain];
view = [v ah_retain];
}
}

Expand All @@ -243,19 +267,22 @@ - (void)dismissPopoverAnimated:(BOOL)animated userInitiated:(BOOL)userInitiated
if (animated) {

self.view.userInteractionEnabled = NO;
[UIView beginAnimations:@"FadeOut" context:[NSNumber numberWithBool:userInitiated]];
[UIView beginAnimations:@"FadeOut" context:(__bridge void *)([NSNumber numberWithBool:userInitiated])];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationDidStopSelector:@selector(fadeAnimationDidStop:finished:context:)];

[UIView setAnimationDuration:FADE_DURATION];

self.view.alpha = 0.0;
_dimView.alpha = 0.0;

[UIView commitAnimations];
} else {
[contentViewController viewDidDisappear:animated];
[self.view removeFromSuperview];
self.view = nil;
[_dimView removeFromSuperview];
self.dimView = nil;
[backgroundView removeFromSuperview];
[backgroundView release];
backgroundView = nil;
Expand All @@ -275,31 +302,36 @@ - (CGRect)displayAreaForView:(UIView *)theView {

//Enable to use the simple popover style
- (WEPopoverContainerViewProperties *)defaultContainerViewProperties {
WEPopoverContainerViewProperties *ret = [[WEPopoverContainerViewProperties new] autorelease];

CGSize imageSize = CGSizeMake(30.0f, 30.0f);
NSString *bgImageName = @"popoverBgSimple.png";
CGFloat bgMargin = 6.0;
CGFloat contentMargin = 2.0;

ret.leftBgMargin = bgMargin;
ret.rightBgMargin = bgMargin;
ret.topBgMargin = bgMargin;
ret.bottomBgMargin = bgMargin;
ret.leftBgCapSize = imageSize.width/2;
ret.topBgCapSize = imageSize.height/2;
ret.bgImageName = bgImageName;
ret.leftContentMargin = contentMargin;
ret.rightContentMargin = contentMargin;
ret.topContentMargin = contentMargin;
ret.bottomContentMargin = contentMargin;
ret.arrowMargin = 1.0;

ret.upArrowImageName = @"popoverArrowUpSimple.png";
ret.downArrowImageName = @"popoverArrowDownSimple.png";
ret.leftArrowImageName = @"popoverArrowLeftSimple.png";
ret.rightArrowImageName = @"popoverArrowRightSimple.png";
return ret;
WEPopoverContainerViewProperties *ret = [[[WEPopoverContainerViewProperties alloc] init] autorelease];

NSString *bgImageName = nil;
CGFloat bgMargin = 0.0;
CGFloat bgCapSize = 0.0;
CGFloat contentMargin = 4.0;

bgImageName = @"popoverBg.png";

// These constants are determined by the popoverBg.png image file and are image dependent
bgMargin = 13; // margin width of 13 pixels on all sides popoverBg.png (62 pixels wide - 36 pixel background) / 2 == 26 / 2 == 13
bgCapSize = 31; // ImageSize/2 == 62 / 2 == 31 pixels

ret.leftBgMargin = bgMargin;
ret.rightBgMargin = bgMargin;
ret.topBgMargin = bgMargin;
ret.bottomBgMargin = bgMargin;
ret.leftBgCapSize = bgCapSize;
ret.topBgCapSize = bgCapSize;
ret.bgImageName = bgImageName;
ret.leftContentMargin = contentMargin;
ret.rightContentMargin = contentMargin - 1; // Need to shift one pixel for border to look correct
ret.topContentMargin = contentMargin - 1;
ret.bottomContentMargin = contentMargin;

ret.upArrowImageName = @"popoverArrowUp.png";
ret.downArrowImageName = @"popoverArrowDown.png";
ret.leftArrowImageName = @"popoverArrowLeft.png";
ret.rightArrowImageName = @"popoverArrowRight.png";
return ret;
}

@end
5 changes: 3 additions & 2 deletions Classes/Popover/WETouchableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <Foundation/Foundation.h>
#import "ARCHelper.h"

@class WETouchableView;

Expand All @@ -24,13 +25,13 @@
*/
@interface WETouchableView : UIView {
BOOL touchForwardingDisabled;
id <WETouchableViewDelegate> delegate;
id <WETouchableViewDelegate> __ah_weak delegate;
NSArray *passthroughViews;
BOOL testHits;
}

@property (nonatomic, assign) BOOL touchForwardingDisabled;
@property (nonatomic, assign) id <WETouchableViewDelegate> delegate;
@property (nonatomic, ah_weak) id <WETouchableViewDelegate> delegate;
@property (nonatomic, copy) NSArray *passthroughViews;

@end
Loading