iPhone交項級数と平方

6から1までの平方の和と差を交互にやるとこうなります。というiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *numbers;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:0.3 green:0.7 blue:0.3 alpha:1];

    [self createNumericalExpression];

    [self createButtons];

    [self createConsole];

}

– (void)createNumericalExpression

{

    self.numbers = [NSMutableArray array];

    

    for (int n = 6; n > 0; n–) {

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

        l.attributedText = [self squareLabelSignificand:[@(n) stringValue] exp:@”2″];

        [l sizeToFit];

        l.center = CGPointMake(480 – n * 70, 80);

        [self.view addSubview:l];

        

        if (n>1) {

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

            operator.text = (n%2) ? @”+” : @”-“;

            operator.font = [UIFont fontWithName:@”ChalkboardSE-Light” size:25.0];

            [operator sizeToFit];

            operator.center = CGPointMake(l.center.x + 30, l.center.y);

            [self.view addSubview:operator];

        }

        

        [self.numbers insertObject:l atIndex:0];

    }

}

– (void)createButtons

{

    

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

        float x = 40 + i * 140;

        float y = 120;

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.tag = i;

        btn.frame = CGRectMake(x, y, 100, 40);

        btn.backgroundColor = [UIColor brownColor];

        [btn setTitle:@”▼” forState:UIControlStateNormal];

        [self.view addSubview:btn];

        

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

    }

}

– (void)tap:(UIButton *)sender

{

    switch (sender.tag) {

        case 0:

            [self showRectAnimationLeft:6 right:5 point:CGPointMake(100, 100)];

            break;

        case 1:

            [self showRectAnimationLeft:4 right:3 point:CGPointMake(240, 100)];

            break;

        case 2:

            [self showRectAnimationLeft:2 right:1 point:CGPointMake(380, 100)];

            break;

        default:

            break;

    }

}

– (void)showRectA

{

}

– (void)showRectB

{

    CGPoint oLeft = CGPointMake(100, 200);

    NSMutableArray *left = [NSMutableArray array];

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

        float x = (i%4) * 100 / 4.0 + 5 + oLeft.x;

        float y = (i/4) * 100 / 4.0 + 5 + oLeft.y;

        UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];

        dot.backgroundColor = [UIColor whiteColor];

        dot.center = CGPointMake(x, y);

        [self.view addSubview:dot];

        

        [left addObject:dot];

    }

    

    CGPoint oRight = CGPointMake(300, 200);

    NSMutableArray *right = [NSMutableArray array];

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

        float x = (i%3) * 100 / 4.0 + 5 + oRight.x;

        float y = (i/3) * 100 / 4.0 + 5 + oRight.y;

        UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];

        dot.backgroundColor = [UIColor redColor];

        dot.center = CGPointMake(x, y);

        [self.view addSubview:dot];

        [right addObject:dot];

    }

    

    [UIView animateWithDuration:2.0 animations:^{

        for (UIView *d in right)

            d.transform = CGAffineTransformMakeTranslation(-200, 0);

    } completion:^(BOOL finished) {

        for (UIView *d in right)

            d.backgroundColor = [UIColor blackColor];

    }];

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

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

            if (i%4 == 3 && i < left.count1) {

                [UIView animateWithDuration:1.0 animations:^{

                    ((UIView *)left[i]).transform = CGAffineTransformMakeTranslation(200, 0);

                }];

            }

        }

    });

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

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

        l.text = @”4 + 3″;

        l.font = [UIFont boldSystemFontOfSize:40];

        l.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];

        [l sizeToFit];

        l.center = CGPointMake(240, 250);

        [self.view addSubview:l];

        

        [UIView animateWithDuration:1.0 animations:^{

            l.center = CGPointMake(240, 100);

        }];

    });

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [left enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [obj removeFromSuperview];

        }];

        [right enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [obj removeFromSuperview];

        }];

    });

    

}

– (void)showRectC

