iPhoneドット強調


ドットの上をなぞるとその周辺が強調されるiPhoneアプリのサンプルコードを描いてみます。







#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor purpleColor];

    

    [self createDot];

}

– (void)createDot

{

    for (int i=0; i<30*30; i++) {

        float x = (i % 30) * 10 + 10;

        float y = (i / 30) * 10 + 30;

        UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(x, y, 4, 4)];

        dot.tag = 1;

        dot.layer.cornerRadius = 2;

        dot.backgroundColor = [UIColor whiteColor];

        [self.view addSubview:dot];

    }

}

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

{

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

    [self transformDots:p];

}

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

{

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

    [self transformDots:p];

}

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

{

    for (UIView *v in self.view.subviews) {

        if (v.tag == 1) {

            v.transform = CGAffineTransformIdentity;

        }

    }

}

– (void)transformDots:(CGPoint)p

{

    for (UIView *v in self.view.subviews) {

        if (v.tag == 1) {

            float dr = hypotf(p.x – v.center.x, p.y – v.center.y);

            float scale = MAX(1, (160.0 – dr) / 80.0);

            v.transform = CGAffineTransformMakeScale(scale, scale);

        }

    }

}

@end