iPhoneスクロールと数字

なんちゃってパララックスで遊ぶiPhoneアプリのサンプルコードを描いてみます。


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

@interface ViewController () <UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *ones;

@property (nonatomic, weak) UIScrollView *tens;

@property (nonatomic, weak) UIScrollView *hundreds;

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    self.view.backgroundColor = [self color:0];

    [self createOnesDigit];

    [self createTensDigit];

    [self createHundredsDigit];

    [self createMeter];

}

– (void)createMeter

{

    UIView *needle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 320)];

    needle.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

    needle.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.4];

    [self.view addSubview:needle];

}

– (void)createOnesDigit

{

    UIScrollView *digitScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 250, 480, 20)];

    digitScrollView.contentSize = CGSizeMake(20 * 1000 + CGRectGetMaxX(self.view.bounds), 20);

    digitScrollView.showsHorizontalScrollIndicator = NO;

    digitScrollView.bounces = NO;

    digitScrollView.delegate = self;

    digitScrollView.backgroundColor = [self color:1];

    [self.view addSubview:digitScrollView];

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

        float x = i * 20 + CGRectGetMidX(self.view.bounds) – 10;

        CATextLayer *number = [CATextLayer layer];

        number.frame = CGRectMake(x, 0, 20, 20);

        number.fontSize = 10;

        number.string = [@(i) stringValue];

        number.foregroundColor = [self color:4].CGColor;

        number.alignmentMode = kCAAlignmentCenter;

        [digitScrollView.layer addSublayer:number];

    }

    self.ones = digitScrollView;

}

– (void)createTensDigit

{

    UIScrollView *digitScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 150, 480, 40)];

    digitScrollView.contentSize = CGSizeMake(50 * 100 + CGRectGetMaxX(self.view.bounds), 20);

    digitScrollView.showsHorizontalScrollIndicator = NO;

    digitScrollView.bounces = NO;

    digitScrollView.delegate = self;

    digitScrollView.backgroundColor = [self color:2];

    [self.view addSubview:digitScrollView];

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

        float x = i * 50 + CGRectGetMidX(self.view.bounds) – 20;

        UILabel *number = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, 40, 40)];

        number.font = [UIFont boldSystemFontOfSize:20];

        number.text = [@(i*10) stringValue];

        number.textColor = [self color:4];

        number.textAlignment = NSTextAlignmentCenter;

        [digitScrollView addSubview:number];

    }

    self.tens = digitScrollView;

}

– (void)createHundredsDigit

{

    UIScrollView *digitScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 480, 60)];

    digitScrollView.contentSize = CGSizeMake(100 * 10 + CGRectGetMaxX(self.view.bounds), 20);

    digitScrollView.showsHorizontalScrollIndicator = NO;

    digitScrollView.bounces = NO;

    digitScrollView.delegate = self;

    digitScrollView.backgroundColor = [self color:3];

    [self.view addSubview:digitScrollView];

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

        float x = i * 100+ CGRectGetMidX(self.view.bounds) – 30;

        UILabel *number = [[UILabel alloc] initWithFrame:CGRectMake(x, 15, 60, 60)];

        number.font = [UIFont boldSystemFontOfSize:30];

        number.text = [@(i*100) stringValue];

        number.textColor = [self color:4];

        number.textAlignment = NSTextAlignmentCenter;

        [digitScrollView addSubview:number];

    }

    self.hundreds = digitScrollView;

}

– (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    if (scrollView == self.ones) {

        self.tens.contentOffset = CGPointMake(scrollView.contentOffset.x / 4.0, self.tens.contentOffset.y);

        self.hundreds.contentOffset = CGPointMake(scrollView.contentOffset.x / 4.0 / 5.0, self.hundreds.contentOffset.y);

    } else if (scrollView == self.tens) {

        self.ones.contentOffset = CGPointMake(scrollView.contentOffset.x * 4.0, self.ones.contentOffset.y);

        self.hundreds.contentOffset = CGPointMake(scrollView.contentOffset.x / 5.0, self.hundreds.contentOffset.y);

    } else if (scrollView == self.hundreds) {

        self.ones.contentOffset = CGPointMake(scrollView.contentOffset.x * 4.0 * 5.0, self.ones.contentOffset.y);

        self.tens.contentOffset = CGPointMake(scrollView.contentOffset.x * 5.0, self.tens.contentOffset.y);

    }

    

}

#define ColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:0.8]

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return ColorHex(0xDDF09A);

        case 1:

            return ColorHex(0xFDFCBF);

        case 2:

            return ColorHex(0x8DA279);

        case 3:

            return ColorHex(0xFACE52);

        case 4:

            return ColorHex(0xD1694F);

        default:

            break;

    }

    return nil;

}

@end