我想向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){

    }];

当我运行代码时,视图仍然没有晃动。为什么?

更新

我必须使用关键帧动画,因为视图摇晃后,它需要移动到另一个地方然后摇晃然后停止。这是我编辑的代码,但仍然无法使用。为什么?
    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:

注意: frame属性不能直接设置动画。相反,您应该为boundsanchorPointposition属性的适当组合设置动画,以实现所需的结果。

在您的示例中,无论如何您只是在改变位置,因此即使可以对框架进行动画处理,也最好对位置进行动画处理。

其他备注

价值

除了[NSValue value:&rect withObjCType:@encode(CGRect)]之外,我更喜欢专用的[NSValue valueWithCGRect:rect](在这种情况下为[NSValue valueWithCGPoint:point])

动画块

正如评论中已经说过的那样:仅在执行隐式动画时,才应组合使用Core Animation和Animation Block(因为否则它们将禁用视图)。您正在做的是一个显式动画,因此无需这样做。
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/

10-10 18:33
查看更多