{

    CGPoint oLeft = CGPointMake(100, 200);

    NSMutableArray *left = [NSMutableArray array];

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

        float x = (i%2) * 100 / 4.0 + 5 + oLeft.x;

        float y = (i/2) * 100 / 4.0 + 5 + oLeft.y;

        UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

        dot.backgroundColor = [UIColor whiteColor];

        dot.center = CGPointMake(x, y);

        [self.view addSubview:dot];

        

        [left addObject:dot];

    }

    

    CGPoint oRight = CGPointMake(300, 200);

    NSMutableArray *right = [NSMutableArray array];

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

        float x = (i%1) * 100 / 4.0 + 5 + oRight.x;

        float y = (i/1) * 100 / 4.0 + 5 + oRight.y;

        UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

        dot.backgroundColor = [UIColor redColor];

        dot.center = CGPointMake(x, y);

        [self.view addSubview:dot];

        [right addObject:dot];

    }

    

    [UIView animateWithDuration:2.0 animations:^{

        for (UIView *d in right)

            d.transform = CGAffineTransformMakeTranslation(-200, 0);

    } completion:^(BOOL finished) {

        for (UIView *d in right)

            d.backgroundColor = [UIColor blackColor];

    }];

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

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

            if (i%2 == 1 && i < left.count1) {

                [UIView animateWithDuration:1.0 animations:^{

                    ((UIView *)left[i]).transform = CGAffineTransformMakeTranslation(200, 0);

                }];

            }

        }

    });

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

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

        l.text = @”2 + 1″;

        l.font = [UIFont boldSystemFontOfSize:40];

        l.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];

        [l sizeToFit];

        l.center = CGPointMake(240, 250);

        [self.view addSubview:l];

        

        [UIView animateWithDuration:1.0 animations:^{

            l.center = CGPointMake(380, 100);

        }];

    });

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [left enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [obj removeFromSuperview];

        }];

        [right enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [obj removeFromSuperview];

        }];

    });

}

– (void)showRectAnimationLeft:(int)l right:(int)r point:(CGPoint)o

{

    CGPoint oLeft = CGPointMake(100, 200);

    NSMutableArray *left = [NSMutableArray array];

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

        float x = (i%l) * 100 / l + 5 + oLeft.x;

        float y = (i/l) * 100 / l + 5 + oLeft.y;

        UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40/l, 40/l)];

        dot.backgroundColor = [UIColor whiteColor];

        dot.center = CGPointMake(x, y);

        [self.view addSubview:dot];

        

        [left addObject:dot];

    }

    

    CGPoint oRight = CGPointMake(300, 200);

    NSMutableArray *right = [NSMutableArray array];

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

        float x = (i%r) * 100 / l + 5 + oRight.x;

        float y = (i/r) * 100 / l + 5 + oRight.y;

        UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40/l, 40/l)];

        dot.backgroundColor = [UIColor redColor];

        dot.center = CGPointMake(x, y);

        [self.view addSubview:dot];

        [right addObject:dot];

    }

    

    [UIView animateWithDuration:2.0 animations:^{

        for (UIView *d in right)

            d.transform = CGAffineTransformMakeTranslation(-200, 0);

    } completion:^(BOOL finished) {

        for (UIView *d in right)

            d.backgroundColor = [UIColor blackColor];

    }];

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

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

            if (i%l == l-1 && i < left.count1) {

                [UIView animateWithDuration:1.0 animations:^{

                    ((UIView *)left[i]).transform = CGAffineTransformMakeTranslation(200, 0);

                }];

            }

        }

    });

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

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

        label.text = [NSString stringWithFormat:@”%d + %d”, l, r];

        label.font = [UIFont boldSystemFontOfSize:40];

        label.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];

        [label sizeToFit];

        label.center = CGPointMake(240, 250);

        [self.view addSubview:label];

        

        [UIView animateWithDuration:1.0 animations:^{

            label.center = o;

        }];

    });

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [left enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [obj removeFromSuperview];

        }];

        [right enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [obj removeFromSuperview];

        }];

    });

}

– (void)createConsole

{

    UIView *console = [[UIView alloc] initWithFrame:CGRectMake(20, 180, 440, 130)];

    console.backgroundColor = [UIColor blackColor];

    console.layer.cornerRadius = 10;

    [self.view addSubview:console];

}

– (NSAttributedString *)squareLabelSignificand:(NSString *)sign exp:(NSString *)exp

{

    NSString *str = [sign stringByAppendingString:exp];

    NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:str];

    

    UIFont *fontA = [UIFont fontWithName:@”ChalkboardSE-Light” size:50.0];

    NSDictionary *attrsDictionaryA = @{NSFontAttributeName:fontA};

    [atts addAttributes:attrsDictionaryA range:NSMakeRange(0, sign.length)];

    

    

    UIFont *fontB = [UIFont fontWithName:@”ChalkboardSE-Light” size:25.0];

    NSDictionary *attrsDictionaryB = @{NSFontAttributeName:fontB, NSBaselineOffsetAttributeName:@25};

    

    [atts addAttributes:attrsDictionaryB range:NSMakeRange(sign.length, exp.length)];

    

    return atts;

}

@end