Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pikacode committed Nov 27, 2017
1 parent ce80ab8 commit f9a121f
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 137 deletions.
2 changes: 1 addition & 1 deletion EBBannerView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "EBBannerView"
s.version = "1.0.3"
s.version = "1.0.4"
s.summary = "展示跟iOS9/10/11推送一样的横幅/提示音/振动,或自定义view。Show a banner the same with iOS9/10/11 nofitication(sound/vibrate), or customize."

# This description is used to generate tags and improve search results.
Expand Down
105 changes: 28 additions & 77 deletions EBBannerView/Classes/EBBannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ @interface EBBannerView(){
@property (nonatomic, assign)BOOL isExpand;
@property(nonatomic, assign, readonly)CGFloat standardHeight;
@property (nonatomic, assign, readonly)CGFloat calculatedHeight;
@property(nonatomic, assign)EBBannerViewStyle style;

@property (nonatomic, assign, readonly)CGFloat fixedX;
@property (nonatomic, assign, readonly)CGFloat fixedY;
@property (nonatomic, assign, readonly)CGFloat fixedWidth;

@property(nonatomic, strong)EBBannerViewMaker *maker;

@end

@implementation EBBannerView
Expand All @@ -55,14 +56,15 @@ +(void)sharedBannerViewInit{
});
}

