Skip to content

Commit

Permalink
UIControl+JKSound
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakey committed Jun 3, 2016
1 parent 6ec1750 commit 98dcc74
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
6 changes: 6 additions & 0 deletions JKCategories-Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@
A2AB1A661B381E3900ED88AF /* UIFontDemoViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A2AB1A641B381E3900ED88AF /* UIFontDemoViewController.xib */; };
A2AF02A41A74BACB003FEC8D /* NSDictionary+JKSafeAccess.m in Sources */ = {isa = PBXBuildFile; fileRef = A2AF02A31A74BACB003FEC8D /* NSDictionary+JKSafeAccess.m */; };
A2AF02AA1A74C096003FEC8D /* NSDictionary+JKMerge.m in Sources */ = {isa = PBXBuildFile; fileRef = A2AF02A91A74C096003FEC8D /* NSDictionary+JKMerge.m */; };
A2B13FF61D01AA300035FC4F /* UIControl+JKSound.m in Sources */ = {isa = PBXBuildFile; fileRef = A2B13FF51D01AA300035FC4F /* UIControl+JKSound.m */; };
A2B995CA1B46778500011613 /* UIButton+JKTouchAreaInsets.m in Sources */ = {isa = PBXBuildFile; fileRef = A2B995C91B46778500011613 /* UIButton+JKTouchAreaInsets.m */; };
A2B995CD1B46779500011613 /* UINavigationItem+JKMargin.m in Sources */ = {isa = PBXBuildFile; fileRef = A2B995CC1B46779500011613 /* UINavigationItem+JKMargin.m */; };
A2B995D31B46797500011613 /* UIViewController+JKBlockSegue.m in Sources */ = {isa = PBXBuildFile; fileRef = A2B995D21B46797500011613 /* UIViewController+JKBlockSegue.m */; };
Expand Down Expand Up @@ -925,6 +926,8 @@
A2AF02A31A74BACB003FEC8D /* NSDictionary+JKSafeAccess.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "NSDictionary+JKSafeAccess.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
A2AF02A81A74C096003FEC8D /* NSDictionary+JKMerge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "NSDictionary+JKMerge.h"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
A2AF02A91A74C096003FEC8D /* NSDictionary+JKMerge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "NSDictionary+JKMerge.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
A2B13FF41D01AA300035FC4F /* UIControl+JKSound.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIControl+JKSound.h"; sourceTree = "<group>"; };
A2B13FF51D01AA300035FC4F /* UIControl+JKSound.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIControl+JKSound.m"; sourceTree = "<group>"; };
A2B995C81B46778500011613 /* UIButton+JKTouchAreaInsets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIButton+JKTouchAreaInsets.h"; sourceTree = "<group>"; };
A2B995C91B46778500011613 /* UIButton+JKTouchAreaInsets.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIButton+JKTouchAreaInsets.m"; sourceTree = "<group>"; };
A2B995CB1B46779500011613 /* UINavigationItem+JKMargin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UINavigationItem+JKMargin.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1168,6 +1171,8 @@
A202D9951B0F914D00EAB199 /* UIControl */ = {
isa = PBXGroup;
children = (
A2B13FF41D01AA300035FC4F /* UIControl+JKSound.h */,
A2B13FF51D01AA300035FC4F /* UIControl+JKSound.m */,
A202D99C1B0F919300EAB199 /* UIControl+JKBlock.h */,
A202D99D1B0F919300EAB199 /* UIControl+JKBlock.m */,
A202D9B91B1035DC00EAB199 /* UIControl+JKActionBlocks.h */,
Expand Down Expand Up @@ -3348,6 +3353,7 @@
A22845251BE2085C000AD2F6 /* NSDecimalNumber+JKCalculatingByString.m in Sources */,
A2958D2D1B359BE400D7AA0F /* NSString+JKPinyin.m in Sources */,
A2958CFD1B358EA400D7AA0F /* UIApplication+JKNetworkActivityIndicator.m in Sources */,
A2B13FF61D01AA300035FC4F /* UIControl+JKSound.m in Sources */,
A2958CDE1B35826C00D7AA0F /* CAShapeLayerDemoViewController.m in Sources */,
A284C1491AEBCFB000D90ED5 /* NSIndexPathDemoViewController.m in Sources */,
A202D9A21B102A4F00EAB199 /* NSNumber+JKRound.m in Sources */,
Expand Down
20 changes: 20 additions & 0 deletions JKCategories/UIKit/UIControl/UIControl+JKSound.h
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
72 changes: 72 additions & 0 deletions JKCategories/UIKit/UIControl/UIControl+JKSound.m
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

0 comments on commit 98dcc74

Please sign in to comment.