2

我想为 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"];
4

3 回答 3

5

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


CALayer 上的 frame 属性的文档中

注意:frame属性不能直接设置动画。bounds相反,您应该对,anchorPoint和属性的适当组合进行动画处理,position以实现所需的结果。

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

其他备注

NSValue

而不是[NSValue value:&rect withObjCType:@encode(CGRect)]我更喜欢专业的 [NSValue valueWithCGRect:rect](或[NSValue valueWithCGPoint:point]在这种情况下)

动画块

正如评论中已经说过的:当你做隐式动画时,你应该只结合核心动画和动画块(因为它们是禁用视图的)。你正在做的是一个明确的动画,所以没有必要这样做。

“钥匙”在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
于 2013-05-30T10:59:34.227 回答
1

试试这个代码

CABasicAnimation *theRotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[theRotateAnimation setDuration:0.2f];
theRotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[theRotateAnimation setRepeatCount:CGFLOAT_MAX];
[theRotateAnimation setAutoreverses:YES];
[theRotateAnimation setFromValue:[NSNumber numberWithFloat:(- 2 * (M_PI / 180))]];
[theRotateAnimation setToValue:[NSNumber numberWithFloat:(2 * (M_PI / 180))]];
[self.boView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
[self.boView.layer addAnimation:theRotateAnimation forKey:@"RotateAnimation"];

这肯定会动摇你的看法。

于 2013-05-30T09:11:15.960 回答
0

试试这个,摇动视图动画的简单代码

CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
    [shake setDuration:0.2];
    [shake setRepeatCount:4];
    [shake setAutoreverses:YES];
    [shake setFromValue:[NSValue valueWithCGPoint:
                         CGPointMake(View.center.x - 5,View.center.y)]];
    [shake setToValue:[NSValue valueWithCGPoint:
                       CGPointMake(View.center.x + 5, View.center.y)]];
    [View.layer addAnimation:shake forKey:@"position"];
于 2013-05-30T09:13:41.930 回答