| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- //
- // MMPopupView.m
- // MMPopupView
- //
- // Created by Ralph Li on 9/6/15.
- // Copyright © 2015 LJC. All rights reserved.
- //
- #import "MMPopupView.h"
- #import "MMPopupWindow.h"
- #import "MMPopupDefine.h"
- #import "MMPopupCategory.h"
- #import <Masonry/Masonry.h>
- static NSString * const MMPopupViewHideAllNotification = @"MMPopupViewHideAllNotification";
- @implementation MMPopupView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
-
- if ( self )
- {
- [self setup];
- }
-
- return self;
- }
- - (void)setup
- {
- self.type = MMPopupTypeAlert;
- self.animationDuration = 0.3f;
- self.attachedView = [MMPopupWindow sharedWindow].attachView;
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyHideAll:) name:MMPopupViewHideAllNotification object:nil];
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self name:MMPopupViewHideAllNotification object:nil];
- }
- - (void)notifyHideAll:(NSNotification*)n
- {
- if ( [self isKindOfClass:n.object] )
- {
- [self hide];
- }
- }
- + (void)hideAll
- {
- [[NSNotificationCenter defaultCenter] postNotificationName:MMPopupViewHideAllNotification object:[self class]];
- }
- - (BOOL)visible
- {
- if ( self.attachedView )
- {
- return !self.attachedView.mm_dimBackgroundView.hidden;
- }
-
- return NO;
- }
- - (void)setType:(MMPopupType)type
- {
- _type = type;
-
- switch (type)
- {
- case MMPopupTypeAlert:
- {
- self.showAnimation = [self alertShowAnimation];
- self.hideAnimation = [self alertHideAnimation];
- break;
- }
- case MMPopupTypeSheet:
- {
- self.showAnimation = [self sheetShowAnimation];
- self.hideAnimation = [self sheetHideAnimation];
- break;
- }
- case MMPopupTypeCustom:
- {
- self.showAnimation = [self customShowAnimation];
- self.hideAnimation = [self customHideAnimation];
- break;
- }
-
- default:
- break;
- }
- }
- - (void)setAnimationDuration:(NSTimeInterval)animationDuration
- {
- _animationDuration = animationDuration;
-
- self.attachedView.mm_dimAnimationDuration = animationDuration;
- }
- - (void)show
- {
- [self showWithBlock:nil];
- }
- - (void)showWithBlock:(MMPopupCompletionBlock)block
- {
- if ( block )
- {
- self.showCompletionBlock = block;
- }
-
- if ( !self.attachedView )
- {
- self.attachedView = [MMPopupWindow sharedWindow].attachView;
- }
- [self.attachedView mm_showDimBackground];
-
- MMPopupBlock showAnimation = self.showAnimation;
-
- NSAssert(showAnimation, @"show animation must be there");
-
- showAnimation(self);
-
- if ( self.withKeyboard )
- {
- [self showKeyboard];
- }
- }
- - (void)hide
- {
- [self hideWithBlock:nil];
- }
- - (void)hideWithBlock:(MMPopupCompletionBlock)block
- {
- if ( block )
- {
- self.hideCompletionBlock = block;
- }
-
- if ( !self.attachedView )
- {
- self.attachedView = [MMPopupWindow sharedWindow].attachView;
- }
- [self.attachedView mm_hideDimBackground];
-
- if ( self.withKeyboard )
- {
- [self hideKeyboard];
- }
-
- MMPopupBlock hideAnimation = self.hideAnimation;
-
- NSAssert(hideAnimation, @"hide animation must be there");
-
- hideAnimation(self);
- }
- - (MMPopupBlock)alertShowAnimation
- {
- MMWeakify(self);
- MMPopupBlock block = ^(MMPopupView *popupView){
- MMStrongify(self);
-
- if ( !self.superview )
- {
- [self.attachedView.mm_dimBackgroundView addSubview:self];
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(self.attachedView).centerOffset(CGPointMake(0, self.withKeyboard?-216/2:0));
- }];
- [self layoutIfNeeded];
- }
-
- self.layer.transform = CATransform3DMakeScale(1.2f, 1.2f, 1.0f);
- self.alpha = 0.0f;
-
- [UIView animateWithDuration:self.animationDuration
- delay:0.0 options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
- animations:^{
-
- self.layer.transform = CATransform3DIdentity;
- self.alpha = 1.0f;
-
- } completion:^(BOOL finished) {
-
- if ( self.showCompletionBlock )
- {
- self.showCompletionBlock(self, finished);
- }
- }];
- };
-
- return block;
- }
- - (MMPopupBlock)alertHideAnimation
- {
- MMWeakify(self);
- MMPopupBlock block = ^(MMPopupView *popupView){
- MMStrongify(self);
-
- [UIView animateWithDuration:self.animationDuration
- delay:0
- options: UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
- animations:^{
-
- self.alpha = 0.0f;
-
- }
- completion:^(BOOL finished) {
-
- if ( finished )
- {
- [self removeFromSuperview];
- }
-
- if ( self.hideCompletionBlock )
- {
- self.hideCompletionBlock(self, finished);
- }
-
- }];
- };
-
- return block;
- }
- - (MMPopupBlock)sheetShowAnimation
- {
- MMWeakify(self);
- MMPopupBlock block = ^(MMPopupView *popupView){
- MMStrongify(self);
-
- if ( !self.superview )
- {
- [self.attachedView.mm_dimBackgroundView addSubview:self];
-
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.attachedView);
- make.bottom.equalTo(self.attachedView.mas_bottom).offset(self.attachedView.frame.size.height);
- }];
- [self layoutIfNeeded];
- }
-
- [UIView animateWithDuration:self.animationDuration
- delay:0
- options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
- animations:^{
-
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.attachedView.mas_bottom).offset(0);
- }];
-
- [self.superview layoutIfNeeded];
-
- }
- completion:^(BOOL finished) {
-
- if ( self.showCompletionBlock )
- {
- self.showCompletionBlock(self, finished);
- }
-
- }];
- };
-
- return block;
- }
- - (MMPopupBlock)sheetHideAnimation
- {
- MMWeakify(self);
- MMPopupBlock block = ^(MMPopupView *popupView){
- MMStrongify(self);
-
- [UIView animateWithDuration:self.animationDuration
- delay:0
- options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
- animations:^{
-
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.attachedView.mas_bottom).offset(self.attachedView.frame.size.height);
- }];
-
- [self.superview layoutIfNeeded];
-
- }
- completion:^(BOOL finished) {
-
- if ( finished )
- {
- [self removeFromSuperview];
- }
-
- if ( self.hideCompletionBlock )
- {
- self.hideCompletionBlock(self, finished);
- }
-
- }];
- };
-
- return block;
- }
- - (MMPopupBlock)customShowAnimation
- {
- MMWeakify(self);
- MMPopupBlock block = ^(MMPopupView *popupView){
- MMStrongify(self);
-
- if ( !self.superview )
- {
- [self.attachedView.mm_dimBackgroundView addSubview:self];
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(self.attachedView).centerOffset(CGPointMake(0, -self.attachedView.bounds.size.height));
- }];
- [self layoutIfNeeded];
- }
-
- [UIView animateWithDuration:self.animationDuration
- delay:0
- usingSpringWithDamping:0.8
- initialSpringVelocity:1.5
- options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
- animations:^{
-
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(self.attachedView).centerOffset(CGPointMake(0, self.withKeyboard?-216/2:0));
- }];
-
- [self.superview layoutIfNeeded];
-
- } completion:^(BOOL finished) {
-
- if ( self.showCompletionBlock )
- {
- self.showCompletionBlock(self, finished);
- }
-
- }];
- };
-
- return block;
- }
- - (MMPopupBlock)customHideAnimation
- {
- MMWeakify(self);
- MMPopupBlock block = ^(MMPopupView *popupView){
- MMStrongify(self);
-
- [UIView animateWithDuration:0.3
- delay:0
- options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
- animations:^{
-
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(self.attachedView).centerOffset(CGPointMake(0, self.attachedView.bounds.size.height));
- }];
-
- [self.superview layoutIfNeeded];
-
- } completion:^(BOOL finished) {
-
- if ( finished )
- {
- [self removeFromSuperview];
- }
-
- if ( self.hideCompletionBlock )
- {
- self.hideCompletionBlock(self, finished);
- }
- }];
- };
-
- return block;
- }
- - (void)showKeyboard
- {
-
- }
- - (void)hideKeyboard
- {
-
- }
- @end
|