iOS开发UIMotionEffect运动视觉效果

本文介绍iOS 7中引入的UIMotionEffect特性,包括倾斜和偏移效果实现方法,并提供了一个简单的示例代码来展示如何为UIView添加动态视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、UIMotionEffect简介

  在iOS7.0推出了UIMotionEffect运动视觉效果,就是从屏幕偏移不同角度、看到的效果不同!

NS_CLASS_AVAILABLE_IOS(7_0)
@interface UIMotionEffect : NSObject <NSCopying, NSCoding>
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
//观察者的角度偏移viewerOffset,获取运动视觉效果的各项属性和值
- (nullable NSDictionary<NSString *, id> *)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset;
@end

typedef NS_ENUM(NSInteger, UIInterpolatingMotionEffectType) {
    UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis,//X轴
    UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis//Y轴
};

@interface UIInterpolatingMotionEffect : UIMotionEffect
- (instancetype)initWithKeyPath:(NSString *)keyPath type:(UIInterpolatingMotionEffectType)type NS_DESIGNATED_INITIALIZER;//初始化
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

@property (readonly, nonatomic) NSString *keyPath;//获取角度偏移
@property (readonly, nonatomic) UIInterpolatingMotionEffectType type;//获取类型
@property (nullable, strong, nonatomic) id minimumRelativeValue;//最小角度偏移
@property (nullable, strong, nonatomic) id maximumRelativeValue;//最大角度偏移
@end

@interface UIMotionEffectGroup : UIMotionEffect
@property (nullable, copy, nonatomic) NSArray<__kindof UIMotionEffect *> *motionEffects;//添加水平和垂直效果添加到对应UI上
@end

 

2、简单使用

- (void)addEffectWithOffset:(NSInteger)offset withView:(UIView *)view{
    UIInterpolatingMotionEffect *effectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    effectX.minimumRelativeValue = @(-offset);
    effectX.maximumRelativeValue = @(offset);
    
    UIInterpolatingMotionEffect *effectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    effectY.minimumRelativeValue = @(-offset/2);
    effectY.maximumRelativeValue = @(offset/2);
    
//    UIMotionEffectGroup *group = [[UIMotionEffectGroup alloc] init];
//    group.motionEffects = @[effectX,effectY];
    view.motionEffects = @[effectX,effectY];
}

 

转载于:https://www.cnblogs.com/xianfeng-zhang/p/8926538.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值