2014-03-14 49 views

回答

2

你可以設置UIInterpolatingMotionEffectx和y協調​​的幫助。我只是在我的演示測試一個代碼,我把這個在這裏檢查: -

- (void)viewDidLoad 
{ 
     [super viewDidLoad]; 
     CGRect frame = self.view.bounds; 
     for (NSInteger i=0; i<1; i++) { 
      CGFloat left = (frame.size.width-100)/2+i%2*10; 
      CGFloat top = (frame.size.height-100)/2+i%3*10; 
      UIImageView *demoView = [[UIImageView alloc] initWithFrame:CGRectMake(left, top , 100, 100)]; 

      [demoView setImage:[UIImage imageNamed:@"FA4nH.jpg"]]; 

      // 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]; 
     } 

} 

入住這對這項工作我測試這個漂亮的在我的演示項目的一個真實的設備。希望對你有所幫助。