iPhoneぼかしボタン

4つあるボタンのどれかを押すと、他のボタンがぼかされる。というiPhoneアプリを描いてみます。(SpriteKit使ってます。ぼかし処理は重いです。)

動作イメージ

XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface TextEffectScene : SKScene

@property BOOL contentCreated;

@property (strong, nonatomic) SKEffectNode *blurEffectNode;

@end

@implementation TextEffectScene

@synthesize blurEffectNode;

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    CIFilter *filter = [CIFilter filterWithName:@”CIGaussianBlur”];

    [filter setDefaults];

    [filter setValue:@8 forKey:@”inputRadius”];

    

    blurEffectNode = [[SKEffectNode alloc] init];

    blurEffectNode.blendMode = SKBlendModeMultiply;

    blurEffectNode.filter = filter;

    [self addChild:blurEffectNode];

    [self createIcon:@”First” point:CGPointMake(50, 300) color:[SKColor greenColor]];

    [self createIcon:@”Second” point:CGPointMake(200, 300) color:[SKColor yellowColor]];

    [self createIcon:@”Third” point:CGPointMake(50, 100) color:[SKColor redColor]];

    [self createIcon:@”Fourth” point:CGPointMake(200, 100) color:[SKColor blueColor]];

    

}

– (void)createIcon:(NSString*)text point:(CGPoint)p color:(SKColor*)color

{

    SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(60, 80)];

    box.position = p;

    box.name = @”iconbox”;

    [self addChild:box];

    

    SKShapeNode *icon = [[SKShapeNode alloc] init];

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 20, 60, 60) cornerRadius:5];

    [path appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(5, 65, 10, 10)]];

    [path appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(45, 65, 10, 10)]];

    

    icon.path = path.CGPath;

    icon.fillColor = color;

    icon.strokeColor = [SKColor whiteColor];

    icon.lineWidth = 1;

    [box addChild:icon];

    

    SKLabelNode *initial = [[SKLabelNode alloc] init];

    initial.text = [text substringToIndex:1];

    initial.fontSize = 50;

    initial.position = CGPointMake(30, 20);

    [box addChild:initial];

    

    SKLabelNode *iconName = [[SKLabelNode alloc] init];

    iconName.text = text;

    iconName.fontSize = 20;

    iconName.position = CGPointMake(30, 0);

    [box addChild:iconName];

}

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

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    [self enumerateChildNodesWithName:@”iconbox” usingBlock:^(SKNode *node, BOOL *stop) {

        if ([node containsPoint:p]) {

            [self performSelector:@selector(reset:) withObject:[NSValue valueWithCGPoint:node.position] afterDelay:2.0];

            

            SKAction *move = [SKAction moveTo:CGPointMake(130, 200) duration:0.4];

            [node runAction:move];

        } else {

            [self performSelector:@selector(blur:) withObject:node afterDelay:0.5];

        }

    }];

}

– (void)blur:(SKNode*)node

{

    [node removeFromParent];

    [blurEffectNode addChild:node];

}

– (void)reset:(NSValue*)point

{

    [self enumerateChildNodesWithName:@”iconbox” usingBlock:^(SKNode *node, BOOL *stop) {

        node.position = [point CGPointValue];

    }];

    

    [blurEffectNode enumerateChildNodesWithName:@”iconbox” usingBlock:^(SKNode *node, BOOL *stop) {

        [node removeFromParent];

        [self addChild:node];

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[TextEffectScene alloc] initWithSize:self.view.bounds.size];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end