2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.5 KiB

  1. //
  2. // SCNightModeButton.m
  3. // SCCamera
  4. //
  5. // Created by Liu Liu on 3/19/15.
  6. // Copyright (c) 2015 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "SCNightModeButton.h"
  9. #import <SCFoundation/SCAssertWrapper.h>
  10. static NSTimeInterval const kSCNightModeButtonHiddenDelay = 2.5;
  11. @implementation SCNightModeButton {
  12. dispatch_block_t _delayedHideBlock;
  13. }
  14. - (instancetype)initWithFrame:(CGRect)frame
  15. {
  16. self = [super initWithFrame:frame];
  17. if (self) {
  18. self.image = [UIImage imageNamed:@"camera_nightmode_off_v10"];
  19. self.imageInset = CGSizeMake((CGRectGetWidth(self.bounds) - self.image.size.width) / 2,
  20. (CGRectGetHeight(self.bounds) - self.image.size.height) / 2);
  21. }
  22. return self;
  23. }
  24. - (void)setSelected:(BOOL)selected
  25. {
  26. SC_GUARD_ELSE_RETURN(_selected != selected);
  27. if (selected) {
  28. [self _cancelDelayedHideAnimation];
  29. self.image = [UIImage imageNamed:@"camera_nightmode_on_v10"];
  30. } else {
  31. self.image = [UIImage imageNamed:@"camera_nightmode_off_v10"];
  32. }
  33. self.imageInset = CGSizeMake((CGRectGetWidth(self.bounds) - self.image.size.width) / 2,
  34. (CGRectGetHeight(self.bounds) - self.image.size.height) / 2);
  35. _selected = selected;
  36. }
  37. - (void)show
  38. {
  39. SC_GUARD_ELSE_RETURN(self.hidden);
  40. SCAssertMainThread();
  41. [self _cancelDelayedHideAnimation];
  42. self.hidden = NO;
  43. [self animate];
  44. }
  45. - (void)hideWithDelay:(BOOL)delay
  46. {
  47. SC_GUARD_ELSE_RETURN(!self.hidden);
  48. SCAssertMainThread();
  49. [self _cancelDelayedHideAnimation];
  50. if (delay) {
  51. @weakify(self);
  52. _delayedHideBlock = dispatch_block_create(0, ^{
  53. @strongify(self);
  54. SC_GUARD_ELSE_RETURN(self);
  55. [UIView animateWithDuration:0.3
  56. animations:^{
  57. self.alpha = 0;
  58. }
  59. completion:^(BOOL finished) {
  60. self.alpha = 1;
  61. self.hidden = YES;
  62. _delayedHideBlock = nil;
  63. }];
  64. });
  65. dispatch_time_t delayTime =
  66. dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kSCNightModeButtonHiddenDelay * NSEC_PER_SEC));
  67. dispatch_after(delayTime, dispatch_get_main_queue(), _delayedHideBlock);
  68. } else {
  69. self.hidden = YES;
  70. }
  71. }
  72. - (BOOL)willHideAfterDelay
  73. {
  74. return _delayedHideBlock != nil;
  75. }
  76. #pragma mark - Private
  77. - (void)_cancelDelayedHideAnimation
  78. {
  79. SC_GUARD_ELSE_RETURN(_delayedHideBlock);
  80. dispatch_cancel(_delayedHideBlock);
  81. _delayedHideBlock = nil;
  82. }
  83. @end