2種類のスライダーを取得する「DCSlider」クラス

Objective-Cで通常のスライダーと画像スライダーを取得する「DCSlider」クラスを作成しました。

下記よりファイルのダウンロードが行えます。

ダウンロード

使用方法

通常のスライダー取得

1
2
3
UISlider *slider = [DCSlider planeSlider:self rect:CGRectMake(50, 100, 220, 0)
                               minVolume:0.0 maxVolume:1.0 defaultVolume:0.5f continuous:NO
                                     tag:1 selector:@selector(sliderChangedEvent:)];

画像スライダー取得

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//つまみのサムネイル画像
UIImage *imageForThumb = [self getUIImageFromResources:@"slider_thumb" ext:@"png"];

//つまみのサムネイル画像 (ハイライト時)
UIImage *imageForThumbHighlited = [self getUIImageFromResources:@"slider_thumb-o" ext:@"png"];

//スライダーのベース画像
UIImage *imageMinBase = [self getUIImageFromResources:@"slider_min" ext:@"png"];
UIImage *imageMaxBase = [self getUIImageFromResources:@"slider_max" ext:@"png"];

UISlider *imageSlider = [DCSlider imageSlider:self rect:CGRectMake(50, 200, 224, 22)
                                    minVolume:0.0 maxVolume:1.0 defaultVolume:0.5f continuous:NO
                                   thumbImage:imageForThumb thumbHighlitedImage:imageForThumbHighlited minImage:imageMinBase maxImage:imageMaxBase
                                          tag:1 selector:@selector(sliderChangedEvent:)];

ソースコード

DCSlider.h

1
2
3
4
5
6
7
8
9
#import <UIKit/UIKit.h>

@interface DCSlider : UISlider

#pragma mark - public method
+ (UISlider *)planeSlider:(id)delegate rect:(CGRect)rect minVolume:(CGFloat)minVolume maxVolume:(CGFloat)maxVolume defaultVolume:(CGFloat)defaultVolume continuous:(BOOL)continuous tag:(NSUInteger)tag selector:(SEL)selector;
+ (UISlider *)imageSlider:(id)delegate rect:(CGRect)rect minVolume:(CGFloat)minVolume maxVolume:(CGFloat)maxVolume defaultVolume:(CGFloat)defaultVolume continuous:(BOOL)continuous thumbImage:(UIImage *)thumbImage thumbHighlitedImage:(UIImage *)thumbHighlitedImage minImage:(UIImage *)minImage maxImage:(UIImage *)maxImage tag:(NSUInteger)tag selector:(SEL)selector;

@end

DCSlider.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#import "DCSlider.h"

@implementation DCSlider

// スライダー取得
+ (UISlider *)planeSlider:(id)delegate rect:(CGRect)rect minVolume:(CGFloat)minVolume maxVolume:(CGFloat)maxVolume defaultVolume:(CGFloat)defaultVolume continuous:(BOOL)continuous tag:(NSUInteger)tag selector:(SEL)selector
{
    // スライダー生成
    UISlider *slider = [[UISlider alloc] initWithFrame:rect];

    // 値指定
    slider.minimumValue = minVolume;
    slider.maximumValue = maxVolume;
    slider.value = defaultVolume;
    if (defaultVolume > maxVolume) defaultVolume = maxVolume;
    if (defaultVolume < minVolume) defaultVolume = minVolume;

    // イベント指定
    slider.continuous = continuous;
    slider.tag = tag;
    [slider addTarget:delegate action:selector forControlEvents:UIControlEventValueChanged];

    return slider;
}

