iPhone自動で座席決め

面倒な席決めを代わりにやってくれるiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

typedef enum {

    DeskTagSample = 1,

    DeskTagNotHold,

    DeskTagHold,

} DeskState;

@interface ViewController () <UIGestureRecognizerDelegate>

@property (nonatomic, weak) UIView *selectedDesk;

@property NSInteger counter;

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    self.view.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:0 alpha:1.0];

    

    UIButton *change = [UIButton buttonWithType:UIButtonTypeSystem];

    change.titleLabel.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] fontWithSize:30];

    [change setTitle:@”change” forState:UIControlStateNormal];

    [change sizeToFit];

    change.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) – 30);

    [self.view addSubview:change];

    [change addTarget:self action:@selector(changeSeats) forControlEvents:UIControlEventTouchUpInside];

    

    [self createDesk:DeskTagSample];

}

– (UIView *)createDesk:(DeskState)state

{

    UILabel *deskIcon = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.view.bounds) – 60, 50, 50)];

    deskIcon.tag = state;

    deskIcon.backgroundColor = [UIColor brownColor];

    deskIcon.text = @”?”;

    deskIcon.textColor = [UIColor whiteColor];

    deskIcon.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] fontWithSize:25];

    deskIcon.textAlignment = NSTextAlignmentCenter;

    deskIcon.userInteractionEnabled = YES;

    [self.view addSubview:deskIcon];

    

    UILabel *stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 35, 40, 12)];

    stateLabel.backgroundColor = [UIColor lightGrayColor];

    stateLabel.layer.cornerRadius = 6;

    stateLabel.font = [UIFont boldSystemFontOfSize:12];

    stateLabel.text = @”HOLD”;

    stateLabel.textColor = [UIColor grayColor];

    stateLabel.textAlignment = NSTextAlignmentCenter;

    [deskIcon addSubview:stateLabel];

    

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

    [deskIcon addGestureRecognizer:pan];

    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

    tap.delegate = self;

    [deskIcon addGestureRecognizer:tap];

    

    return deskIcon;

}

– (void)pan:(UIPanGestureRecognizer*)gr

{

    if (gr.state == UIGestureRecognizerStateBegan) {

        UIView *desk;

        if(gr.view.tag == DeskTagSample) {

            desk = [self createDesk:DeskTagNotHold];

            self.counter++;

            [(UILabel*)desk setText:[@(self.counter) stringValue]];

        } else {

            desk = gr.view;

        }

        self.selectedDesk = desk;

        

    } else if (gr.state == UIGestureRecognizerStateChanged) {

        self.selectedDesk.center = [gr locationInView:self.view];

    }

}

– (void)tap:(UITapGestureRecognizer*)gr

{

    UILabel *lagel = gr.view.subviews[0];

    if(lagel.textColor == [UIColor grayColor]) {

        gr.view.tag = DeskTagHold;

        lagel.backgroundColor = [UIColor yellowColor];

        lagel.textColor = [UIColor redColor];

    } else {

        gr.view.tag = DeskTagNotHold;

        lagel.backgroundColor = [UIColor lightGrayColor];

        lagel.textColor = [UIColor grayColor];

    }

}

– (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}

– (void)changeSeats

{

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@”tag == %d”, DeskTagNotHold];

    

    NSArray *members = [self.view.subviews filteredArrayUsingPredicate:predicate];

    NSArray *shuffled = [self shuffle:members];

    for (int i=0; i<[members count]; i++) {

        UILabel *origin = members[i];

        CGPoint p = [(UILabel*)shuffled[i] center];

        dispatch_async(dispatch_get_main_queue(), ^{

            [UIView animateWithDuration:0.5 animations:^{

                origin.center = p;

            }];

        });

    }

}

– (NSArray*)shuffle:(NSArray*)origin

{

    NSMutableArray *copy = [origin mutableCopy];

    for (NSUInteger i = 0; i < [origin count]; i++) {

        NSInteger n = arc4random_uniform([origin count] – i) + i;

        [copy exchangeObjectAtIndex:i withObjectAtIndex:n];

    }

    return copy;

}

@end