iPhone扇

扇を開いたり閉じたりする、iPhoneアプリのサンプルコードを描いてみます。

動かすとこんなんです

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UISlider *slider;

@end

@implementation ViewController

– (void)viewDidLoad

{

    self.view.backgroundColor = [UIColor purpleColor];

    [super viewDidLoad];

    [self createSlider];

    [self createFan];

}

– (void)createSlider

{

    UISlider *s = [[UISlider alloc] init];

    s.center = CGPointMake(160, 400);

    s.minimumValue = 0;

    s.maximumValue = 100;

    [self.view addSubview:s];

    [s addTarget:self action:@selector(slide:) forControlEvents:UIControlEventValueChanged];

    self.slider = s;

}

– (void)createFan

{

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

        UIView *f = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 160)];

        f.tag = i;

        f.backgroundColor = [UIColor redColor];

        f.layer.anchorPoint = CGPointMake(0.5, 0.9);

        f.layer.position = CGPointMake(160, 260);

        f.layer.shadowColor = [UIColor brownColor].CGColor;

        f.layer.shadowOpacity = 1.0;

        f.layer.shadowOffset = CGSizeMake(-1.0, 1.0);

        f.layer.shadowRadius = 0.0;

        f.transform = CGAffineTransformMakeRotation(-M_PI * 0.35);

        [self.view addSubview:f];

    }

}

– (void)slide:(UISlider*)sender

{

    __block float angle = M_PI * 0.065 * sender.value / sender.maximumValue;

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

        if (![v isKindOfClass:[UISlider class]])

            v.transform = CGAffineTransformMakeRotation(-M_PI*0.35 + angle * v.tag);

    }];

}

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

{

    if (self.slider.value > 0) {

        [UIView animateWithDuration:0.5 animations:^{

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

                if (![v isKindOfClass:[UISlider class]])

                    v.transform = CGAffineTransformMakeRotation(-M_PI*0.35);

            }];

        } completion:^(BOOL finished) {

            self.slider.value = self.slider.minimumValue;

        }];

        

    } else {

        

        [UIView animateWithDuration:1.5 animations:^{

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

                if (![v isKindOfClass:[UISlider class]]) {

                    float angle = –M_PI*0.35 + M_PI * 0.065 * v.tag;

                    v.transform = CGAffineTransformMakeRotation(angle);

                }

            }];

        } completion:^(BOOL finished) {

            self.slider.value = self.slider.maximumValue;

        }];

    }

}

@end