forked from shaojiankui/JKCategories
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakey
committed
Jun 3, 2016
1 parent
6ec1750
commit 98dcc74
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// UIControl+Sound.h | ||
// UIControlSound | ||
// | ||
// Created by Fred Showell on 6/01/13. | ||
// Copyright (c) 2013 Fred Showell. All rights reserved. | ||
// https://github.com/scopegate/octave | ||
// Octave: A free library of UI sounds, handmade for iOS http://raisedbeaches.com/octave | ||
#import <UIKit/UIKit.h> | ||
#import <AVFoundation/AVFoundation.h> | ||
|
||
@interface UIControl (JKSound) | ||
|
||
/// Set the sound for a particular control event (or events). | ||
/// @param name The name of the file. The method looks for an image with the specified name in the application’s main bundle. | ||
/// @param controlEvent A bitmask specifying the control events for which the action message is sent. See “Control Events” for bitmask constants. | ||
//不同事件增加不同声音 | ||
- (void)jk_setSoundNamed:(NSString *)name forControlEvent:(UIControlEvents)controlEvent; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// UIControl+Sound.m | ||
// UIControlSound | ||
// | ||
// Created by Fred Showell on 6/01/13. | ||
// Copyright (c) 2013 Fred Showell. All rights reserved. | ||
// | ||
|
||
#import "UIControl+JKSound.h" | ||
#import <objc/runtime.h> | ||
|
||
// Key for the dictionary of sounds for control events. | ||
static char const * const jk_kSoundsKey = "jk_kSoundsKey"; | ||
|
||
@implementation UIControl (JKSound) | ||
|
||
- (void)jk_setSoundNamed:(NSString *)name forControlEvent:(UIControlEvents)controlEvent | ||
{ | ||
// Remove the old UI sound. | ||
NSString *oldSoundKey = [NSString stringWithFormat:@"%lu", controlEvent]; | ||
AVAudioPlayer *oldSound = [self jk_sounds][oldSoundKey]; | ||
[self removeTarget:oldSound action:@selector(play) forControlEvents:controlEvent]; | ||
|
||
// Set appropriate category for UI sounds. | ||
// Do not mute other playing audio. | ||
[[AVAudioSession sharedInstance] setCategory:@"AVAudioSessionCategoryAmbient" error:nil]; | ||
|
||
// Find the sound file. | ||
NSString *file = [name stringByDeletingPathExtension]; | ||
NSString *extension = [name pathExtension]; | ||
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:file withExtension:extension]; | ||
|
||
NSError *error = nil; | ||
|
||
// Create and prepare the sound. | ||
AVAudioPlayer *tapSound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error]; | ||
NSString *controlEventKey = [NSString stringWithFormat:@"%lu", controlEvent]; | ||
NSMutableDictionary *sounds = [self jk_sounds]; | ||
[sounds setObject:tapSound forKey:controlEventKey]; | ||
[tapSound prepareToPlay]; | ||
if (!tapSound) { | ||
NSLog(@"Couldn't add sound - error: %@", error); | ||
return; | ||
} | ||
|
||
// Play the sound for the control event. | ||
[self addTarget:tapSound action:@selector(play) forControlEvents:controlEvent]; | ||
} | ||
|
||
|
||
#pragma mark - Associated objects setters/getters | ||
|
||
- (void)setJk_sounds:(NSMutableDictionary *)sounds | ||
{ | ||
objc_setAssociatedObject(self, jk_kSoundsKey, sounds, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
} | ||
|
||
- (NSMutableDictionary *)jk_sounds | ||
{ | ||
NSMutableDictionary *sounds = objc_getAssociatedObject(self, jk_kSoundsKey); | ||
|
||
// If sounds is not yet created, create it. | ||
if (!sounds) { | ||
sounds = [[NSMutableDictionary alloc] initWithCapacity:2]; | ||
// Save it for later. | ||
[self setJk_sounds:sounds]; | ||
} | ||
|
||
return sounds; | ||
} | ||
|
||
@end |