Skip to content

Commit

Permalink
allow padding specification, remove NSLog
Browse files Browse the repository at this point in the history
  • Loading branch information
konistehrad committed Apr 8, 2018
1 parent cb83ff5 commit 2febfa1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
1 change: 1 addition & 0 deletions ios/SubtleVolume.h
Expand Up @@ -77,6 +77,7 @@ typedef NS_ENUM(NSInteger, SubtleVolumeAnimation) {
@property (nonatomic, strong) UIColor *barTintColor;
@property (nonatomic, assign) id <SubtleVolumeDelegate> delegate;
@property (nonatomic, assign) BOOL animatedByDefault;
@property (nonatomic, assign) CGFloat padding;

- (instancetype)initWithStyle:(SubtleVolumeStyle)style;
- (instancetype)initWithStyle:(SubtleVolumeStyle)style frame:(CGRect)frame;
Expand Down
36 changes: 12 additions & 24 deletions ios/SubtleVolume.mm
Expand Up @@ -79,7 +79,6 @@ - (void)setup {
}

volumeLevel = [[AVAudioSession sharedInstance] outputVolume];
// [self updateVolume:[[AVAudioSession sharedInstance] outputVolume] animated:NO];
[[AVAudioSession sharedInstance] addObserver:self forKeyPath:@"outputVolume" options:NSKeyValueObservingOptionNew context:NULL];
[volume setVolumeThumbImage:[[UIImage alloc] init] forState:UIControlStateNormal];
[volume setUserInteractionEnabled:NO];
Expand All @@ -94,15 +93,18 @@ - (void)setup {

- (void)layoutSubviews {
[super layoutSubviews];
overlay.frame = self.frame;
overlay.frame = CGRectMake(0, 0, self.frame.size.width*volumeLevel, self.frame.size.height);
overlay.frame = CGRectMake(
self.padding,
self.padding,
(self.frame.size.width - (self.padding*2)) * volumeLevel,
self.frame.size.height - (self.padding*2)
);

self.backgroundColor = self.barBackgroundColor;
overlay.backgroundColor = self.barTintColor;

}
- (void)updateVolume:(CGFloat)value animated:(BOOL)animated {
NSLog(@"updateVolume: value:%f animated:%@", value, animated?@"YES":@"NO");
[self.delegate subtleVolume:self willChange:value];
volumeLevel = value;
lastAnimated = animated;
Expand All @@ -119,27 +121,17 @@ - (void)updateVolume:(CGFloat)value animated:(BOOL)animated {
self.timer = nil;
}

NSLog(@"Spinning up timer with timeInterval 2 ...");
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerComplete) userInfo:nil repeats:NO];

[self doShow:animated];

[self.delegate subtleVolume:self didChange:value];
}

- (void)timerComplete {
NSLog(@"timerComplete!");
[self doHide:lastAnimated];
self.timer = nil;
}

- (void)doHide:(BOOL)animated {
NSLog(@"doHide:%@, runningShowAnimation: %@, runningHideAnimation: %@, showing: %@",
animated?@"YES":@"NO",
runningShowAnimation?@"YES":@"NO",
runningHideAnimation?@"YES":@"NO",
showing?@"YES":@"NO");

if(!showing) {
return;
}
Expand Down Expand Up @@ -169,12 +161,10 @@ - (void)doHide:(BOOL)animated {
break;
}
} completion:^(BOOL finished) {
NSLog(@"Hide animation complete; finished? %@", finished?@"YES":@"NO");
showing = NO;
runningHideAnimation = NO;
}];
} else {
NSLog(@"Hide immediate complete.");
showing = NO;
self.alpha = 0.0001;
if(self.animation == SubtleVolumeAnimationSlideDown) {
Expand All @@ -184,12 +174,6 @@ - (void)doHide:(BOOL)animated {
}

- (void)doShow:(BOOL)animated {
NSLog(@"doShow:%@, runningShowAnimation: %@, runningHideAnimation: %@, showing: %@",
animated?@"YES":@"NO",
runningShowAnimation?@"YES":@"NO",
runningHideAnimation?@"YES":@"NO",
showing?@"YES":@"NO");

if(showing) {
return;
}
Expand All @@ -203,6 +187,12 @@ - (void)doShow:(BOOL)animated {
}

if(animated) {
// set up for first run, assuming the animation has changed
// between instantiation and first showing
if(self.animation == SubtleVolumeAnimationSlideDown) {
self.transform = CGAffineTransformMakeTranslation(0, -self.frame.size.height);
}

runningShowAnimation = YES;
[UIView animateWithDuration:0.333 animations:^{
switch (self.animation) {
Expand All @@ -219,12 +209,10 @@ - (void)doShow:(BOOL)animated {
break;
}
} completion:^(BOOL finished) {
NSLog(@"Show animation complete; finished? %@", finished?@"YES":@"NO");
showing = YES;
runningShowAnimation = NO;
}];
} else {
NSLog(@"Show immediate complete.");
showing = YES;
self.alpha = 1;
self.transform = CGAffineTransformIdentity;
Expand Down
20 changes: 14 additions & 6 deletions ios/ViewController.mm
Expand Up @@ -136,10 +136,8 @@ -(id) init {
}

- (void)subtleVolume:(SubtleVolume *)volumeView willChange:(CGFloat)value {
// NSLog(@"%f alpha: %f", value, volumeView.alpha);
}
- (void)subtleVolume:(SubtleVolume *)volumeView didChange:(CGFloat)value {
// NSLog(@"END %f alpha: %f", value, volumeView.alpha);
}

- (void)viewDidLoad {
Expand Down Expand Up @@ -210,10 +208,20 @@ - (void)viewDidLoad {
}
#endif

volume = [[SubtleVolume alloc] initWithStyle:SubtleVolumeStylePlain frame:CGRectMake(10, 0, self.view.frame.size.width-20, 4)];
// volume.animatedByDefault = NO;
volume.barTintColor = [UIColor whiteColor];
volume.barBackgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.3];
CGFloat margin = 0;
CGFloat height = 16;
volume = [[SubtleVolume alloc]
initWithStyle:SubtleVolumeStylePlain
frame:CGRectMake(
margin, // X
0, // Y
self.view.frame.size.width-(margin*2), // width
height // height
)];

volume.padding = 7;
volume.barTintColor = [UIColor blackColor];
volume.barBackgroundColor = [UIColor whiteColor];
volume.animation = SubtleVolumeAnimationSlideDown;
volume.delegate = self;
[self.view addSubview:volume];
Expand Down

0 comments on commit 2febfa1

Please sign in to comment.