+(EBBannerView*)bannerViewWithStyle:(EBBannerViewStyle)style{
+(instancetype)bannerWithBlock:(void(^)(EBBannerViewMaker *make))block{
[EBBannerView sharedBannerViewInit];
if (style < 9) {
style = 9;
}
EBBannerView *bannerView = sharedBannerViews[style-9];
bannerView.style = style;
if (style == EBBannerViewStyleiOS9) {
EBBannerViewMaker *maker = [EBBannerViewMaker new];
block(maker);
maker.style = MAX(maker.style, 9);

EBBannerView *bannerView = sharedBannerViews[maker.style-9];
bannerView.maker = maker;
if (maker.style == EBBannerViewStyleiOS9) {
bannerView.dateLabel.textColor = [[UIImage colorAtPoint:bannerView.dateLabel.center] colorWithAlphaComponent:0.7];
CGPoint lineCenter = bannerView.lineView.center;
bannerView.lineView.backgroundColor = [[UIImage colorAtPoint:CGPointMake(lineCenter.x, lineCenter.y - 7)] colorWithAlphaComponent:0.5];
Expand All @@ -75,9 +77,9 @@ -(void)show{
[_hideTimer invalidate];
_hideTimer = nil;
}
SystemSoundID soundID = self.soundID;
if (self.soundName) {
NSURL *url = [[NSBundle mainBundle] URLForResource:self.soundName withExtension:nil];
SystemSoundID soundID = _maker.soundID;
if (_maker.soundName) {
NSURL *url = [[NSBundle mainBundle] URLForResource:_maker.soundName withExtension:nil];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
}
[[EBMuteDetector sharedDetecotr] detectComplete:^(BOOL isMute) {
Expand All @@ -88,37 +90,35 @@ -(void)show{
}
}];

self.imageView.image = self.icon;
self.titleLabel.text = self.title;
self.dateLabel.text = self.date;
self.contentLabel.text = self.content;
self.lineView.hidden = (self.style == EBBannerViewStyleiOS9 && self.calculatedHeight < 34);
self.imageView.image = _maker.icon;
self.titleLabel.text = _maker.title;
self.dateLabel.text = _maker.date;
self.contentLabel.text = _maker.content;
self.lineView.hidden = (_maker.style == EBBannerViewStyleiOS9 && self.calculatedHeight < 34);

[sharedWindow.rootViewController.view addSubview:self];

self.frame = CGRectMake(self.fixedX, -self.standardHeight, self.fixedWidth, self.standardHeight);

WEAK_SELF(weakSelf);
[UIView animateWithDuration:self.animationDuration animations:^{
[UIView animateWithDuration:_maker.animationDuration animations:^{
weakSelf.frame = CGRectMake(weakSelf.fixedX, weakSelf.fixedY, weakSelf.fixedWidth, weakSelf.standardHeight);
} completion:^(BOOL finished) {
_hideTimer = [NSTimer scheduledTimerWithTimeInterval:weakSelf.stayDuration target:weakSelf selector:@selector(hide) userInfo:nil repeats:NO];
_hideTimer = [NSTimer scheduledTimerWithTimeInterval:weakSelf.maker.stayDuration target:weakSelf selector:@selector(hide) userInfo:nil repeats:NO];
}];
}

+(void)showWithContent:(NSString*)content{
EBBannerViewStyle style = [UIDevice currentDevice].systemVersion.intValue;
EBBannerView *bannerView = [EBBannerView bannerViewWithStyle:style];
bannerView.content = content;
bannerView.soundID = 1312;
[bannerView show];
[[EBBannerView bannerWithBlock:^(EBBannerViewMaker *make) {
make.content = content;
}] show];
}

#pragma mark - private

-(void)hide{
WEAK_SELF(weakSelf);
[UIView animateWithDuration:self.animationDuration animations:^{
[UIView animateWithDuration:_maker.animationDuration animations:^{
weakSelf.frame = CGRectMake(weakSelf.fixedX, -weakSelf.standardHeight, weakSelf.fixedWidth, weakSelf.standardHeight);
} completion:^(BOOL finished) {
[weakSelf removeFromSuperview];
Expand Down Expand Up @@ -146,7 +146,7 @@ -(void)addGestureRecognizer{
}

-(void)tapGesture:(UITapGestureRecognizer*)tapGesture{
[[NSNotificationCenter defaultCenter] postNotificationName:EBBannerViewDidClickNotification object:self.object];
[[NSNotificationCenter defaultCenter] postNotificationName:EBBannerViewDidClickNotification object:_maker.object];
[self hide];
}

Expand All @@ -157,12 +157,12 @@ -(void)swipeUpGesture:(UISwipeGestureRecognizer*)gesture{
}

-(void)swipeDownGesture:(UISwipeGestureRecognizer*)gesture{
if (self.style == EBBannerViewStyleiOS9) {
if (_maker.style == EBBannerViewStyleiOS9) {
if (gesture.direction == UISwipeGestureRecognizerDirectionDown && !self.lineView.hidden) {
self.isExpand = YES;
WEAK_SELF(weakSelf);
CGFloat originHeight = self.contentLabel.frame.size.height;
[UIView animateWithDuration:self.animationDuration animations:^{
[UIView animateWithDuration:_maker.animationDuration animations:^{
weakSelf.frame = CGRectMake(weakSelf.fixedX, weakSelf.fixedY, weakSelf.fixedWidth, weakSelf.standardHeight + weakSelf.calculatedHeight - originHeight + 1);
} completion:^(BOOL finished) {
weakSelf.frame = CGRectMake(weakSelf.fixedX, weakSelf.fixedY, weakSelf.fixedWidth, weakSelf.standardHeight + weakSelf.calculatedHeight - originHeight + 1);
Expand All @@ -173,51 +173,9 @@ -(void)swipeDownGesture:(UISwipeGestureRecognizer*)gesture{

#pragma mark - @property

-(UIImage *)icon{
if (!_icon) {
_icon = [EBBannerView defaultIcon];
}
return _icon;
}

-(NSString *)title{
if (!_title) {
_title = [EBBannerView defaultTitle];
}
return _title ?: @"";
}

-(NSString *)date{
if (!_date) {
_date = [EBBannerView defaultDate];
}
return _date;
}

-(NSString *)content{
if (!_content) {
_content = @"";
}
return _content;
}

-(NSTimeInterval)animationDuration{
if (!_animationDuration) {
_animationDuration = [EBBannerView defaultAnimationTime];
}
return _animationDuration;
}

-(NSTimeInterval)stayDuration{
if (!_stayDuration) {
_stayDuration = [EBBannerView defaultStayTime];;
}
return _stayDuration;
}

-(CGFloat)standardHeight{
CGFloat height;
switch (self.style) {
switch (_maker.style) {
case EBBannerViewStyleiOS9:
height = 70;
break;
Expand All @@ -241,13 +199,6 @@ -(CGFloat)calculatedHeight{
return calculatedHeight;
}

-(UInt32)soundID{
if (_soundID == 0) {
_soundID = [EBBannerView defaultSoundID];
}
return _soundID;
}

-(BOOL)isiPhoneX{
static BOOL isiPhoneX = NO;
static dispatch_once_t onceToken;
Expand Down
76 changes: 76 additions & 0 deletions EBBannerView/Classes/EBBannerViewMaker.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// EBBannerViewMaker.m
// EBBannerView
//
// Created by wxc on 2017/11/27.
//

#import "EBBannerView.h"

@implementation EBBannerViewMaker

-(EBBannerViewStyle)style{
if (!_style) {
_style = MAX(UIDevice.currentDevice.systemVersion.intValue, 9);
}
return _style;
}

-(UIImage *)icon{
if (!_icon) {
_icon = [UIImage imageNamed:@"AppIcon40x40"] ?: [UIImage imageNamed:@"AppIcon60x60"] ?: [UIImage imageNamed:@"AppIcon80x80"];
}
return _icon;
}

-(NSString *)title{
if (!_title) {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
_title = [infoDictionary objectForKey:@"CFBundleDisplayName"] ?: [infoDictionary objectForKey:@"CFBundleName"];
}
return _title ?: @"";
}

-(NSString *)date{
if (!_date) {
_date = NSLocalizedString(@"现在", nil);
}
return _date;
}

-(NSString *)content{
if (!_content) {
_content = @"";
}
return _content;
}

-(NSTimeInterval)animationDuration{
if (!_animationDuration) {
_animationDuration = 0.3;
}
return _animationDuration;
}

-(NSTimeInterval)stayDuration{
if (!_stayDuration) {
_stayDuration = 4;
}
return _stayDuration;
}

-(UInt32)soundID{
if (_soundID == 0) {
_soundID = 1312;
}
return _soundID;
}

-(id)object{
if (!_object) {
_object = self.content.copy;
}
return _object;
}

@end
12 changes: 8 additions & 4 deletions EBBannerView/EBBannerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>

@protocol EBCustomBannerViewProtocol;
@class EBCustomBannerView;
@class EBCustomBannerView, EBBannerViewMaker;

typedef enum : NSInteger {
EBBannerViewStyleiOS9 = 9,
Expand All @@ -23,21 +23,25 @@ typedef enum : NSInteger {
+(void)showWithContent:(NSString*)content;

//get a specific style banner, customize values below, then call 'show'
+(instancetype)bannerViewWithStyle:(EBBannerViewStyle)style;
+(instancetype)bannerWithBlock:(void(^)(EBBannerViewMaker *make))block;
-(void)show;

@end

@interface EBBannerViewMaker : NSObject

@property(nonatomic, assign)EBBannerViewStyle style;//default is UIDevice.currentDevice.systemVersion.intValue
@property(nonatomic, strong)UIImage *icon;//default is app icon
@property(nonatomic, strong)NSString *title;//default is app name
@property(nonatomic, strong)NSString *date;//default is "now" = "现在"
@property(nonatomic, strong)NSString *content;
@property(nonatomic, assign)NSTimeInterval animationDuration;//default is 0.3
@property(nonatomic, assign)NSTimeInterval stayDuration;//default is 4.0
@property(nonatomic, strong)id object;
@property(nonatomic, strong)id object;//default is content
@property(nonatomic, assign)UInt32 soundID;//default is 1312
@property(nonatomic, strong)NSString *soundName;

@end

//add observer for this notification to handle tap event and get the 'object' above
extern NSString *const EBBannerViewDidClickNotification;//监听点击弹窗的事件

36 changes: 16 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,21 @@ up to system version,will show iOS 9/10/11 style,auto show app icon/name.
#### 2.Customize all values with a iOS style
#### 2.Customize all values include iOS style
```objc
//1.init a banner with ios9/10/11 style
EBBannerView *banner = [EBBannerView bannerViewWithStyle:EBBannerViewStyleiOS9];
//2.set custom values, if not set will use default values
banner.content = @"custom content";
//banner.icon =
//banner.title =
//banner.date = @"2017-10-23";
//...
//1.create a banner, custom all values
EBBannerView *banner = [EBBannerView bannerWithBlock:^(EBBannerViewMaker *make) {
make.style = EBBannerViewStyleiOS9;//custom system, default is current
//make.style = 9;
make.content = @"MINE eye hath played the painter and hath stelled";
//make.object = ...
//make.icon = ...
//make.title = ...
//make.soundID = ...
}];
//3.show
//2.show
[banner show];
```

Expand All @@ -106,27 +107,20 @@ banner.content = @"custom content";

(if not set will use default values)

- `style`:the iOS style, default is up to system version, type enum : NSInteger {9/10/11}

- `style`:the iOS style, default is `UIDevice.currentDevice.systemVersion.intValue`, type enum : NSInteger {9/10/11}
- `icon`:the icon, default is app icon, type UIImage

- `title`:the title, default is app name, type NSString

- `date`:the date, default is localized string @"现在" = @"now", type NSString

- `content`:the content, type NSString

- `animationDuration`:the animation time(show/hide the banner), type NSTimeInterval

- `stayDuration`:how long the banner will stay before hide, type NSTimeInterval

- `object`:you can set it when create, then get it when clicked by adding an observer (see below), default is `content`,type id
- `soundID`:the sound will play when the banner is showing (when the mute is turn on iPhone will vibrate), type UInt32

- it's iOS system sound id, default push notification sound "Tritone" is 1312
- More sound id to see here [iOS Predefined sounds](http://iphonedevwiki.net/index.php/AudioServices#) or here [AudioServices sounds](http://www.cocoachina.com/bbs/read.php?tid=134344)

- You can download all the system sounds [UISounds.zip](/UISounds.zip) , listen and choose one which you perfer, then check out it's `id` with the form above

- `soundName`:play a cusome sound file, type NSString
- drag the file to Xcode proj
- pass the file name and extension,e.g. `banner.soundName = @"sound.mp3"`
Expand Down Expand Up @@ -197,6 +191,8 @@ EBCustomBannerView *customView = [EBCustomBannerView customView:view block:^(EBC





## Handle click event and pass value (system style)

- add an observer for `EBBannerViewDidClickNotification` and handle click event
Expand Down
Loading

0 comments on commit f9a121f

Please sign in to comment.