// 画像スライダー取得
+ (UISlider *)imageSlider:(id)delegate rect:(CGRect)rect minVolume:(CGFloat)minVolume maxVolume:(CGFloat)maxVolume defaultVolume:(CGFloat)defaultVolume continuous:(BOOL)continuous thumbImage:(UIImage *)thumbImage thumbHighlitedImage:(UIImage *)thumbHighlitedImage minImage:(UIImage *)minImage maxImage:(UIImage *)maxImage tag:(NSUInteger)tag selector:(SEL)selector
{
    // スライダー生成
    UISlider *imageSlider = [[UISlider alloc] initWithFrame:rect];

    // 値指定
    imageSlider.minimumValue = minVolume;
    imageSlider.maximumValue = maxVolume;
    imageSlider.value = defaultVolume;
    if (defaultVolume > maxVolume) defaultVolume = maxVolume;
    if (defaultVolume < minVolume) defaultVolume = minVolume;

    // 画像指定
    UIImage *imageForThumb = thumbImage;
    UIImage *imageForThumbHighlited = thumbHighlitedImage;
    UIImage *imageMinBase = minImage;
    UIImage *imageForMin = [imageMinBase stretchableImageWithLeftCapWidth:4 topCapHeight:0];
    UIImage *imageMaxBase = maxImage;
    UIImage *imageForMax = [imageMaxBase stretchableImageWithLeftCapWidth:4 topCapHeight:0];
    [imageSlider setThumbImage:imageForThumb forState:UIControlStateNormal];
    [imageSlider setThumbImage:imageForThumbHighlited forState:UIControlStateHighlighted];
    [imageSlider setMinimumTrackImage:[imageForMin resizableImageWithCapInsets:UIEdgeInsetsFromString(@"4")]
                             forState:UIControlStateNormal];
    [imageSlider setMaximumTrackImage:[imageForMax resizableImageWithCapInsets:UIEdgeInsetsFromString(@"4")]
                             forState:UIControlStateNormal];

    // イベント指定
    imageSlider.continuous = continuous;
    imageSlider.tag = tag;
    [imageSlider addTarget:delegate action:selector forControlEvents:UIControlEventValueChanged];

    return imageSlider;
}

@end

サンプルソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@implementation ViewController

typedef enum sliderId : NSUInteger {
    PLANE_SLIDER = 1,
    IMAGE_SLIDER = 2
} sliderId;

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    //スライダー配置
    [self setSlider];

    //画像スライダー配置
    [self setImageSlider];
}

//スライダー配置
- (void)setSlider
{
    UISlider *slider = [DCSlider planeSlider:self rect:CGRectMake(50, 100, 220, 0)
                                   minVolume:SLIDER_MIN_VOLUME maxVolume:SLIDER_MAX_VOLUME defaultVolume:SLIDER_DEF_VOLUME continuous:NO
                                         tag:PLANE_SLIDER selector:@selector(sliderChangedEvent:)];
    [self.view addSubview:slider];
}

//画像スライダー配置
- (void)setImageSlider
{
    UIImage *imageForThumb = [self getUIImageFromResources:@"slider_thumb" ext:@"png"];
    UIImage *imageForThumbHighlited = [self getUIImageFromResources:@"slider_thumb-o" ext:@"png"];
    UIImage *imageMinBase = [self getUIImageFromResources:@"slider_min" ext:@"png"];
    UIImage *imageMaxBase = [self getUIImageFromResources:@"slider_max" ext:@"png"];
    UISlider *imageSlider = [DCSlider imageSlider:self rect:CGRectMake(50, 200, 224, 22)
                                        minVolume:SLIDER_MIN_VOLUME maxVolume:SLIDER_MAX_VOLUME defaultVolume:SLIDER_DEF_VOLUME continuous:NO
                                       thumbImage:imageForThumb thumbHighlitedImage:imageForThumbHighlited minImage:imageMinBase maxImage:imageMaxBase
                                              tag:IMAGE_SLIDER selector:@selector(sliderChangedEvent:)];
    [self.view addSubview:imageSlider];
}

//スライダー変更時のイベント
- (void)sliderChangedEvent:(UISlider *)slider
{
    NSUInteger tag = slider.tag;
    if (tag == PLANE_SLIDER) {
        NSLog(@"sliderValue: %f", slider.value);
    } else if (tag == IMAGE_SLIDER) {
        NSLog(@"imageSliderValue: %f", slider.value);
    }
}

//画像ファイル取得
- (UIImage *)getUIImageFromResources:(NSString*)fileName ext:(NSString*)ext
{
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:ext];
    UIImage *img = [[UIImage alloc] initWithContentsOfFile:path];
    return (img);
}

@end

お薦めの参考書

