Σをドンドン計算していくiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@interface ViewController ()
@property int counter;
@property double total;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createTitle];
[self createGraph];
[self createButton];
}
-(void)createTitle
{
UILabel *a = [[UILabel alloc] init];
a.text = @”Σ”;
a.font = [UIFont systemFontOfSize:40];
[a sizeToFit];
a.center = CGPointMake(160, 100);
[self.view addSubview:a];
UILabel *upper = [[UILabel alloc] init];
upper.text = @”∞”;
[upper sizeToFit];
upper.center = CGPointMake(a.center.x, a.center.y – 25);
[self.view addSubview:upper];
UILabel *bottom = [[UILabel alloc] init];
bottom.text = @”i=1″;
bottom.font = [UIFont systemFontOfSize:14];
[bottom sizeToFit];
bottom.center = CGPointMake(a.center.x, a.center.y + 25);
[self.view addSubview:bottom];
UILabel *numerator = [[UILabel alloc] init];
numerator.text = @”i+1″;
numerator.font = [UIFont systemFontOfSize:25];
[numerator sizeToFit];
numerator.center = CGPointMake(a.center.x + 34, a.center.y – 18);
[self.view addSubview:numerator];
UILabel *b = [[UILabel alloc] init];
b.text = @”ー“;
b.font = [UIFont systemFontOfSize:50];
[b sizeToFit];
b.center = CGPointMake(a.center.x + 30, a.center.y);
[self.view addSubview:b];
UILabel *denominator = [[UILabel alloc] init];
denominator.text = @”2″;
denominator.font = [UIFont systemFontOfSize:25];
[denominator sizeToFit];
denominator.center = CGPointMake(a.center.x + 30, a.center.y + 15);
[self.view addSubview:denominator];
UILabel *power = [[UILabel alloc] init];
power.text = @”i”;
[power sizeToFit];
power.center = CGPointMake(a.center.x + 40, a.center.y + 10);
[self.view addSubview:power];
}
– (void)createGraph
{
UIBezierPath *path = [UIBezierPath bezierPath];
float s = 250;
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(0, s)];
[path addLineToPoint:CGPointMake(s, s)];
CAShapeLayer *l = [CAShapeLayer layer];
l.position = CGPointMake(40, 150);
l.path = path.CGPath;
l.strokeColor = [UIColor darkGrayColor].CGColor;
l.lineWidth = 2;
l.fillColor = [UIColor clearColor].CGColor;
[self.view.layer addSublayer:l];
}
– (void)createButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@”START” forState:UIControlStateNormal];
[button sizeToFit];
button.center = CGPointMake(160, 440);
[self.view addSubview:button];
[button addTarget:self action:@selector(touch:) forControlEvents:UIControlEventTouchUpInside];
}
– (void)touch:(UIButton *)sender
{
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(prot:) userInfo:nil repeats:YES];
}
– (void)prot:(NSTimer *)sender
{
if (self.counter > 40) {
[sender invalidate];
}
NSUInteger i = self.counter++;
self.total += (i + i) / pow(2.0, i);
UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 3, 3)];
dot.center = CGPointMake(i * 10 + 50, 200 – self.total * 10);
dot.backgroundColor = [UIColor orangeColor];
[self.view addSubview:dot];
UILabel *cutin = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
cutin.textAlignment = NSTextAlignmentCenter;
cutin.text = [NSString stringWithFormat:@”x:%d y:%.2lf”, i, self.total];
cutin.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.8];
cutin.layer.cornerRadius = 20;
cutin.layer.masksToBounds = YES;
cutin.center = CGPointMake(400, 280 + (i % 5) * 20);
[self.view addSubview:cutin];
[UIView animateWithDuration:2.5 animations:^{
cutin.transform = CGAffineTransformMakeTranslation(-400, 0);
} completion:^(BOOL finished) {
[cutin removeFromSuperview];
}];
}
@end