iphoneカクカククルクル

色のついたパネルを好きなようにならべた後、メーリゴーランドみたいにくるくる回せるようなiPhoneアプリのサンプルコードを描いてみます。


サンプルを動かした動画

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (weak, nonatomic) UIView *selectedCopy;

@property (strong, nonatomic) NSMutableArray *marks;

@property (strong, nonatomic) NSTimer *timer;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

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

    [self createMark];

}

– (void)createMark

{

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

        float x = 20 + 60 * i;

        float y = 400;

        UIView *mark = [[UIView alloc] initWithFrame:CGRectMake(x, y, 50, 50)];

        mark.backgroundColor = [self color:i];

        mark.layer.cornerRadius = 25;

        mark.tag = 1;

        [self.view addSubview:mark];

    }

    

    UIButton *start = [UIButton buttonWithType:UIButtonTypeSystem];

    start.backgroundColor = [self color:0];

    start.frame = CGRectMake(260, 400, 50, 50);

    [start setTitle:@”Go” forState:UIControlStateNormal];

    start.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];

    [self.view addSubview:start];

    

    [start addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];

}

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

{

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

    

    [self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        if ([obj tag] == 1) {

            if (CGRectContainsPoint([obj frame], p)) {

                // copy view

                self.selectedCopy = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:obj]];

                self.selectedCopy.tag = 2;

                [self.view addSubview:self.selectedCopy];

            }

        }

    }];

}

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

{

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

    if (self.selectedCopy) {

        self.selectedCopy.center = p;

    }

}

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

{

    if (!self.marks) self.marks = [[NSMutableArray alloc] init];

    if (self.selectedCopy) {

        [self.marks addObject:self.selectedCopy];

        self.selectedCopy = nil;

    }

}

– (void)push:(UIButton*)sender

{

    if ([sender.titleLabel.text isEqual:@”Go”]) {

        [sender setTitle:@”stop” forState:UIControlStateNormal];

        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(goround) userInfo:nil repeats:YES];

        

    } else {

        [sender setTitle:@”Go” forState:UIControlStateNormal];

        

        [self.timer invalidate];

        self.timer = nil;

        

        [self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            if ([obj tag] == 2) {

                [UIView animateWithDuration:0.5 animations:^{

                    [obj layer].transform = CATransform3DMakeRotation(M_PI, 1, 0, 0);

                } completion:^(BOOL finished) {

                    [obj removeFromSuperview];

                }];

            }

        }];

    }

}

– (void)goround

{

    [UIView animateWithDuration:0.4 animations:^{

        CGPoint first;

        for (int i=0; i<[self.marks count]; i++) {

            if (i == 0) {

                first = [self.marks[0] center];

                [self.marks[i] setCenter:[self.marks[i + 1] center]];

            } else if (i == [self.marks count] – 1) {

                [self.marks[i] setCenter:first];

            } else {

                [self.marks[i] setCenter:[self.marks[i + 1] center]];

            }

        }

    }];

}

#define UIColorHex(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 UIColorHex(0x1D2939);

        case 1:

            return UIColorHex(0x1CAF9A);

        case 2:

            return UIColorHex(0xFEFEFE);

        case 3:

            return UIColorHex(0xEE4F4B);

        case 4:

            return UIColorHex(0xD1DC48);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end