gpt4 book ai didi

ios - 关键帧动画失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:55 26 4
gpt4 key购买 nike

我想给 UIView 添加震动效果,但是我失败了。这是我的代码

    CGRect rectL=CGRectMake(473, 473, 88, 88);
CGRect rectR=CGRectMake(493, 473, 88, 88);
NSValue *valueL = [NSValue value: &rectL
withObjCType:@encode(CGRect)];
NSValue *valueR = [NSValue value: &rectR
withObjCType:@encode(CGRect)];
NSArray *firstArray=@[valueL,valueR,valueL,valueR,valueL,valueR,valueL,valueR];

[UIView animateWithDuration:2 animations:^{
CAKeyframeAnimation * theAnimation;

// Create the animation object, specifying the position property as the key path.
theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"frame"];
theAnimation.values=firstArray;
theAnimation.duration=5.0;
theAnimation.calculationMode= kCAAnimationLinear;
// Add the animation to the layer.
[self.boView.layer addAnimation:theAnimation forKey:@"frame"];//boView is an outlet of UIView
}completion:^(BOOL finished){

}];

当我运行代码时, View 静止不动,没有晃动。为什么?

更新

我必须使用关键帧动画,因为 View 晃动后,它需要移动到另一个地方然后晃动然后停止。这是我编辑的代码,但它仍然不起作用。为什么?

    CGRect rectL=CGRectMake(463, 473, 88, 88);
CGRect rectR=CGRectMake(503, 473, 88, 88);
NSValue *valueL = [NSValue value: &rectL
withObjCType:@encode(CGRect)];
NSValue *valueR = [NSValue value: &rectR
withObjCType:@encode(CGRect)];
NSArray *firstArray=@[valueL,valueR,valueL,valueR,valueL,valueR,valueL,valueR];



CAKeyframeAnimation * theAnimation;

// Create the animation object, specifying the position property as the key path.
theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"frame"];
theAnimation.values=firstArray;
theAnimation.duration=5.0;
theAnimation.calculationMode= kCAAnimationLinear;
// Add the animation to the layer.
[self.boView.layer addAnimation:theAnimation forKey:@"frame"];

最佳答案

tl;dr:frame 属性不能直接设置动画。


来自 the documentation of the frame property on CALayer :

Note: The frame property is not directly animatable. Instead you should animate the appropriate combination of the bounds, anchorPoint and position properties to achieve the desired result.

在您的示例中,您只是改变了位置,所以即使您可以为框架设置动画,也最好为位置设置动画。

其他备注

NS 值

我更喜欢专门的[NSValue valueWithCGRect:rect](或[NSValue valueWithCGPoint:point] 在这种情况下)

动画 block

正如评论中已经说过的那样:在执行隐式动画时,您应该只组合核心动画和动画 block (因为它们在 View 中是禁用的)。您正在做的是显式动画,因此没有必要这样做。

addAnimation:forKey:中的“键”

一种常见的误解是,您在添加动画时传递的键是您要更改的属性。事实并非如此。 keyPath 已在动画上配置。

该键用于让您能够使用 animationForKey: 向图层请求已添加的动画:如果您打算向图层请求它,我建议您给它更多描述性字符串。否则你可以传递 nil


修改后的代码

我已经根据这些评论修改了你的代码

CGPoint leftPoint  = CGPointMake(473, 473);
CGPoint rightPoint = CGPointMake(493, 473);

NSValue *leftValue = [NSValue valueWithCGPoint:leftPoint];
NSValue *rightValue = [NSValue valueWithCGPoint:rightPoint];
NSArray *firstArray = @[leftValue, rightValue,
leftValue, rightValue,
leftValue, rightValue,
leftValue, rightValue];

CAKeyframeAnimation * theAnimation;

// Create the animation object, specifying the position property as the key path.
theAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
theAnimation.values = firstArray;
theAnimation.duration = 5.0;
theAnimation.calculationMode = kCAAnimationLinear;
// Add the animation to the layer.
[self.boView.layer addAnimation:theAnimation
forKey:@"moveBackAndForth"]; //boView is an outlet of UIView

关于ios - 关键帧动画失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16831562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com