Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

複数のターゲット/アクション

Tatsuro Ueda edited this page · 2 revisions

イベントが複数ある場合、ボタンはイベントに応じてターゲットとアクションを変えなければならない。

ボタンクラス:

enum {
    buttonEvent_Touched,      // 指が離れた
    buttonEvent_MoveOut,      // ボタン外に指が外れた
    buttonEvent_MoveIn,       // ボタン内に指が戻った
    buttonEvent_TouchUpInside // 指がボタン内で離れた
};

@interface Button : UIView
{
    id     targets[4];
    SEL    actions[4];
}
- (void)setTarget:(id)target action:(SEL)action forEvent:(int)event;
@end

@implementation Button
- (void)setTarget:(id)target action:(SEL)action forEvent:(int)event
{
    targets[event] = target;
    actions[event] = action;
}
@end

NSDictionaryクラスを使う

さらにNSDictionaryクラスを使うと、以下のように書くことができる。

ボタンクラス:

@interface Button : UIView {
    NSMutableDictionary *targetActionDic;
}
- (void)setTarget:(id)target action:(SEL)action forEvent:(int)event;
{
    id  target;
    SEL action;
}
@end

@implementation Button
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        targetAndActionDic = [[NSMutableDictionary alloc] init];
    }
}

- (void)setTarget:(id)target action:(SEL)action forEvent:(int)event
{
    TargetAndAction *ta = [[TargetAndAction alloc] init];

    ta->target = target;
    ta->action = action;
    NSString *key = [NSString stringWithFormat:@"%d", event];
    [targetAndActionDic setObject:ta forKey:key
}

- (void)sendAction:(int)event
{
    NSString *key = [NSString stringWithFormat:@"%d", event];
    targetAndAction *ta = [targetAndActionDic objectForKey:key];
    if (ta)
    {
        [ta->target performSelector:ta->action withObject:self];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.alpha = 0.5;
    [self sendAction:buttonEvent_Touched];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.alpha = 1.0;
    [self sendAction:buttonEvent_TouchUpInside];
}

- (void)touchesCanceled:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.alpha = 1.0;
    [self sendAction:buttonEvent_MoveOut];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    float alpha;
    if (CGRectContainsPoint(self.bounds, [touch locationInView:self])) 
    {
        alpha = 0.5;
    } 
    else 
    {
        alpha = 1.0;
    }
    if (self.alpha != alpha)
    {
        self.alpha = alpha;
        [self sendAction:(alpha == 1.0)?buttonEvent_MoveOut:buttonEvent_MoveIn];
    }
}

@end

TargetAndActionクラス:

@interface TargetAndAction : NSObject {
@public
    id   target;

...

    ta->target = target;
    ta->action = action;

ビューコントローラクラス:

@interface ViewController : UIViewController
@property UIColor *lastColor
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    for (int i = 0; i < 7; i++)
    {
        ...

        bt.backgroundColor = [UIColor colorWithHue:(float)i / 7.0 
                                        saturation:1.0
                                        brightness:1.0
                                             alpha:1.0
                             ];
        [bt setTarget:self action:@selector(buttonActionStart:) forEvent:buttonEvent_Touched];
        [bt setTarget:self action:@selector(buttonActionMoveIn:) forEvent:buttonEvent_MoveIn];
        [bt setTarget:self action:@selector(buttonActionMoveOut:) forEvent:buttonEvent_MoveOut];

        ...

    }    
}   

- (void)buttonActionStart:(UIView *)sender
{
    self.lastColor = indicator.backgroundColor;
    indicator.backgroundColor = sender.backgroundColor;
}

- (void)buttonActionMoveIn:(UIView *)sender
{
    indicator.backgroundColor = sender.backgourndColor;
}

- (void)buttonActionMoveOut:(UIView *)sender
{
    indicator.backgroundColor = self.lastColor
}

@end
Something went wrong with that request. Please try again.