iPhoneブロードキャスト

発信器をポチポチとふやしてブロードキャストしてみるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *receivers;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithWhite:0.68 alpha:1];

    self.receivers = [NSMutableArray array];

}

– (UIView *)addReceiver

{

    int cnt = self.receivers.count;

    float h = cnt * 20 + 60;

    UIView *receiver = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, h)];

    receiver.backgroundColor = [UIColor colorWithHue:cnt * 0.2 saturation:0.6 brightness:0.8 alpha:1];

    [self.view addSubview:receiver];

    [self.receivers addObject:receiver];

    

    for (int i=0; i<self.receivers.count; i++) {

        UIView *r = self.receivers[i];

        r.frame = CGRectMake(r.frame.origin.x, r.frame.origin.y, 80, h);

        

        UIView *newColorBox = [[UIView alloc] initWithFrame:CGRectMake(10, cnt*20 + 10, 60, 16)];

        newColorBox.backgroundColor = receiver.backgroundColor;

        [r addSubview:newColorBox];

        

        UIView *box = [[UIView alloc] initWithFrame:CGRectMake(10, i*20 + 10, 60, 16)];

        box.backgroundColor = r.backgroundColor;

        [receiver addSubview:box];

    }

    

    

    NSMutableDictionary *viewsDictionary = [NSMutableDictionary dictionary];

    for (int i=0; i<2; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.backgroundColor = [UIColor lightGrayColor];

        btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];

        [btn setTitle:i ? @”+” : @”=” forState:UIControlStateNormal];

        [receiver addSubview:btn];

        

        btn.translatesAutoresizingMaskIntoConstraints = NO;

        [viewsDictionary setObject:btn forKey:[NSString stringWithFormat:@”btn%d”, i+1]];

        

        [btn addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchUpInside];

    }

    

    

    // Auto Layout

    [receiver addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”[btn1]-20-[btn2]” options:NSLayoutFormatAlignAllBottom metrics:nil views:viewsDictionary]];

    [receiver addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:[btn1(20)]-10-|” options:0 metrics:nil views:viewsDictionary]];

    [receiver addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:[btn2(20)]-10-|” options:0 metrics:nil views:viewsDictionary]];

    return receiver;

}

– (void)send:(UIButton *)sender

{

    NSString *word = sender.titleLabel.text;

    UIView *transmitter = sender.superview;

    for (int i=0; i<self.receivers.count; i++) {

        UIView *receiver = self.receivers[i];

        [receiver.subviews enumerateObjectsUsingBlock:^(UIView *v, NSUInteger idx, BOOL *stop) {

            if ([v.backgroundColor isEqual:transmitter.backgroundColor]) {

                UILabel *l = [[UILabel alloc] init];

                l.text = word;

                l.textColor = [UIColor whiteColor];

                l.font = [UIFont boldSystemFontOfSize:20];

                [l sizeToFit];

                

                float oy = l.center.y;

                l.center = [v convertPoint:sender.center fromView:transmitter];

                

                [UIView animateWithDuration:0.3 animations:^{

                    l.center = CGPointMake(v.tag * 15 + 10, oy);

                }];

                [v addSubview:l];

                v.tag = v.tag + 1;

            }

        }];

    }

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    if (self.view == [self.view hitTest:p withEvent:nil]) {

        UIView *v = [self addReceiver];

        v.center = p;

    }

    

}

@end