您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 7 自帶的傾斜晃動效果實作

編輯:iOS 開發綜合

iOS 7 提供了 UIInterpolatingMotionEffect,可以讓畫面中的視圖隨裝置傾斜而產生立體的位移感。做法很簡單:分別針對水平與垂直軸建立兩個 motion effect,再掛到目標視圖上即可。

- (void)registerEffectForView:(UIView *)aView depth:(CGFloat)depth;
{
    UIInterpolatingMotionEffect *effectX;
    UIInterpolatingMotionEffect *effectY;

    effectX = [[UIInterpolatingMotionEffect alloc]
        initWithKeyPath:@"center.x"
                   type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    effectY = [[UIInterpolatingMotionEffect alloc]
        initWithKeyPath:@"center.y"
                   type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];

    effectX.maximumRelativeValue = @(depth);
    effectX.minimumRelativeValue = @(-depth);
    effectY.maximumRelativeValue = @(depth);
    effectY.minimumRelativeValue = @(-depth);

    [aView addMotionEffect:effectX];
    [aView addMotionEffect:effectY];
}

其中 depth 參數決定位移幅度,數值越大,視圖隨傾斜移動的範圍就越明顯。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved