Skip to content

Instantly share code, notes, and snippets.

@darcyliu
Created August 24, 2013 04:38
Show Gist options
  • Save darcyliu/6326095 to your computer and use it in GitHub Desktop.
Save darcyliu/6326095 to your computer and use it in GitHub Desktop.
UIMotionEffect Demo
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = self.view.bounds;
for (NSInteger i=0; i<10; i++) {
CGFloat left = (frame.size.width-100)/2+i%2*10;
CGFloat top = (frame.size.height-100)/2+i%3*10;
UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(left, top , 100, 100)];
demoView.backgroundColor = [UIColor colorWithRed:i/12.0f green:i/18.0f blue:i/24.0f alpha:0.8];
[self.view addSubview:demoView];
UIInterpolatingMotionEffect *xAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
UIInterpolatingMotionEffect *yAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
left += i*50;
top += i *50;
xAxis.minimumRelativeValue = @(-left);
xAxis.maximumRelativeValue = @(left);
yAxis.minimumRelativeValue = @(-top);
yAxis.maximumRelativeValue = @(top);
UIMotionEffectGroup *motionEffectGroup = [[UIMotionEffectGroup alloc] init];
motionEffectGroup.motionEffects = @[xAxis, yAxis];
[demoView addMotionEffect:motionEffectGroup];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment