Skip to content

Commit

Permalink
Add 3 styles of animation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaschengdev committed Nov 9, 2013
1 parent f405091 commit a808070
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 41 deletions.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ WYPopoverController is for the presentation of content in popover on iPhone / iP

* UIAppearance support
* Works like UIPopoverController
* Animation options
* Automatic orientation support
* UIStoryboard support
* Keyboard show / hide support
* iOS 6 & 7 support


### UIAppearance support

---
Expand Down Expand Up @@ -94,6 +94,27 @@ Determines whether the default content appearance should be used for the popover

The margins that define the portion of the screen in which it is permissible to display the popover.

### Animation options

---

Introduced in release 0.1.7, there are 3 styles of animation :
* Fade *(by default)*
* Scale
* Fade with Scale

#### Examples

```objective-c

popover = [[WYPopoverController alloc] initWithContentViewController:contentViewController];
[popover presentPopoverFromRect:btn.bounds
inView:btn
permittedArrowDirections:WYPopoverArrowDirectionAny
animated:YES
options:WYPopoverAnimationOptionFadeWithScale];
```
### ARC
---
Expand All @@ -108,13 +129,13 @@ WYPopoverController uses ARC.
#### Cocoapods
Add this line `pod 'WYPopoverController', '~> 0.1.5'` to your PodFile.
Add this line `pod 'WYPopoverController', '~> 0.1.7'` to your PodFile.
Your PodFile should look like :
```Ruby
platform :ios, '6.0'
pod 'WYPopoverController', '~> 0.1.5'
pod 'WYPopoverController', '~> 0.1.7'
```

To use the `master` branch of the repo :
Expand Down
24 changes: 22 additions & 2 deletions WYPopoverController/WYPopoverController.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Version 0.1.6
Version 0.1.7
WYPopoverController is available under the MIT license.
Expand Down Expand Up @@ -30,7 +30,7 @@
@protocol WYPopoverControllerDelegate;

#ifndef WY_POPOVER_DEFAULT_ANIMATION_DURATION
#define WY_POPOVER_DEFAULT_ANIMATION_DURATION 0.20f
#define WY_POPOVER_DEFAULT_ANIMATION_DURATION .20f
#endif

typedef NS_OPTIONS(NSUInteger, WYPopoverArrowDirection) {
Expand All @@ -43,6 +43,12 @@ typedef NS_OPTIONS(NSUInteger, WYPopoverArrowDirection) {
WYPopoverArrowDirectionUnknown = NSUIntegerMax
};

typedef NS_OPTIONS(NSUInteger, WYPopoverAnimationOptions) {
WYPopoverAnimationOptionFade = 1UL << 0, // default
WYPopoverAnimationOptionScale = 1UL << 1,
WYPopoverAnimationOptionFadeWithScale = WYPopoverAnimationOptionFade | WYPopoverAnimationOptionScale
};

////////////////////////////////////////////////////////////////////////////////////////////////////////

@interface WYPopoverBackgroundView : UIView
Expand Down Expand Up @@ -106,6 +112,20 @@ typedef NS_OPTIONS(NSUInteger, WYPopoverArrowDirection) {

- (void)presentPopoverAsDialogAnimated:(BOOL)animated;

- (void)presentPopoverFromRect:(CGRect)rect
inView:(UIView *)view
permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections
animated:(BOOL)animated
options:(WYPopoverAnimationOptions)options;

- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item
permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections
animated:(BOOL)animated
options:(WYPopoverAnimationOptions)options;

- (void)presentPopoverAsDialogAnimated:(BOOL)animated
options:(WYPopoverAnimationOptions)options;

- (void)dismissPopoverAnimated:(BOOL)animated;

@end
Expand Down
137 changes: 115 additions & 22 deletions WYPopoverController/WYPopoverController.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Version 0.1.6
Version 0.1.7
WYPopoverController is available under the MIT license.
Expand Down Expand Up @@ -1339,6 +1339,8 @@ @interface WYPopoverController () <WYPopoverOverlayViewDelegate>
__weak UIBarButtonItem *barButtonItem;
CGRect keyboardRect;
BOOL hasAppearanceProxyAvailable;

WYPopoverAnimationOptions options;
}

@property (nonatomic, strong, readonly) UIView *rootView;
Expand Down Expand Up @@ -1403,12 +1405,14 @@ - (UIView *)rootView

if (result.subviews.count > 0)
{
for (UIView *view in result.subviews) {
if(!view.isHidden){
return view;
}
}
// result = [result.subviews lastObject];
for (UIView *view in result.subviews)
{
if(!view.isHidden)
{
return view;
}
}
// result = [result.subviews lastObject];
}

return result;
Expand Down Expand Up @@ -1492,14 +1496,47 @@ - (CGSize)contentSizeForViewInPopover
return result;
}

