用停止动作摆动UIView(iOS)

时间:2013-03-30 15:35:17

标签: iphone ios objective-c core-animation

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

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){

                                      }];

    }]; 
}

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

请建议修补此代码建议实现所需动画的方法

由于

1 个答案:

答案 0 :(得分:0)

您想要使用关键帧动画。我实际上在我的书(http://www.apeth.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];

当然,这与你想要做的事情不一样,但它应该让你开始。