iPhone三角形を表示

3回画面をタッチすると、タッチした点を結んだ三角形が表示されるといった簡単なiPhoneアプリのサンプルコードを描いてみます。


サンプルを動かした動画です

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (strong, nonatomic) NSMutableArray *points;

@property (strong, nonatomic) NSMutableArray *marks;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

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

}

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

{

    

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

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

    

    for (UITouch *t in touches) {

        [self.points addObjectsFromArray:[touches allObjects]];

        

        CGPoint p = [t locationInView:self.view];

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

        NSMutableAttributedString *mstr = [[NSMutableAttributedString alloc] initWithString:@”x”];

        [mstr addAttributes:@{

                              NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline],

                              NSStrokeWidthAttributeName : @-4,

                              NSStrokeColorAttributeName : [self color:0],

                              NSForegroundColorAttributeName : [self color:2]

                              } range:NSMakeRange(0, 1)];

        markLabel.attributedText = mstr;

        [markLabel sizeToFit];

        markLabel.center = p;

        [self.view addSubview:markLabel];

        

        markLabel.transform = CGAffineTransformMakeScale(5.0, 5.0);

        [UIView animateWithDuration:0.5 animations:^{

            markLabel.transform = CGAffineTransformIdentity;

        }];

    }

}

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

{

    if ([self.points count] > 2) {

        [self createTraiangle:[((UITouch *) self.points[0]) locationInView:self.view]

                       point2:[((UITouch *) self.points[1]) locationInView:self.view]

                       point3:[((UITouch *) self.points[2]) locationInView:self.view]];

        [self.points removeAllObjects];

    }

}

– (void)createTraiangle:(CGPoint)p1 point2:(CGPoint)p2 point3:(CGPoint)p3

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:p1];

    [path addLineToPoint:p2];

    [path addLineToPoint:p3];

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.path = path.CGPath;

    sl.fillColor = [self color:arc4random()%4 + 1].CGColor;

    [self.view.layer addSublayer:sl];

}

#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