- (void)presentPopoverFromRect:(CGRect)aRect inView:(UIView *)aView permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections animated:(BOOL)aAnimated
- (void)presentPopoverFromRect:(CGRect)aRect
inView:(UIView *)aView
permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections
animated:(BOOL)aAnimated
{
[self presentPopoverFromRect:aRect
inView:aView
permittedArrowDirections:arrowDirections
animated:aAnimated
options:WYPopoverAnimationOptionFade];
}

- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item
permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections
animated:(BOOL)aAnimated
{
[self presentPopoverFromBarButtonItem:item
permittedArrowDirections:arrowDirections
animated:aAnimated
options:WYPopoverAnimationOptionFade];
}

- (void)presentPopoverAsDialogAnimated:(BOOL)aAnimated
{
[self presentPopoverAsDialogAnimated:aAnimated
options:WYPopoverAnimationOptionFade];
}

- (void)presentPopoverFromRect:(CGRect)aRect
inView:(UIView *)aView
permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections
animated:(BOOL)aAnimated
options:(WYPopoverAnimationOptions)aOptions
{
NSAssert((arrowDirections != WYPopoverArrowDirectionUnknown), @"WYPopoverArrowDirection must not be UNKNOWN");

rect = aRect;
inView = aView;
permittedArrowDirections = arrowDirections;
animated = aAnimated;
options = aOptions;

CGSize contentViewSize = self.contentSizeForViewInPopover;

Expand Down Expand Up @@ -1567,11 +1604,23 @@ - (void)presentPopoverFromRect:(CGRect)aRect inView:(UIView *)aView permittedArr

if (animated)
{
containerView.alpha = 0;
if ((options & WYPopoverAnimationOptionFade) == WYPopoverAnimationOptionFade)
{
containerView.alpha = 0;
}

[viewController viewWillAppear:YES];

if ((options & WYPopoverAnimationOptionScale) == WYPopoverAnimationOptionScale)
{
CGAffineTransform transform = [self transformTranslateForArrowDirection:containerView.arrowDirection];
transform = CGAffineTransformScale(transform, 0, 0);
containerView.transform = transform;
}

[UIView animateWithDuration:WY_POPOVER_DEFAULT_ANIMATION_DURATION animations:^{
containerView.alpha = 1;
containerView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
if ([viewController isKindOfClass:[UINavigationController class]] == NO)
{
Expand Down Expand Up @@ -1614,6 +1663,63 @@ - (void)presentPopoverFromRect:(CGRect)aRect inView:(UIView *)aView permittedArr
}
}

- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item
permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections
animated:(BOOL)aAnimated
options:(WYPopoverAnimationOptions)aOptions
{
barButtonItem = item;
UIView *itemView = [barButtonItem valueForKey:@"view"];
arrowDirections = WYPopoverArrowDirectionDown | WYPopoverArrowDirectionUp;
[self presentPopoverFromRect:itemView.bounds
inView:itemView
permittedArrowDirections:arrowDirections
animated:aAnimated
options:aOptions];
}

- (void)presentPopoverAsDialogAnimated:(BOOL)aAnimated
options:(WYPopoverAnimationOptions)aOptions
{
[self presentPopoverFromRect:CGRectZero
inView:nil
permittedArrowDirections:WYPopoverArrowDirectionNone
animated:aAnimated
options:aOptions];
}

- (CGAffineTransform)transformTranslateForArrowDirection:(WYPopoverArrowDirection)arrowDirection
{
CGAffineTransform transform = CGAffineTransformIdentity;

if (containerView.arrowHeight > 0)
{
CGSize containerViewSize = containerView.frame.size;

if (arrowDirection == WYPopoverArrowDirectionDown)
{
transform = CGAffineTransformTranslate(CGAffineTransformIdentity, containerView.arrowOffset, containerViewSize.height / 2);
}

if (arrowDirection == WYPopoverArrowDirectionUp)
{
transform = CGAffineTransformTranslate(CGAffineTransformIdentity, containerView.arrowOffset, -containerViewSize.height / 2);
}

if (arrowDirection == WYPopoverArrowDirectionRight)
{
transform = CGAffineTransformTranslate(CGAffineTransformIdentity, containerView.frame.size.width / 2, containerView.arrowOffset);
}

if (arrowDirection == WYPopoverArrowDirectionLeft)
{
transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -containerView.frame.size.width / 2, containerView.arrowOffset);
}
}

return transform;
}

- (void)setPopoverNavigationBarBackgroundImage
{
if (wantsDefaultContentAppearance == NO && [viewController isKindOfClass:[UINavigationController class]])
Expand Down Expand Up @@ -1644,19 +1750,6 @@ - (void)setPopoverNavigationBarBackgroundImage
}
}

- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections animated:(BOOL)aAnimated
{
barButtonItem = item;
UIView *itemView = [barButtonItem valueForKey:@"view"];
arrowDirections = WYPopoverArrowDirectionDown | WYPopoverArrowDirectionUp;
[self presentPopoverFromRect:itemView.bounds inView:itemView permittedArrowDirections:arrowDirections animated:aAnimated];
}

- (void)presentPopoverAsDialogAnimated:(BOOL)aAnimated
{
[self presentPopoverFromRect:CGRectZero inView:nil permittedArrowDirections:WYPopoverArrowDirectionNone animated:aAnimated];
}

- (void)positionPopover
{
CGSize contentViewSize = self.contentSizeForViewInPopover;
Expand Down
11 changes: 9 additions & 2 deletions WYPopoverController/WYStoryboardPopoverSegue.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Version 0.1.6
Version 0.1.7
WYPopoverController is available under the MIT license.
Expand Down Expand Up @@ -28,6 +28,13 @@

@interface WYStoryboardPopoverSegue : UIStoryboardSegue

- (WYPopoverController*)popoverControllerWithSender:(id)sender permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
- (WYPopoverController*)popoverControllerWithSender:(id)sender
permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections
animated:(BOOL)animated;

- (WYPopoverController*)popoverControllerWithSender:(id)sender
permittedArrowDirections:(WYPopoverArrowDirection)arrowDirections
animated:(BOOL)animated
options:(WYPopoverAnimationOptions)options;

@end
34 changes: 28 additions & 6 deletions WYPopoverController/WYStoryboardPopoverSegue.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Version 0.1.6
Version 0.1.7
WYPopoverController is available under the MIT license.
Expand Down Expand Up @@ -30,9 +30,10 @@ of this software and associated documentation files (the "Software"), to deal

@interface WYStoryboardPopoverSegue()
{
WYPopoverController* popoverController;
WYPopoverController *popoverController;
id sender;
WYPopoverArrowDirection arrowDirections;
WYPopoverAnimationOptions options;
BOOL animated;
}

Expand All @@ -46,20 +47,41 @@ - (void)perform
{
if ([sender isKindOfClass:[UIBarButtonItem class]])
{
[popoverController presentPopoverFromBarButtonItem:(UIBarButtonItem*)sender permittedArrowDirections:arrowDirections animated:animated];
[popoverController presentPopoverFromBarButtonItem:(UIBarButtonItem*)sender
permittedArrowDirections:arrowDirections
animated:animated
options:options];
}
else if ([sender isKindOfClass:[UIView class]])
{
UIView* view = (UIView*)sender;
[popoverController presentPopoverFromRect:view.bounds inView:view permittedArrowDirections:arrowDirections animated:animated];
UIView *view = (UIView *)sender;
[popoverController presentPopoverFromRect:view.bounds
inView:view
permittedArrowDirections:arrowDirections
animated:animated
options:options];
}
}

- (WYPopoverController*)popoverControllerWithSender:(id)aSender permittedArrowDirections:(WYPopoverArrowDirection)aArrowDirections animated:(BOOL)aAnimated
- (WYPopoverController *)popoverControllerWithSender:(id)aSender
permittedArrowDirections:(WYPopoverArrowDirection)aArrowDirections
animated:(BOOL)aAnimated
{
return [self popoverControllerWithSender:aSender
permittedArrowDirections:aArrowDirections
animated:aAnimated
options:WYPopoverAnimationOptionFade];
}

- (WYPopoverController *)popoverControllerWithSender:(id)aSender
permittedArrowDirections:(WYPopoverArrowDirection)aArrowDirections
animated:(BOOL)aAnimated
options:(WYPopoverAnimationOptions)aOptions
{
sender = aSender;
arrowDirections = aArrowDirections;
animated = aAnimated;
options = aOptions;

popoverController = [[WYPopoverController alloc] initWithContentViewController:self.destinationViewController];

Expand Down
Loading

0 comments on commit a808070

Please sign in to comment.