0

我需要一个带有振荡动画的视图来进入屏幕,最后,动画应该以自然的方式停止(减少振荡 - 钟摆效果)。我在屏幕上方添加了子视图,以便视图在需要时旋转到屏幕中。添加子视图的代码是:

myView.layer.anchorPoint = CGPointMake(1.0, 0.0);

[[self view] addSubview:myView];
[myView setHidden:YES];
// Rotate 75 degrees to hide it off screen
CGAffineTransform rotationTransform = CGAffineTransformIdentity;
rotationTransform = CGAffineTransformRotate(rotationTransform, DEGREES_RADIANS(75));
bannerView.transform = rotationTransform;
bannerView.center = CGPointMake(((self.view.bounds.size.width)/2.0), -5.0);

[self performSelector:@selector(animateSwing) withObject:nil afterDelay:3.0];

我试图实现这一点的方式是,视图应该旋转一个完整的半圆并向后旋转,然后旋转一个半圆,最后使用 EaseOut 动画曲线在所需的点停止。我的方法的代码animateSwing()如下:

- (void)animateSwing {
     NSLog(@"ANIMATING");
    [myView setHidden:NO];
    CGAffineTransform swingTransform = CGAffineTransformIdentity;
    swingTransform = CGAffineTransformRotate(swingTransform, DEGREES_RADIANS(-20));


    [UIView animateWithDuration:0.30
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     [UIView setAnimationRepeatCount:1.5];
                     [UIView setAnimationRepeatAutoreverses:YES];  
                     myView.transform = swingTransform;

                 }completion:^(BOOL finished){
                     [UIView animateWithDuration:0.10
                                           delay:0.0
                                         options:UIViewAnimationOptionCurveEaseOut
                                      animations:^{
                                          myView.transform = CGAffineTransformMakeRotation(DEGREES_RADIANS(0));
                                      }completion:^(BOOL Finished){

                                      }];

    }]; 
}

由于某种原因,上面的代码不起作用。如果我不链接动画,代码将执行半圆例程。但是,如果我像上面那样链接动画,它只会在所需点周围振荡一点,然后突然结束。

请建议对此代码的修复建议一种实现所需动画的方法

谢谢

4

1 回答 1

0

您想使用关键帧动画。我的书中实际上有一个“减少摇摆”动画的例子(http://www.aeth.com/iOSBook/ch17.html#_keyframe_animation):

CompassLayer* c = (CompassLayer*)self.compass.layer;
NSMutableArray* values = [NSMutableArray array];
[values addObject: @0.0f];
int direction = 1;
for (int i = 20; i < 60; i += 5, direction *= -1) { // alternate directions
    [values addObject: @(direction*M_PI/(float)i)];
}
[values addObject: @0.0f];
CAKeyframeAnimation* anim =
    [CAKeyframeAnimation animationWithKeyPath:@"transform"];
anim.values = values;
anim.additive = YES;
anim.valueFunction =
    [CAValueFunction functionWithName: kCAValueFunctionRotateZ];
[c.arrow addAnimation:anim forKey:nil];

当然,这与您尝试做的不同,但它应该让您开始。

于 2013-03-30T15:48:25.583 回答