animationWithKeyPath键值对

本文介绍了如何使用animationWithKeyPath来实现各种动画效果,包括比例转换、旋转、透明度变化等,并提供了详细的属性说明。

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

 

animationWithKeyPath键值对的方式来改变动画

<Jacky Shin:可以从这个网址查到哪些可以做为动画效果, 打开xcode帮助,搜索animatable properties,就可以看到列表>

animationWithKeyPath的值:

  transform.scale = 比例轉換

    transform.scale.x = 闊的比例轉換

    transform.scale.y = 高的比例轉換

    transform.rotation.z = 平面圖的旋轉

    opacity = 透明度

    margin

    zPosition

    backgroundColor    背景颜色

    cornerRadius    圆角

    borderWidth

    bounds

    contents

    contentsRect

    cornerRadius

    frame

    hidden

    mask

    masksToBounds

    opacity

    position

    shadowColor

    shadowOffset

    shadowOpacity

    shadowRadius

 

转载于:https://www.cnblogs.com/haohao-developer/p/5316868.html

### 关于 `AnimatableExtend` 的介绍 在 iOS 开发中,动画效果对于提升用户体验至关重要。苹果官方提供了多种方式来实现平滑过渡和动态变化的效果[^1]。 #### 动画属性扩展机制 为了增强视图及其子类的可动特性,开发者可以通过创建自定义的 animatable 属性来进行更灵活的操作。这通常涉及到重写 CALayer 中的关键路径方法 `_animateValue(forKey:)` 或者通过声明新的关联键值并指定其为 animatable 类型。然而,“AnimatableExtend”并不是标准库中的术语或预定义组件;相反,它可能是指一种模式或是第三方库的名字,在此背景下理解该概念时需要注意上下文环境[^2]。 如果指的是如何让某些特定属性变得可被 Core Animation 支持,则可以考虑如下方案: - **Swift 实现** 对于 Swift 来说,要使某个新加入的属性能够参与动画过程,最常见的方式之一就是利用 @objc 和 dynamic 关键字修饰符,并确保所操作的对象继承自 NSObject 协议以便支持 KVC/KVO 机制。 ```swift class CustomView: UIView { @objc dynamic var customProperty: CGFloat = 0.0 override class func initialize() { if self === CustomView.self { let keyPath = "customProperty" CATransaction.begin() CATransaction.setDisableActions(true) layer.addKeyframeAnimation( forKey: keyPath, values: nil, keyTimes: nil, timingFunctions: nil, duration: 0.3 ) CATransaction.commit() } } // MARK: - Override animation methods to support the new property override public class func needsDisplay(forKey key: String) -> Bool { return key == "customProperty" ? true : super.needsDisplay(forKey: key) } override public func action(for layer: CALayer, forKey event: String) -> CAAction? { guard event == "customProperty" else { return super.action(for: layer, forKey: event)} let animation = CABasicAnimation(keyPath: event) animation.fromValue = presentation()?.value(forKey: event) animation.toValue = value(forKey: event) return animation } } ``` - **Objective-C 实现** 如果是在 Objective-C 环境下工作的话,同样也可以采用相似的方法去注册一个新的 animatable 属性给定一个UIView/NSView 子类: ```objectivec @interface CustomView () @property (nonatomic, assign) CGFloat customProperty; + (BOOL)_needsDisplayForKey:(NSString *)key; - (id<CAAction>)_defaultActionForLayer:(CALayer *)layer forKey:(NSString *)event; @end static void *CustomPropertyKey = &CustomPropertyKey; @implementation CustomView + (void)load { [self registerAnimations]; } + (void)registerAnimations { NSString *keyPath = @"customProperty"; [CATransaction begin]; [CATransaction setDisableActions:YES]; [self.layerClass addKeyframeAnimationForKey:keyPath withValues:nil times:nil functions:nil duration:0.3f]; [CATransaction commit]; } + (BOOL)_needsDisplayForKey:(NSString *)key { return [key isEqualToString:@"customProperty"] ? YES : [super _needsDisplayForKey:key]; } - (id<CAAction>)_defaultActionForLayer:(CALayer *)layer forKey:(NSString *)event { if ([event isEqualToString:@"customProperty"]) { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:event]; animation.fromValue = [(CALayer *)[self presentationLayer] valueForKey:event]; animation.toValue = [self valueForKey:event]; return animation; } else { return [super _defaultActionForLayer:layer forKey:event]; } } @end ``` 上述代码展示了怎样在一个自定义控件里增加了一个名为 `customProperty` 的 animatable 特性,允许这个数值的变化能够在界面上表现为流畅的视觉转换效果。值得注意的是实际项目开发过程中应当依据具体需求调整细节部分以适应不同场景下的应用逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值