本気ではじめるiPhoneアプリ作り Xcode 7.x+Swift 2.x対応
iOSアプリ開発をこれから始める方に最もお薦めな一冊です。解り辛い点は図解入りで解説されており、プログラミングの動作の仕組みから詳しく知ることができます。基本から通信処理まで押さえられており、アプリ公開の手順についても解説されています。

   このエントリーをはてなブックマークに追加

About

Masaki Hirokawa (@dolice_apps)

デザインエンジニア 廣川政樹の開発ブログ。Objective-Cや Javaなど iPhone/Androidアプリ開発に関する技術情報を掲載しています。

iPhone apps

  • リラックス・ヒーリング(無料)
  • 望みが叶う!引き寄せの法則アプリ(無料)
  • ミステリー - 怖い話や不思議な体験、都市伝説まとめ
  • 浮世絵壁紙 - 美しい日本画ギャラリー(無料)
  • アート壁紙HD(無料)
  • 綺麗な壁紙HD iPhone 7/7 Plus/SE & iPod対応(無料)
  • クールな壁紙HD iPhone 7/7 Plus/SE & iPod対応(無料)

Android apps

  • 浮世絵壁紙 - 美しい日本画ギャラリー
  • 綺麗な高画質壁紙

Objective-C Classes

Tag Cloud

ActionScript(9) ActionScript3(7) Ad(4) Adfurikun(2) AdMob(9) Android(10) Animation(17) AppDelegate(3) ARC(1) ArrayList(1) AVAudioPlayer(4) AVAudioSession(1) AVFoundation(3) Banner(2) Bitmap(1) Camera(4) CGAffineTransform(4) CGBlendMode(2) CGContextRef(1) CGImageRef(1) Classes(44) CLLocationManager(1) ConnectivityManager(1) ContentResolver(1) CoreLocation(2) Delegate(3) Device(7) Display(1) DisplayMetrics(1) Download(101) Facebook(6) Foundation(72) Framework(2) Google Analytics(1) Handler(1) iAd(6) ImageView(1) In-AppPurchase(1) iOS(12) iOS 7(15) iOS 7.1(3) iOS 8(1) iPad(7) iPhone(21) iPhone 6(4) Java(7) JavaScript(2) LINE(4) Localize(1) Magazine(1) MediaStore(1) MFComposeViewController(1) Nend(1) NetworkInfo(1) NSArray(23) NSCalendar(3) NSData(2) NSDate(7) NSDateComponents(2) NSDateFormatter(2) NSDictionary(12) NSEnumerator(1) NSIndexSet(1) NSInteger(6) NSMutableArray(17) NSMutableDictionary(5) NSMutableOrderedSet(7) NSMutableString(4) NSMutableURLRequest(1) NSNotificationCenter(1) NSNumber(1) NSObject(1) NSOrderedSet(7) NSRange(2) NSSelectorFromString(1) NSSet(6) NSString(19) NSTimer(4) NSTimeZone(1) NSURL(7) NSURLConnection(1) NSURLRequest(2) NSUserDefaults(7) NSXMLParser(2) Objective-C(222) PHP(1) Products(14) QuartzCore(3) RSS(2) Runnable(1) Sample(51) Screensaver(9) SDK(2) ShareCompat(1) Social(10) StoreKit(1) Twitter(6) UIAccelerometer(3) UIActionSheet(1) UIActivityIndicator(1) UIActivityIndicatorView(3) UIActivityViewController(1) UIAlertView(5) UIApplication(7) UIButton(4) UIColor(3) UIDatePicker(5) UIDevice(6) UIDeviceOrientation(1) UIEvent(6) UIImage(15) UIImagePickerController(4) UIImageView(12) UIKit(58) UILabel(7) UINavigationBar(4) UINavigationItem(1) UIPasteboard(4) UIScreen(1) UIScrollBar(1) UIScrollView(1) UISlider(3) UIStoryboardSegue(3) UISwitch(1) UITabBar(3) UITableView(4) UITextField(2) UIView(11) UIViewAnimationTransition(1) UIViewController(4) UIWebView(5) WindowManager(1) Xcode(48) Xcode 5(8) Xcode 5.1(2)