Skip to content

Instantly share code, notes, and snippets.

@C4Tutorials
Created April 14, 2013 21:50
Show Gist options
  • Save C4Tutorials/5384383 to your computer and use it in GitHub Desktop.
Save C4Tutorials/5384383 to your computer and use it in GitHub Desktop.
Composite Objects with Movies
//
// C4WorkSpace.m
// Composite Objects Tutorial
//
// Created by Travis Kirton.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace
-(void)setup {
//create 2 movies
C4Movie *mainMovie = [C4Movie movieNamed:@"inception.mov"];
C4Movie *maskedMovie = [mainMovie copy];
//create a mask shape object
C4Shape *maskShape = [C4Shape ellipse:CGRectMake(0, 0, mainMovie.height * 1.5f, mainMovie.height * 1.5f)];
//create a shape that will become the hole
CGMutablePathRef hole = CGPathCreateMutableCopy([C4Shape ellipse:CGRectMake(0, 0, mainMovie.height, mainMovie.height)].path);
//copy our maskShape's path, add the hole to make a donut
CGMutablePathRef donut = CGPathCreateMutableCopy(maskShape.path);
CGFloat diff = (maskShape.height-mainMovie.height)/2.0f;
const CGAffineTransform displacement = CGAffineTransformMakeTranslation(diff,diff);
CGPathAddPath(donut, &displacement, hole);
//reset the mask shape's path to the donut
maskShape.path = donut;
//switch to the even-odd fill rule so the hole is transparent
maskShape.fillRule = FILLEVENODD;
//set the maskShape as the mask for the masked movie
maskedMovie.mask = maskShape;
//move the mask shape to the center of the masked movie
maskShape.center = CGPointMake(mainMovie.width/2,mainMovie.height/2);
//create a copy of the maskedShape's path
C4Shape *whiteShape = [maskShape copy];
//make it white
whiteShape.fillColor = [UIColor whiteColor];
whiteShape.lineWidth = 0.0f;
//center it on the center of the movie
whiteShape.center = maskShape.center;
//add it to the movie (this makes it look like this part of the movie is cut out)
[mainMovie addShape:whiteShape];
//center the main movie to the canvas
mainMovie.center = self.canvas.center;
[mainMovie addMovie:maskedMovie];
[self.canvas addMovie:mainMovie];
//animate the main movie to rotate every 5 seconds (the masked movie will also rotate in with the main movie)
mainMovie.animationOptions = REPEAT;
mainMovie.animationDuration = 5.0f;
mainMovie.rotation = TWO_PI;
//rotate the masked movie (inside the main movie) in the opposite direction
maskedMovie.animationOptions = REPEAT | LINEAR;
maskedMovie.animationDuration = 10.0f;
maskedMovie.rotation = -TWO_PI;
//play and loop both movies
mainMovie.shouldAutoplay = YES;
mainMovie.loops = YES;
maskedMovie.shouldAutoplay = YES;
maskedMovie.volume = 0.0f;
maskedMovie.loops = YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment