Skip to content

Commit

Permalink
Fix rotation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakey committed Jul 5, 2016
1 parent 4990ef1 commit bd80c21
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 68 deletions.
2 changes: 2 additions & 0 deletions JKNotifier.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(SYSTEM_APPS_DIR)/Reveal.app/Contents/SharedSupport/iOS-Libraries",
"$(PROJECT_DIR)",
);
INFOPLIST_FILE = JKNotifier/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
Expand All @@ -294,6 +295,7 @@
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(SYSTEM_APPS_DIR)/Reveal.app/Contents/SharedSupport/iOS-Libraries",
"$(PROJECT_DIR)",
);
INFOPLIST_FILE = JKNotifier/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
Expand Down
14 changes: 11 additions & 3 deletions JKNotifier.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 71 additions & 17 deletions JKNotifier/JKNotifier/JKNotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,87 @@
#import "JKNotifierBar.h"
@interface JKNotifier : NSObject
+ (JKNotifier*)shareInstance;

+(JKNotifierBar*)showNotiferRemain:(NSString*)note;

+(JKNotifierBar*)showNotiferRemain:(NSString*)note
/**
* 显示一个Notifer,需要手动点击关闭
*
* @param note Notifer 内容
*
* @return JKNotifierBar
*/
+ (JKNotifierBar*)showNotiferRemain:(NSString*)note;
/**
* 显示一个Notifer,需要手动点击关闭
*
* @param note Notifer 内容
* @param appName Notifer 自定义APP名称 默认读取CFBundleName
*
* @return JKNotifierBar
*/
+ (JKNotifierBar*)showNotiferRemain:(NSString*)note
name:(NSString*)appName;


+(JKNotifierBar*)showNotifer:(NSString*)note;

/**
* 显示一个Notifer,2秒后自动关闭
*
* @param note Notifer 内容
*
* @return JKNotifierBar
*/
+ (JKNotifierBar*)showNotifer:(NSString*)note;
/**
* 显示一个Notifer,delay秒后自动关闭
*
* @param note Notifer 内容
* @param delay 自动关闭时间
*
* @return JKNotifierBar
*/
+ (JKNotifierBar*)showNotifer:(NSString *)note
dismissAfter:(NSTimeInterval)delay;

+(JKNotifierBar*)showNotifer:(NSString*)note
/**
* 显示一个Notifer,内容,名称,图标
*
* @param note Notifer 内容
* @param appName APP名称
* @param appIcon APP图标
*
* @return JKNotifierBar
*/
+ (JKNotifierBar*)showNotifer:(NSString*)note
name:(NSString*)appName
icon:(UIImage*)appIcon;

+(JKNotifierBar*)showNotifer:(NSString*)note
/**
* 显示一个Notifer,内容,名称,图标,关闭时间
*
* @param note Notifer 内容
* @param appName APP名称
* @param appIcon APP图标
* @param delay 自动关闭时间
*
* @return JKNotifierBar
*/
+ (JKNotifierBar*)showNotifer:(NSString*)note
name:(NSString*)appName
icon:(UIImage*)appIcon
dismissAfter:(NSTimeInterval)delay;

/**
* 关闭Notifer
*/
+ (void)dismiss;

/**
* 关闭Notifer
*/
- (void)dismiss;

/**
* 延迟关闭Notifer
*
* @param delay 延迟关闭时间
*/
+ (void)dismissAfter:(NSTimeInterval)delay;

+(void)handleClickAction:(JKNotifierBarClickBlock)notifierBarClickBlock;
/**
* 点击Notifer的回调
*
* @param notifierBarClickBlock 点击Notifer的回调
*/
+ (void)handleClickAction:(JKNotifierBarClickBlock)notifierBarClickBlock;

