From e57b3995e5209bc1deaf47de75c49288f15a2055 Mon Sep 17 00:00:00 2001 From: miromelo Date: Wed, 28 May 2014 21:04:31 +0800 Subject: [PATCH] add new initialize method - (id)initWithContentView:(UIView *)contentView --- WYPopoverController/WYPopoverController.h | 72 ++-- WYPopoverController/WYPopoverController.m | 393 ++++++++++++---------- 2 files changed, 252 insertions(+), 213 deletions(-) diff --git a/WYPopoverController/WYPopoverController.h b/WYPopoverController/WYPopoverController.h index 25f23381..1a923cf6 100644 --- a/WYPopoverController/WYPopoverController.h +++ b/WYPopoverController/WYPopoverController.h @@ -5,23 +5,23 @@ Copyright © 2013 Nicolas CHENG - 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 + 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 + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ + */ #import #import @@ -31,11 +31,11 @@ @class WYPopoverTheme; #ifndef WY_POPOVER_DEFAULT_ANIMATION_DURATION - #define WY_POPOVER_DEFAULT_ANIMATION_DURATION .25f +#define WY_POPOVER_DEFAULT_ANIMATION_DURATION .25f #endif #ifndef WY_POPOVER_MIN_SIZE - #define WY_POPOVER_MIN_SIZE CGSizeMake(240, 160) +#define WY_POPOVER_MIN_SIZE CGSizeMake(20, 20) #endif typedef NS_OPTIONS(NSUInteger, WYPopoverArrowDirection) { @@ -64,24 +64,24 @@ typedef NS_OPTIONS(NSUInteger, WYPopoverAnimationOptions) { @property (nonatomic, strong) UIColor *glossShadowColor UI_APPEARANCE_SELECTOR; @property (nonatomic, assign) CGSize glossShadowOffset UI_APPEARANCE_SELECTOR; -@property (nonatomic, assign) NSUInteger glossShadowBlurRadius UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) CGFloat glossShadowBlurRadius UI_APPEARANCE_SELECTOR; -@property (nonatomic, assign) NSUInteger borderWidth UI_APPEARANCE_SELECTOR; -@property (nonatomic, assign) NSUInteger arrowBase UI_APPEARANCE_SELECTOR; -@property (nonatomic, assign) NSUInteger arrowHeight UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) CGFloat borderWidth UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) CGFloat arrowBase UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) CGFloat arrowHeight UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *outerShadowColor UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *outerStrokeColor UI_APPEARANCE_SELECTOR; -@property (nonatomic, assign) NSUInteger outerShadowBlurRadius UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) CGFloat outerShadowBlurRadius UI_APPEARANCE_SELECTOR; @property (nonatomic, assign) CGSize outerShadowOffset UI_APPEARANCE_SELECTOR; -@property (nonatomic, assign) NSUInteger outerCornerRadius UI_APPEARANCE_SELECTOR; -@property (nonatomic, assign) NSUInteger minOuterCornerRadius UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) CGFloat outerCornerRadius UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) CGFloat minOuterCornerRadius UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *innerShadowColor UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *innerStrokeColor UI_APPEARANCE_SELECTOR; -@property (nonatomic, assign) NSUInteger innerShadowBlurRadius UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) CGFloat innerShadowBlurRadius UI_APPEARANCE_SELECTOR; @property (nonatomic, assign) CGSize innerShadowOffset UI_APPEARANCE_SELECTOR; -@property (nonatomic, assign) NSUInteger innerCornerRadius UI_APPEARANCE_SELECTOR; +@property (nonatomic, assign) CGFloat innerCornerRadius UI_APPEARANCE_SELECTOR; @property (nonatomic, assign) UIEdgeInsets viewContentInsets UI_APPEARANCE_SELECTOR; @@ -100,8 +100,9 @@ typedef NS_OPTIONS(NSUInteger, WYPopoverAnimationOptions) { @property (nonatomic, assign) UIEdgeInsets popoverLayoutMargins; @property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible; @property (nonatomic, strong, readonly) UIViewController *contentViewController; +@property (nonatomic, strong, readonly) UIView *popoverContentView; @property (nonatomic, assign) CGSize popoverContentSize; -@property (nonatomic, assign) float animationDuration; +@property (nonatomic, assign) CGFloat animationDuration; @property (nonatomic, strong) WYPopoverTheme *theme; @@ -112,6 +113,9 @@ typedef NS_OPTIONS(NSUInteger, WYPopoverAnimationOptions) { - (id)initWithContentViewController:(UIViewController *)viewController; +// custom +- (id)initWithContentView:(UIView *)contentView; + // theme - (void)beginThemeUpdates; @@ -210,7 +214,7 @@ typedef NS_OPTIONS(NSUInteger, WYPopoverAnimationOptions) { - (BOOL)popoverControllerShouldIgnoreKeyboardBounds:(WYPopoverController *)popoverController; -- (void)popoverController:(WYPopoverController *)popoverController willTranslatePopoverWithYOffset:(float *)value; +- (void)popoverController:(WYPopoverController *)popoverController willTranslatePopoverWithYOffset:(CGFloat *)value; @end @@ -224,24 +228,24 @@ typedef NS_OPTIONS(NSUInteger, WYPopoverAnimationOptions) { @property (nonatomic, strong) UIColor *glossShadowColor; @property (nonatomic, assign) CGSize glossShadowOffset; -@property (nonatomic, assign) NSUInteger glossShadowBlurRadius; +@property (nonatomic, assign) CGFloat glossShadowBlurRadius; -@property (nonatomic, assign) NSUInteger borderWidth; -@property (nonatomic, assign) NSUInteger arrowBase; -@property (nonatomic, assign) NSUInteger arrowHeight; +@property (nonatomic, assign) CGFloat borderWidth; +@property (nonatomic, assign) CGFloat arrowBase; +@property (nonatomic, assign) CGFloat arrowHeight; @property (nonatomic, strong) UIColor *outerShadowColor; @property (nonatomic, strong) UIColor *outerStrokeColor; -@property (nonatomic, assign) NSUInteger outerShadowBlurRadius; +@property (nonatomic, assign) CGFloat outerShadowBlurRadius; @property (nonatomic, assign) CGSize outerShadowOffset; -@property (nonatomic, assign) NSUInteger outerCornerRadius; -@property (nonatomic, assign) NSUInteger minOuterCornerRadius; +@property (nonatomic, assign) CGFloat outerCornerRadius; +@property (nonatomic, assign) CGFloat minOuterCornerRadius; @property (nonatomic, strong) UIColor *innerShadowColor; @property (nonatomic, strong) UIColor *innerStrokeColor; -@property (nonatomic, assign) NSUInteger innerShadowBlurRadius; +@property (nonatomic, assign) CGFloat innerShadowBlurRadius; @property (nonatomic, assign) CGSize innerShadowOffset; -@property (nonatomic, assign) NSUInteger innerCornerRadius; +@property (nonatomic, assign) CGFloat innerCornerRadius; @property (nonatomic, assign) UIEdgeInsets viewContentInsets; diff --git a/WYPopoverController/WYPopoverController.m b/WYPopoverController/WYPopoverController.m index 34ea4862..2aceff14 100644 --- a/WYPopoverController/WYPopoverController.m +++ b/WYPopoverController/WYPopoverController.m @@ -50,10 +50,10 @@ of this software and associated documentation files (the "Software"), to deal @interface UIColor (WYPopover) -- (BOOL)getValueOfRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)blue alpha:(CGFloat *)apha; +- (BOOL)getValueOfRed:(CGFloat*)red green:(CGFloat*)green blue:(CGFloat*)blue alpha:(CGFloat*)apha; - (NSString *)hexString; -- (UIColor *)colorByLighten:(float)d; -- (UIColor *)colorByDarken:(float)d; +- (UIColor *)colorByLighten:(CGFloat)d; +- (UIColor *)colorByDarken:(CGFloat)d; @end @@ -70,7 +70,7 @@ - (BOOL)getValueOfRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)blue CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace); CGColorSpaceRelease(colorSpace); - CGFloat rFloat = 0.0, gFloat = 0.0, bFloat = 0.0, aFloat = 0.0; + CGFloat rFloat = 0, gFloat = 0, bFloat = 0, aFloat = 0; BOOL result = NO; if (colorSpaceModel == kCGColorSpaceModelRGB) @@ -106,7 +106,7 @@ - (NSString *)hexString return [NSString stringWithFormat:@"#%02x%02x%02x%02x", r, g, b, a]; } -- (UIColor *)colorByLighten:(float)d +- (UIColor *)colorByLighten:(CGFloat)d { CGFloat rFloat, gFloat, bFloat, aFloat; [self getValueOfRed:&rFloat green:&gFloat blue:&bFloat alpha:&aFloat]; @@ -117,7 +117,7 @@ - (UIColor *)colorByLighten:(float)d alpha:1.0]; } -- (UIColor *)colorByDarken:(float)d +- (UIColor *)colorByDarken:(CGFloat)d { CGFloat rFloat, gFloat, bFloat, aFloat; [self getValueOfRed:&rFloat green:&gFloat blue:&bFloat alpha:&aFloat]; @@ -210,9 +210,7 @@ - (void)setContentSize:(CGSize)aContentSize #pragma clang diagnostic pop #ifdef WY_BASE_SDK_7_ENABLED - if ([self respondsToSelector:@selector(setPreferredContentSize:)]) { - [self setPreferredContentSize:aContentSize]; - } + [self setPreferredContentSize:aContentSize]; #endif } @@ -221,9 +219,7 @@ - (void)sizzled_pushViewController:(UIViewController *)aViewController animated: if (self.isEmbedInPopover) { #ifdef WY_BASE_SDK_7_ENABLED - if ([aViewController respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { - aViewController.edgesForExtendedLayout = UIRectEdgeNone; - } + aViewController.edgesForExtendedLayout = UIRectEdgeNone; #endif CGSize contentSize = [self contentSize:aViewController]; [self setContentSize:contentSize]; @@ -333,7 +329,7 @@ @interface WYPopoverArea : NSObject @property (nonatomic, assign) WYPopoverArrowDirection arrowDirection; @property (nonatomic, assign) CGSize areaSize; -@property (nonatomic, assign, readonly) float value; +@property (nonatomic, assign, readonly) CGFloat value; @end @@ -376,14 +372,14 @@ - (NSString*)description return [NSString stringWithFormat:@"%@ [ %f x %f ]", direction, areaSize.width, areaSize.height]; } -- (float)value +- (CGFloat)value { - float result = 0; + CGFloat result = 0; if (areaSize.width > 0 && areaSize.height > 0) { - float w1 = ceilf(areaSize.width / 10.0); - float h1 = ceilf(areaSize.height / 10.0); + CGFloat w1 = ceilf(areaSize.width / 10.0); + CGFloat h1 = ceilf(areaSize.height / 10.0); result = (w1 * h1); } @@ -507,9 +503,9 @@ + (id)themeForIOS7 { return result; } -- (NSUInteger)innerCornerRadius +- (CGFloat)innerCornerRadius { - float result = innerCornerRadius; + CGFloat result = innerCornerRadius; if (borderWidth == 0) { @@ -615,7 +611,7 @@ + (UIImage *)imageWithColor:(UIColor *)color; @implementation UIImage (WYPopover) -static float edgeSizeFromCornerRadius(float cornerRadius) { +static CGFloat edgeSizeFromCornerRadius(CGFloat cornerRadius) { return cornerRadius * 2 + 1; } @@ -625,9 +621,9 @@ + (UIImage *)imageWithColor:(UIColor *)color } + (UIImage *)imageWithColor:(UIColor *)color - cornerRadius:(float)cornerRadius + cornerRadius:(CGFloat)cornerRadius { - float min = edgeSizeFromCornerRadius(cornerRadius); + CGFloat min = edgeSizeFromCornerRadius(cornerRadius); CGSize minSize = CGSizeMake(min, min); @@ -636,7 +632,7 @@ + (UIImage *)imageWithColor:(UIColor *)color + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)aSize - cornerRadius:(float)cornerRadius + cornerRadius:(CGFloat)cornerRadius { CGRect rect = CGRectMake(0, 0, aSize.width, aSize.height); UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius]; @@ -661,17 +657,17 @@ @interface WYPopoverBackgroundInnerView : UIView @property (nonatomic, strong) UIColor *gradientTopColor; @property (nonatomic, strong) UIColor *gradientBottomColor; -@property (nonatomic, assign) float gradientHeight; -@property (nonatomic, assign) float gradientTopPosition; +@property (nonatomic, assign) CGFloat gradientHeight; +@property (nonatomic, assign) CGFloat gradientTopPosition; @property (nonatomic, strong) UIColor *innerShadowColor; @property (nonatomic, assign) CGSize innerShadowOffset; -@property (nonatomic, assign) float innerShadowBlurRadius; -@property (nonatomic, assign) float innerCornerRadius; +@property (nonatomic, assign) CGFloat innerShadowBlurRadius; +@property (nonatomic, assign) CGFloat innerCornerRadius; -@property (nonatomic, assign) float navigationBarHeight; +@property (nonatomic, assign) CGFloat navigationBarHeight; @property (nonatomic, assign) BOOL wantsDefaultContentAppearance; -@property (nonatomic, assign) float borderWidth; +@property (nonatomic, assign) CGFloat borderWidth; @end @@ -718,14 +714,12 @@ - (void)drawRect:(CGRect)rect NSArray* fillGradientColors = [NSArray arrayWithObjects: (id)gradientTopColor.CGColor, (id)gradientBottomColor.CGColor, nil]; - - CGFloat fillGradientLocations[2] = { 0, 1 }; - + CGFloat fillGradientLocations[] = {0, 1}; CGGradientRef fillGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)fillGradientColors, fillGradientLocations); //// innerRect Drawing - float barHeight = (wantsDefaultContentAppearance == NO) ? navigationBarHeight : 0; - float cornerRadius = (wantsDefaultContentAppearance == NO) ? innerCornerRadius : 0; + CGFloat barHeight = (wantsDefaultContentAppearance == NO) ? navigationBarHeight : 0; + CGFloat cornerRadius = (wantsDefaultContentAppearance == NO) ? innerCornerRadius : 0; CGRect innerRect = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect) + barHeight, CGRectGetWidth(rect) , CGRectGetHeight(rect) - barHeight); @@ -791,10 +785,11 @@ @protocol WYPopoverOverlayViewDelegate; @interface WYPopoverOverlayView : UIView { - BOOL testHits; + BOOL _isAccessible; } @property(nonatomic, assign) id delegate; +@property(nonatomic, assign) BOOL testHits; @property(nonatomic, unsafe_unretained) NSArray *passthroughViews; @end @@ -808,6 +803,7 @@ @interface WYPopoverOverlayView : UIView @protocol WYPopoverOverlayViewDelegate @optional +//- (void)popoverOverlayView:(WYPopoverOverlayView *)overlayView didTouchAtPoint:(CGPoint)point; - (void)popoverOverlayViewDidTouch:(WYPopoverOverlayView *)overlayView; @end @@ -819,8 +815,26 @@ - (void)popoverOverlayViewDidTouch:(WYPopoverOverlayView *)overlayView; @implementation WYPopoverOverlayView +- (id)initWithFrame:(CGRect)aFrame +{ + self = [super initWithFrame:aFrame]; + if (self) + { + self.autoresizesSubviews = NO; + self.accessibilityTraits = UIAccessibilityTraitNone; + } + return self; +} + - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + //UITouch *oneTouch = [touches anyObject]; + + //if ([self.delegate respondsToSelector:@selector(popoverOverlayView:didTouchAtPoint:)]) + //{ + // [self.delegate popoverOverlayView:self didTouchAtPoint:[oneTouch locationInView:self]]; + //} + if ([self.delegate respondsToSelector:@selector(popoverOverlayViewDidTouch:)]) { [self.delegate popoverOverlayViewDidTouch:self]; @@ -829,7 +843,8 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { - if (testHits) { + if (self.testHits) + { return NO; } @@ -837,9 +852,9 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event if (view == self) { - testHits = YES; + self.testHits = YES; UIView *superHitView = [self.superview hitTest:point withEvent:event]; - testHits = NO; + self.testHits = NO; if ([self isPassthroughView:superHitView]) { @@ -871,6 +886,10 @@ - (void)accessibilityElementDidBecomeFocused { self.accessibilityLabel = NSLocalizedString(@"Double-tap to dismiss pop-up window.", nil); } +- (void)accessibilityElementDidLoseFocus { + +} + @end //////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -898,15 +917,13 @@ @interface WYPopoverBackgroundView () @property (nonatomic, assign) WYPopoverArrowDirection arrowDirection; @property (nonatomic, strong, readonly) UIView *contentView; -@property (nonatomic, assign, readonly) float navigationBarHeight; +@property (nonatomic, assign, readonly) CGFloat navigationBarHeight; @property (nonatomic, assign, readonly) UIEdgeInsets outerShadowInsets; -@property (nonatomic, assign) float arrowOffset; +@property (nonatomic, assign) CGFloat arrowOffset; @property (nonatomic, assign) BOOL wantsDefaultContentAppearance; -@property (nonatomic, assign, getter = isAppearing) BOOL appearing; - - (void)setViewController:(UIViewController *)viewController; - +- (void)setPopOverContentView:(UIView *)view; - (CGRect)outerRect; - (CGRect)innerRect; - (CGRect)arrowRect; @@ -988,33 +1005,11 @@ - (id)initWithContentSize:(CGSize)aContentSize return self; } -/* -- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event -{ - BOOL result = [super pointInside:point withEvent:event]; - - if (self.isAppearing == NO) - { - BOOL isTouched = [self isTouchedAtPoint:point]; - - if (isTouched == NO && UIAccessibilityIsVoiceOverRunning()) - { - UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil); - UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, NSLocalizedString(@"Double-tap to dismiss pop-up window.", nil)); - } - } - - return result; -} -*/ - - - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *oneTouch = [touches anyObject]; - CGPoint point = [oneTouch locationInView:self]; - if ([self isTouchedAtPoint:point] == NO) + if ([self isTouchedAtPoint:[oneTouch locationInView:self]] == NO) { if ([self.delegate respondsToSelector:@selector(popoverBackgroundViewDidTouchOutside:)]) { @@ -1035,9 +1030,9 @@ - (UIEdgeInsets)outerShadowInsets return result; } -- (void)setArrowOffset:(float)value +- (void)setArrowOffset:(CGFloat)value { - float coef = 1; + CGFloat coef = 1; if (value != 0) { @@ -1047,7 +1042,7 @@ - (void)setArrowOffset:(float)value CGRect outerRect = [self outerRect]; - float delta = self.arrowBase / 2. + .5; + CGFloat delta = self.arrowBase / 2. + .5; delta += MIN(minOuterCornerRadius, outerCornerRadius); @@ -1083,20 +1078,25 @@ - (void)setArrowOffset:(float)value - (void)setViewController:(UIViewController *)viewController { - contentView = viewController.view; + navigationBarHeight = 0; + if (viewController != nil) { + if ([viewController isKindOfClass:[UINavigationController class]]) + { + UINavigationController* navigationController = (UINavigationController*)viewController; + navigationBarHeight = navigationController.navigationBar.bounds.size.height; + } + [self setPopOverContentView:viewController.view]; + } + +} + +-(void)setPopOverContentView:(UIView *)view{ + contentView = view; contentView.frame = CGRectIntegral(CGRectMake(0, 0, self.bounds.size.width, 100)); [self addSubview:contentView]; - navigationBarHeight = 0; - - if ([viewController isKindOfClass:[UINavigationController class]]) - { - UINavigationController* navigationController = (UINavigationController*)viewController; - navigationBarHeight = navigationController.navigationBar.bounds.size.height; - } - contentView.frame = CGRectIntegral([self innerRect]); if (innerView == nil) @@ -1124,6 +1124,7 @@ - (void)setViewController:(UIViewController *)viewController innerView.frame = CGRectIntegral(contentView.frame); [self.layer setNeedsDisplay]; + } - (CGSize)sizeThatFits:(CGSize)size @@ -1202,8 +1203,7 @@ - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context NSArray* fillGradientColors = [NSArray arrayWithObjects: (id)self.fillTopColor.CGColor, (id)self.fillBottomColor.CGColor, nil]; - - CGFloat fillGradientLocations[2] = {0, 1}; + CGFloat fillGradientLocations[] = {0, 1}; CGGradientRef fillGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)fillGradientColors, fillGradientLocations); // Frames @@ -1217,7 +1217,7 @@ - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context CGPoint origin = CGPointZero; - float reducedOuterCornerRadius = 0; + CGFloat reducedOuterCornerRadius = 0; if (arrowDirection == WYPopoverArrowDirectionUp || arrowDirection == WYPopoverArrowDirectionDown) { @@ -1358,8 +1358,8 @@ - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context CGContextSaveGState(context); { - float xOffset = glossShadowOffset.width + round(outerRectBorderRect.size.width); - float yOffset = glossShadowOffset.height; + CGFloat xOffset = glossShadowOffset.width + round(outerRectBorderRect.size.width); + CGFloat yOffset = glossShadowOffset.height; CGContextSetShadowWithColor(context, CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)), glossShadowBlurRadius, @@ -1540,6 +1540,7 @@ - (void)dealloc @interface WYPopoverController () { UIViewController *viewController; + UIView *popoverContentView; CGRect rect; UIView *inView; WYPopoverOverlayView *overlayView; @@ -1565,12 +1566,12 @@ - (void)dismissPopoverAnimated:(BOOL)aAnimated - (WYPopoverArrowDirection)arrowDirectionForRect:(CGRect)aRect inView:(UIView*)aView contentSize:(CGSize)aContentSize - arrowHeight:(float)aArrowHeight + arrowHeight:(CGFloat)aArrowHeight permittedArrowDirections:(WYPopoverArrowDirection)aArrowDirections; - (CGSize)sizeForRect:(CGRect)aRect inView:(UIView *)aView - arrowHeight:(float)aArrowHeight + arrowHeight:(CGFloat)aArrowHeight arrowDirection:(WYPopoverArrowDirection)aArrowDirection; - (void)registerTheme; @@ -1635,7 +1636,7 @@ + (WYPopoverTheme *)defaultTheme return defaultTheme_; } -+ (void)load ++ (void)initialize { [WYPopoverController setDefaultTheme:[WYPopoverTheme theme]]; } @@ -1679,7 +1680,7 @@ - (id)init theme.innerCornerRadius = appearance.innerCornerRadius; theme.viewContentInsets = appearance.viewContentInsets; theme.overlayColor = appearance.overlayColor; - + themeIsUpdating = NO; themeUpdatesEnabled = YES; @@ -1701,6 +1702,18 @@ - (id)initWithContentViewController:(UIViewController *)aViewController return self; } +-(id)initWithContentView:(UIView *)contentView +{ + self = [self init]; + + if (self) + { + popoverContentView = contentView; + } + + return self; +} + - (void)setTheme:(WYPopoverTheme *)value { [self unregisterTheme]; @@ -1794,6 +1807,10 @@ - (UIViewController *)contentViewController return viewController; } +-(UIView *)popoverContentView{ + return popoverContentView; +} + - (CGSize)topViewControllerContentSize { CGSize result = CGSizeZero; @@ -1906,33 +1923,19 @@ - (void)presentPopoverFromRect:(CGRect)aRect animated = aAnimated; options = aOptions; - if (!inView) - { - inView = [UIApplication sharedApplication].keyWindow.rootViewController.view; - if (CGRectIsEmpty(rect)) - { - rect = CGRectMake((int)inView.bounds.size.width / 2 - 5, (int)inView.bounds.size.height / 2 - 5, 10, 10); - } - } - CGSize contentViewSize = self.popoverContentSize; if (overlayView == nil) { overlayView = [[WYPopoverOverlayView alloc] initWithFrame:inView.window.bounds]; - overlayView.autoresizesSubviews = NO; - overlayView.isAccessibilityElement = YES; - overlayView.accessibilityTraits = UIAccessibilityTraitNone; overlayView.delegate = self; + overlayView.userInteractionEnabled = YES; overlayView.passthroughViews = passthroughViews; backgroundView = [[WYPopoverBackgroundView alloc] initWithContentSize:contentViewSize]; - backgroundView.appearing = YES; - backgroundView.isAccessibilityElement = YES; - backgroundView.accessibilityTraits = UIAccessibilityTraitNone; - backgroundView.delegate = self; backgroundView.hidden = YES; + backgroundView.accessibilityViewIsModal = YES; [inView.window addSubview:backgroundView]; [inView.window insertSubview:overlayView belowSubview:backgroundView]; @@ -1953,6 +1956,8 @@ - (void)presentPopoverFromRect:(CGRect)aRect [strongSelf->viewController viewDidAppear:YES]; } + strongSelf->backgroundView.accessibilityViewIsModal = NO; + if ([strongSelf->viewController respondsToSelector:@selector(preferredContentSize)]) { [strongSelf->viewController addObserver:self forKeyPath:NSStringFromSelector(@selector(preferredContentSize)) options:0 context:nil]; @@ -1961,8 +1966,6 @@ - (void)presentPopoverFromRect:(CGRect)aRect { [strongSelf->viewController addObserver:self forKeyPath:NSStringFromSelector(@selector(contentSizeForViewInPopover)) options:0 context:nil]; } - - strongSelf->backgroundView.appearing = NO; } if (completion) @@ -1977,17 +1980,15 @@ - (void)presentPopoverFromRect:(CGRect)aRect }; - void (^adjustTintDimmed)() = ^() { #ifdef WY_BASE_SDK_7_ENABLED - if ([inView.window respondsToSelector:@selector(setTintAdjustmentMode:)]) { - for (UIView *subview in inView.window.subviews) { - if (subview != backgroundView) { - [subview setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed]; - } + if ([inView.window respondsToSelector:@selector(setTintAdjustmentMode:)]) { + for (UIView *subview in inView.window.subviews) { + if (subview != backgroundView) { + [subview setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed]; } } + } #endif - }; backgroundView.hidden = NO; @@ -2018,14 +2019,12 @@ - (void)presentPopoverFromRect:(CGRect)aRect strongSelf->backgroundView.alpha = 1; strongSelf->backgroundView.transform = endTransform; } - adjustTintDimmed(); } completion:^(BOOL finished) { completionBlock(YES); }]; } else { - adjustTintDimmed(); [viewController viewWillAppear:NO]; completionBlock(NO); } @@ -2053,6 +2052,8 @@ - (void)presentPopoverFromRect:(CGRect)aRect selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } + + overlayView.isAccessibilityElement = YES; } - (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)aItem @@ -2145,7 +2146,7 @@ - (CGAffineTransform)transformForArrowDirection:(WYPopoverArrowDirection)arrowDi CGAffineTransform transform = backgroundView.transform; UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; - + CGSize containerViewSize = backgroundView.frame.size; if (backgroundView.arrowHeight > 0) @@ -2205,12 +2206,20 @@ - (void)setPopoverNavigationBarBackgroundImage [navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault]; } } + if (viewController) { + viewController.view.clipsToBounds = YES; + }else{ + popoverContentView.clipsToBounds = YES; + } - viewController.view.clipsToBounds = YES; if (backgroundView.borderWidth == 0) { - viewController.view.layer.cornerRadius = backgroundView.outerCornerRadius; + if (viewController) { + viewController.view.layer.cornerRadius = backgroundView.outerCornerRadius; + }else{ + popoverContentView.layer.cornerRadius = backgroundView.outerCornerRadius; + } } } @@ -2225,21 +2234,11 @@ - (void)positionPopover:(BOOL)aAnimated CGRect viewFrame; CGRect containerFrame = CGRectZero; - float minX, maxX, minY, maxY, offset = 0; + CGFloat minX, maxX, minY, maxY, offset = 0; CGSize containerViewSize = CGSizeZero; - float overlayWidth = UIInterfaceOrientationIsPortrait(orientation) ? overlayView.bounds.size.width : overlayView.bounds.size.height; - float overlayHeight = UIInterfaceOrientationIsPortrait(orientation) ? overlayView.bounds.size.height : overlayView.bounds.size.width; - - float keyboardHeight = UIInterfaceOrientationIsPortrait(orientation) ? keyboardRect.size.height : keyboardRect.size.width; - - if (delegate && [delegate respondsToSelector:@selector(popoverControllerShouldIgnoreKeyboardBounds:)]) { - BOOL shouldIgnore = [delegate popoverControllerShouldIgnoreKeyboardBounds:self]; - - if (shouldIgnore) { - keyboardHeight = 0; - } - } + CGFloat overlayWidth = UIInterfaceOrientationIsPortrait(orientation) ? overlayView.bounds.size.width : overlayView.bounds.size.height; + CGFloat overlayHeight = UIInterfaceOrientationIsPortrait(orientation) ? overlayView.bounds.size.height : overlayView.bounds.size.width; WYPopoverArrowDirection arrowDirection = permittedArrowDirections; @@ -2253,7 +2252,7 @@ - (void)positionPopover:(BOOL)aAnimated minX = popoverLayoutMargins.left; maxX = overlayWidth - popoverLayoutMargins.right; minY = WYStatusBarHeight() + popoverLayoutMargins.top; - maxY = overlayHeight - popoverLayoutMargins.bottom - keyboardHeight; + maxY = overlayHeight - popoverLayoutMargins.bottom; // Which direction ? // @@ -2265,11 +2264,20 @@ - (void)positionPopover:(BOOL)aAnimated // Position of the popover // + CGFloat keyboardHeight = UIInterfaceOrientationIsPortrait(orientation) ? keyboardRect.size.height : keyboardRect.size.width; + + if (delegate && [delegate respondsToSelector:@selector(popoverControllerShouldIgnoreKeyboardBounds:)]) { + BOOL shouldIgnore = [delegate popoverControllerShouldIgnoreKeyboardBounds:self]; + + if (shouldIgnore) { + keyboardHeight = 0; + } + } minX -= backgroundView.outerShadowInsets.left; maxX += backgroundView.outerShadowInsets.right; minY -= backgroundView.outerShadowInsets.top; - maxY += backgroundView.outerShadowInsets.bottom; + maxY += backgroundView.outerShadowInsets.bottom - keyboardHeight; if (arrowDirection == WYPopoverArrowDirectionDown) { @@ -2494,15 +2502,20 @@ - (void)positionPopover:(BOOL)aAnimated backgroundView.wantsDefaultContentAppearance = wantsDefaultContentAppearance; - [backgroundView setViewController:viewController]; + if (viewController) { + + [backgroundView setViewController:viewController]; + }else{ + [backgroundView setPopOverContentView:popoverContentView]; + } // keyboard support // if (keyboardHeight > 0) { - float keyboardY = UIInterfaceOrientationIsPortrait(orientation) ? keyboardRect.origin.y : keyboardRect.origin.x; + CGFloat keyboardY = UIInterfaceOrientationIsPortrait(orientation) ? keyboardRect.origin.y : keyboardRect.origin.x; - float yOffset = containerFrame.origin.y + containerFrame.size.height - keyboardY; + CGFloat yOffset = containerFrame.origin.y + containerFrame.size.height - keyboardY; if (yOffset > 0) { @@ -2526,7 +2539,7 @@ - (void)positionPopover:(BOOL)aAnimated containerFrame = backgroundView.frame; containerFrame.origin = WYPointRelativeToOrientation(containerOrigin, containerFrame.size, orientation); - + if (aAnimated == YES) { backgroundView.frame = savedContainerFrame; __weak __typeof__(self) weakSelf = self; @@ -2579,13 +2592,13 @@ - (void)dismissPopoverAnimated:(BOOL)aAnimated completion:(void (^)(void))completion callDelegate:(BOOL)callDelegate { - float duration = self.animationDuration; + CGFloat duration = self.animationDuration; WYPopoverAnimationOptions style = aOptions; __weak __typeof__(self) weakSelf = self; - - void (^adjustTintAutomatic)() = ^() { + void (^afterCompletionBlock)() = ^() { + #ifdef WY_BASE_SDK_7_ENABLED if ([inView.window respondsToSelector:@selector(setTintAdjustmentMode:)]) { for (UIView *subview in inView.window.subviews) { @@ -2595,15 +2608,11 @@ - (void)dismissPopoverAnimated:(BOOL)aAnimated } } #endif - }; - - void (^completionBlock)() = ^() { __typeof__(self) strongSelf = weakSelf; - if (strongSelf) { - [strongSelf->backgroundView removeFromSuperview]; - + if (strongSelf) + { strongSelf->backgroundView = nil; [strongSelf->overlayView removeFromSuperview]; @@ -2613,15 +2622,42 @@ - (void)dismissPopoverAnimated:(BOOL)aAnimated { [strongSelf->viewController viewDidDisappear:aAnimated]; } + + if (completion) + { + completion(); + } + else if (callDelegate) + { + if (strongSelf->delegate && [strongSelf->delegate respondsToSelector:@selector(popoverControllerDidDismissPopover:)]) + { + [strongSelf->delegate popoverControllerDidDismissPopover:strongSelf]; + } + } } + }; + + void (^completionBlock)() = ^() { - if (completion) + __typeof__(self) strongSelf = weakSelf; + [strongSelf->backgroundView removeFromSuperview]; + + if (aAnimated) { - completion(); + [UIView animateWithDuration:duration animations:^{ + __typeof__(self) strongSelf = weakSelf; + + if (strongSelf) + { + strongSelf->overlayView.alpha = 0; + } + } completion:^(BOOL finished) { + afterCompletionBlock(); + }]; } - else if (callDelegate && strongSelf && strongSelf->delegate && [strongSelf->delegate respondsToSelector:@selector(popoverControllerDidDismissPopover:)]) + else { - [strongSelf->delegate popoverControllerDidDismissPopover:strongSelf]; + afterCompletionBlock(); } }; @@ -2678,18 +2714,17 @@ - (void)dismissPopoverAnimated:(BOOL)aAnimated CGAffineTransform endTransform = [self transformForArrowDirection:strongSelf->backgroundView.arrowDirection]; strongSelf->backgroundView.transform = endTransform; } - strongSelf->overlayView.alpha = 0; } - adjustTintAutomatic(); } completion:^(BOOL finished) { completionBlock(); }]; } else { - adjustTintAutomatic(); completionBlock(); } + + overlayView.isAccessibilityElement = NO; } #pragma mark KVO @@ -2719,17 +2754,17 @@ - (void)popoverOverlayViewDidTouch:(WYPopoverOverlayView *)aOverlayView //if (!isTouched) //{ - BOOL shouldDismiss = !viewController.modalInPopover; - - if (shouldDismiss && delegate && [delegate respondsToSelector:@selector(popoverControllerShouldDismissPopover:)]) - { - shouldDismiss = [delegate popoverControllerShouldDismissPopover:self]; - } - - if (shouldDismiss) - { - [self dismissPopoverAnimated:animated options:options completion:nil callDelegate:YES]; - } + BOOL shouldDismiss = !viewController.modalInPopover; + + if (shouldDismiss && delegate && [delegate respondsToSelector:@selector(popoverControllerShouldDismissPopover:)]) + { + shouldDismiss = [delegate popoverControllerShouldDismissPopover:self]; + } + + if (shouldDismiss) + { + [self dismissPopoverAnimated:animated options:options completion:nil callDelegate:YES]; + } //} } @@ -2745,7 +2780,7 @@ - (void)popoverBackgroundViewDidTouchOutside:(WYPopoverBackgroundView *)aBackgro - (WYPopoverArrowDirection)arrowDirectionForRect:(CGRect)aRect inView:(UIView *)aView contentSize:(CGSize)contentSize - arrowHeight:(float)arrowHeight + arrowHeight:(CGFloat)arrowHeight permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections { WYPopoverArrowDirection arrowDirection = WYPopoverArrowDirectionUnknown; @@ -2810,8 +2845,8 @@ - (WYPopoverArrowDirection)arrowDirectionForRect:(CGRect)aRect WYPopoverArea *area1 = (WYPopoverArea *)obj1; WYPopoverArea *area2 = (WYPopoverArea *)obj2; - float val1 = area1.value; - float val2 = area2.value; + CGFloat val1 = area1.value; + CGFloat val2 = area2.value; NSComparisonResult result = NSOrderedSame; @@ -2870,7 +2905,7 @@ - (WYPopoverArrowDirection)arrowDirectionForRect:(CGRect)aRect - (CGSize)sizeForRect:(CGRect)aRect inView:(UIView *)aView - arrowHeight:(float)arrowHeight + arrowHeight:(CGFloat)arrowHeight arrowDirection:(WYPopoverArrowDirection)arrowDirection { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; @@ -2878,9 +2913,9 @@ - (CGSize)sizeForRect:(CGRect)aRect CGRect viewFrame = [aView convertRect:aRect toView:nil]; viewFrame = WYRectInWindowBounds(viewFrame, orientation); - float minX, maxX, minY, maxY = 0; + CGFloat minX, maxX, minY, maxY = 0; - float keyboardHeight = UIInterfaceOrientationIsPortrait(orientation) ? keyboardRect.size.height : keyboardRect.size.width; + CGFloat keyboardHeight = UIInterfaceOrientationIsPortrait(orientation) ? keyboardRect.size.height : keyboardRect.size.width; if (delegate && [delegate respondsToSelector:@selector(popoverControllerShouldIgnoreKeyboardBounds:)]) { BOOL shouldIgnore = [delegate popoverControllerShouldIgnoreKeyboardBounds:self]; @@ -2890,9 +2925,9 @@ - (CGSize)sizeForRect:(CGRect)aRect } } - float overlayWidth = UIInterfaceOrientationIsPortrait(orientation) ? overlayView.bounds.size.width : overlayView.bounds.size.height; + CGFloat overlayWidth = UIInterfaceOrientationIsPortrait(orientation) ? overlayView.bounds.size.width : overlayView.bounds.size.height; - float overlayHeight = UIInterfaceOrientationIsPortrait(orientation) ? overlayView.bounds.size.height : overlayView.bounds.size.width; + CGFloat overlayHeight = UIInterfaceOrientationIsPortrait(orientation) ? overlayView.bounds.size.height : overlayView.bounds.size.width; minX = popoverLayoutMargins.left; maxX = overlayWidth - popoverLayoutMargins.right; @@ -2959,10 +2994,10 @@ - (CGSize)sizeForRect:(CGRect)aRect return result; } -static float WYStatusBarHeight() { +static CGFloat WYStatusBarHeight() { UIInterfaceOrientation orienation = [[UIApplication sharedApplication] statusBarOrientation]; - float statusBarHeight = 0; + CGFloat statusBarHeight = 0; { CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; statusBarHeight = statusBarFrame.size.height; @@ -2976,9 +3011,9 @@ static float WYStatusBarHeight() { return statusBarHeight; } -static float WYInterfaceOrientationAngleOfOrientation(UIInterfaceOrientation orientation) +static CGFloat WYInterfaceOrientationAngleOfOrientation(UIInterfaceOrientation orientation) { - float angle; + CGFloat angle; switch (orientation) { @@ -3003,8 +3038,8 @@ static CGRect WYRectInWindowBounds(CGRect rect, UIInterfaceOrientation orientati UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; - float windowWidth = keyWindow.bounds.size.width; - float windowHeight = keyWindow.bounds.size.height; + CGFloat windowWidth = keyWindow.bounds.size.width; + CGFloat windowHeight = keyWindow.bounds.size.height; CGRect result = rect; @@ -3037,8 +3072,8 @@ static CGPoint WYPointRelativeToOrientation(CGPoint origin, CGSize size, UIInter UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; - float windowWidth = keyWindow.bounds.size.width; - float windowHeight = keyWindow.bounds.size.height; + CGFloat windowWidth = keyWindow.bounds.size.width; + CGFloat windowHeight = keyWindow.bounds.size.height; CGPoint result = origin;