iPhone常用対数2進数

logの値を2進数の01010101に概算してみるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *cells;

@property (nonatomic) int count;

@property (nonatomic) float value;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    [self createCells];

    [self showLogx];

    self.value = 2.0;

}

– (void)createCells {

    self.cells = [NSMutableArray array];

    

    CGPoint start = CGPointMake(20, 190);

    float w = (CGRectGetMaxX(self.view.bounds) – 40) / 7.0;

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

        float x = (i % 7) * w + start.x;

        float y = (i / 7) * w + start.y;

        

        UIView *cell = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, w)];

        cell.backgroundColor = [UIColor colorWithHue:0.2 saturation:0.3 brightness:1 alpha:1];

        

        cell.layer.borderWidth = 1.0;

        [self.view addSubview:cell];

        [self.cells addObject:cell];

        

        

        if ((i%7) == 0 && i < 42) {

            cell.backgroundColor = [UIColor lightGrayColor];

            UILabel *l = [[UILabel alloc] initWithFrame:cell.bounds];

            l.text = [NSString stringWithFormat:@”%d”, -i / 7];

            l.textColor = [UIColor colorWithHue:0.2 saturation:0.3 brightness:1 alpha:1];

            l.textAlignment = NSTextAlignmentCenter;

            l.font = [UIFont boldSystemFontOfSize:20];

            [cell addSubview:l];

        }

        

        if (i==42) {

            cell.backgroundColor = [UIColor lightGrayColor];

            UILabel *l = [[UILabel alloc] initWithFrame:cell.bounds];

            l.text = @”x>10″;

            l.textColor = [UIColor colorWithHue:0.2 saturation:0.3 brightness:1 alpha:1];

            l.textAlignment = NSTextAlignmentCenter;

            l.font = [UIFont boldSystemFontOfSize:20];

            [cell addSubview:l];

        } else if (i > 42) {

            cell.backgroundColor = [UIColor orangeColor];

        }

    }

}

– (void)showLogx {

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

    NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:@”log102″];

    [atts addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, 6)];

    

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

    NSDictionary *attrsDictionaryA = @{NSFontAttributeName:fontA};

    [atts addAttributes:attrsDictionaryA range:NSMakeRange(0, 3)];

    [atts addAttributes:attrsDictionaryA range:NSMakeRange(5, 1)];

    

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

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

    [atts addAttributes:attrsDictionaryB range:NSMakeRange(3, 2)];

    // vertical align

    [atts addAttributes:@{NSBaselineOffsetAttributeName:@-5} range:NSMakeRange(3, 2)];

    

    logx.attributedText = atts;

    [logx sizeToFit];

    logx.center = CGPointMake(CGRectGetMidX(self.view.bounds), 130);

    [self.view addSubview:logx];

}

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

{

    if (self.count > 5) {

        return;

    }

    

    int row = self.count + 1;

    int col = self.count;

    int idx = row + col * 7;

    UIView *cell = self.cells[idx];

    

    BOOL divide = NO;

    if (self.value > 10) {

        self.value *= 0.1;

        divide = YES;

    }

    

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

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

        l1.text = [NSString stringWithFormat:@”%.2f”, self.value];

        if (divide) {

            l1.numberOfLines = 2;

            l1.text = [l1.text stringByAppendingString:@”\n×10″];

        }

        l1.transform = CGAffineTransformMakeRotation(M_PI * 0.2);

        [l1 sizeToFit];

        [cell addSubview:l1];

        l1.center = CGPointMake(CGRectGetMidX(cell.bounds), CGRectGetMidY(cell.bounds));

        if (i == 0) {

            l1.center = cell.center;

            [self.view addSubview:l1];

            [UIView animateWithDuration:0.4 animations:^{

                l1.center = CGPointMake(cell.center.x, cell.center.y + (6 – col) * cell.bounds.size.height);

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:0.1 animations:^{

                    l1.layer.transform = CATransform3DRotate(l1.layer.transform, M_PI*0.5, 0, 1, 0);

                } completion:^(BOOL finished) {

                    l1.text = divide ? @”1″ : @”0″;

                    [l1 sizeToFit];

                    [UIView animateWithDuration:0.1 animations:^{

                        l1.layer.transform = CATransform3DRotate(l1.layer.transform, –M_PI*0.5, 0, 1, 0);

                        l1.layer.transform = CATransform3DScale(l1.layer.transform, 2.0, 2.0, 1.00);

                    }];

                }];

            }];

        }

    }

    self.value *= self.value;

    ++self.count;

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end