@end
66 changes: 47 additions & 19 deletions JKNotifier/JKNotifier/JKNotifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,54 +26,65 @@ + (JKNotifier*)shareInstance {
});
return notifier;
}
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifierOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

}
return self;
}
#pragma --mark getter
-(NSString*)appName{
- (NSString*)appName{
if (!_appName) {
_appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]?:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
}
return _appName;
}
-(UIImage*)defaultIcon{
- (UIImage*)defaultIcon{
if (!_defaultIcon) {

_defaultIcon = [self loadPlistIcon] ?:[UIImage imageNamed:@"AppIcon"] ?:[UIImage imageNamed:@"AppIcon-1"] ?:[UIImage imageNamed:@"AppIcon-2"] ?:[UIImage imageNamed:@"AppIcon-3"] ?:[UIImage imageNamed:@"JKNotifier_default_icon"];
}
return _defaultIcon;
}
-(JKNotifierBar*)notifierBar{
- (JKNotifierBar*)notifierBar{
if (!_notifierBar) {
_notifierBar = [[JKNotifierBar alloc] init];
_notifierBar.transform = CGAffineTransformMakeTranslation(0, -_notifierBar.frame.size.height);
CGRect frame = _notifierBar.frame;
frame.origin.y = -frame.size.height;
_notifierBar.frame = frame;
}
return _notifierBar;
}
+(void)handleClickAction:(JKNotifierBarClickBlock)notifierBarClickBlock{
+ (void)handleClickAction:(JKNotifierBarClickBlock)notifierBarClickBlock{
[[self shareInstance].notifierBar handleClickAction:notifierBarClickBlock];
}

#pragma --mark class method
+(JKNotifierBar*)showNotiferRemain:(NSString*)note{
+ (JKNotifierBar*)showNotiferRemain:(NSString*)note{
return [JKNotifier showNotiferRemain:note name:nil];
}

+(JKNotifierBar*)showNotiferRemain:(NSString*)note
+ (JKNotifierBar*)showNotiferRemain:(NSString*)note
name:(NSString*)appName{
return [JKNotifier showNotifer:note name:appName icon:nil dismissAfter:-1];
}

+(JKNotifierBar*)showNotifer:(NSString*)note{
+ (JKNotifierBar*)showNotifer:(NSString*)note{
return [JKNotifier showNotifer:note dismissAfter:2];
}

+(JKNotifierBar*)showNotifer:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{
+ (JKNotifierBar*)showNotifer:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{
return [JKNotifier showNotifer:note name:appName icon:appIcon dismissAfter:2];
}

+ (JKNotifierBar*)showNotifer:(NSString *)note
dismissAfter:(NSTimeInterval)delay{
return [self showNotifer:note name:nil icon:nil dismissAfter:delay];
}
+(JKNotifierBar*)showNotifer:(NSString*)note
+ (JKNotifierBar*)showNotifer:(NSString*)note
name:(NSString*)appName
icon:(UIImage*)appIcon
dismissAfter:(NSTimeInterval)delay{
Expand All @@ -98,36 +109,53 @@ + (void)dismissAfter:(NSTimeInterval)delay;

}
#pragma --instance method
-(JKNotifierBar*)showNotifer:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{
- (JKNotifierBar*)showNotifer:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{

[self.notifierBar.layer removeAllAnimations];
self.notifierBar.userInteractionEnabled = YES;
[self.notifierBar removeFromSuperview];
self.notifierBar = nil;

AudioServicesPlaySystemSound(1007);

[self.notifierBar show:note name:appName icon:appIcon];
[UIView animateWithDuration:(0.4) animations:^{
self.notifierBar.alpha = 1.0;
self.notifierBar.transform = CGAffineTransformIdentity;
CGRect frame = _notifierBar.frame;
frame.origin.y = 0.;
_notifierBar.frame = frame;
}];
return self.notifierBar;
}

- (void)dismiss
{
[self dismissWithAnimation:YES];
}
- (void)dismissWithAnimation:(BOOL)animated{
[[NSRunLoop currentRunLoop] cancelPerformSelector:@selector(dismiss) target:self argument:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(dismiss) object:nil];

[UIView animateWithDuration:0.4 animations:^{
self.notifierBar.transform = CGAffineTransformMakeTranslation(0, -self.notifierBar.frame.size.height);
} completion:^(BOOL finished) {
self.notifierBar.userInteractionEnabled = NO;
}];
if(animated){
[UIView animateWithDuration:0.4 animations:^{
CGRect frame = _notifierBar.frame;
frame.origin.y = -frame.size.height;
_notifierBar.frame = frame;
} completion:^(BOOL finished) {
self.notifierBar.userInteractionEnabled = NO;
_notifierBar.hidden = YES;
}];
}else{
_notifierBar.hidden = YES;
}
}
- (void)notifierOrientationChange:(NSNotification *)notification
{
[self dismissWithAnimation:NO];
}

#pragma --mark helper

-(UIImage*)loadPlistIcon{
- (UIImage*)loadPlistIcon{
NSString *iconString = @"Icon.png";
NSArray *icons = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIconFiles"];
if (!icons) {
Expand Down
4 changes: 2 additions & 2 deletions JKNotifier/JKNotifier/JKNotifierBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ typedef void(^JKNotifierBarClickBlock)(NSString *name,NSString *detail,JKNotifie
{
JKNotifierBarClickBlock _notifierBarClickBlock;
}
-(void)show:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon;
- (void)show:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon;

-(void)handleClickAction:(JKNotifierBarClickBlock)notifierBarClickBlock;
- (void)handleClickAction:(JKNotifierBarClickBlock)notifierBarClickBlock;
@end
10 changes: 5 additions & 5 deletions JKNotifier/JKNotifier/JKNotifierBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ - (void)buildWindow
[self addSubview:self.nameLabel];
[self addSubview:self.detailLabel];
}
-(UIFont *)font{
- (UIFont *)font{
if (!_font) {
_font =[UIFont systemFontOfSize:14.0];
}
return _font;
}
-(UIEdgeInsets)edge{
- (UIEdgeInsets)edge{
return UIEdgeInsetsMake(8.0, 50.0, 20.0, 5.0);
}
- (UIImageView *)iconView;
Expand Down Expand Up @@ -112,7 +112,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
}

-(void)handleClickAction:(JKNotifierBarClickBlock)notifierBarClickBlock{
- (void)handleClickAction:(JKNotifierBarClickBlock)notifierBarClickBlock{
_notifierBarClickBlock = [notifierBarClickBlock copy];
}

Expand All @@ -121,7 +121,7 @@ - (void)drawRect:(CGRect)rect{
[[UIColor lightGrayColor] setFill];
[round fill];
}
-(void)show:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{
- (void)show:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{
self.nameLabel.text = appName;
self.detailLabel.text = note;
self.iconView.image = appIcon;
Expand All @@ -140,7 +140,7 @@ -(void)show:(NSString*)note name:(NSString*)appName icon:(UIImage*)appIcon{
CGRectGetWidth(self.frame)-self.edge.left-self.edge.right,detailLabelHeight);

CGFloat selfHeight = MIN(CGRectGetHeight([UIScreen mainScreen].bounds), CGRectGetMaxY(self.detailLabel.frame)+self.edge.bottom);
self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), CGRectGetWidth(self.frame),selfHeight);
self.frame = CGRectMake(0,-selfHeight,CGRectGetWidth(self.frame),selfHeight);

[self setNeedsDisplay];
}
Expand Down
2 changes: 1 addition & 1 deletion JKNotifier/RootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ - (void)viewDidLoad {
// Do any additional setup after loading the view from its nib.
self.title = @"Some Demo of JKNotifier";
}

- (IBAction)showAutoHiddenTouched:(id)sender {
[JKNotifier showNotifer:[NSString stringWithFormat:@"亲,您收到了一条自动隐藏的消息哦!%@",[self random]]];

Expand Down
Loading

0 comments on commit bd80c21

Please sign in to comment.