窓から外をみる感じで風景イメージを流すサンプル

(XcodeのiOS6 Simulatorで試しています。)

ポイント

・BlendModeClearで透明な窓を作る

・適当に横長のイメージを用意してUIImageViewにする

サンプルコード

#import “ViewController.h”

@interface ClearWindow : UIView

@end

@implementation ClearWindow

– (void)drawRect:(CGRect)rect

{

    //

    [[UIColor lightGrayColor] setFill];

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextAddRect(ctx, self.bounds);

    CGContextDrawPath(ctx, kCGPathFill);

    

    //

    [[UIColor clearColor] setFill];

    CGContextSetBlendMode(ctx, kCGBlendModeClear);

    CGContextAddArc(ctx, 160, 200, 100, 0, 2*M_PI, NO);

    CGContextDrawPath(ctx, kCGPathFill);

}

@end

@interface ViewController () {

    UIImageView *iv;

    NSTimer *timer;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    // 風景イメージを貼る

    UIImage *scene = [UIImage imageNamed:@”sampleimage”];

    iv = [[UIImageView alloc] initWithImage:scene];

    iv.center = CGPointMake(160, 200);

    [self.view addSubview:iv];

    

    // 上に窓をかぶせる

    ClearWindow *clw = [[ClearWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    clw.backgroundColor = [UIColor clearColor];

    [self.view addSubview:clw];

}

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

{

    timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moveScene) userInfo:nil repeats:YES];

}

– (void)moveScene

{

    iv.center = CGPointMake(iv.center.x + 1, iv.center.y);

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end