动画按钮两个标签文本更改

时间:2016-01-18 04:27:22

标签: ios objective-c animation

问题在于,当我点击按钮时,两个标签文本将会更改,但我无法在此处显示该动画功能,

CABasicAnimation *rotate =
    [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotate.byValue = @(M_PI*1); // Change to - angle for counter clockwise rotation
    rotate.duration = 0.4;

    [_button.layer addAnimation:rotate
                            forKey:@"myRotationAnimation"];




    CATransition *animation = [CATransition animation];
    animation.duration = 1.0;
    animation.type = kCATransitionFromBottom;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [_name1.layer addAnimation:animation forKey:@"changeTextTransition"];
    [_name2.layer addAnimation:animation forKey:@"changeTextTransition"];

    // Change the text
    _from.text = @"new 222text";

    _to.text = @"new text";

但我希望像这样改变,

enter image description here

当我点击那个箭头按钮时,来自地址的动画将会改变上下动画。你能帮助我,我正在努力,但我想不出来,谢谢你。

2 个答案:

答案 0 :(得分:0)

尝试使用此代码为您的标签设置动画:

- (CAAnimation*)moveup;
{
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"position.y";
    animation.fromValue = @-500;
    animation.toValue = @10;
    animation.duration = 0.25;

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    [label1.layer addAnimation:animation forKey:@"basic"];
    [label2.layer addAnimation:animation forKey:@"basic"];


    label1.layer.position = CGPointMake(0,20);
    label2.layer.position = CGPointMake(0,20);

    return animation;

}

并根据您的需要添加维度参数。 希望这会很好。

您可以循环显示以下代码:

-(CAAnimation*)rotate
{
    CGRect boundingRect = CGRectMake(350, -500, -5,-5);

    CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];
    orbit.keyPath = @"position";
    orbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(boundingRect, NULL));
    orbit.duration =0.5;
    orbit.additive = YES;
    orbit.repeatCount = HUGE_VALF;
    orbit.calculationMode = kCAAnimationPaced;
    orbit.rotationMode = kCAAnimationRotateAuto;

    [btnScan.layer addAnimation:orbit forKey:@"orbit"];
    return orbit;
}

答案 1 :(得分:0)

我找到了一种方法,可以在按钮操作时更改标签的文本,

bool isShown = false;

在按钮操作:

- (IBAction)textChange:(id)sender {

    if (!isShown) {
            from.frame =  CGRectMake(31, 220, 217 , 38);
            to.frame =  CGRectMake(31, 275, 217 , 38);

            [UIView animateWithDuration:0.25 animations:^{
                from.frame =  CGRectMake(31, 275, 217, 38);
                to.frame =  CGRectMake(31, 220, 217 , 38);

            }];
            isShown = true;
        } else {
            [UIView animateWithDuration:0.25 animations:^{
                from.frame =  CGRectMake(31, 220, 217, 38);
                to.frame =  CGRectMake(31, 275, 217, 38);

            }];
            isShown = false;
        }
    }

谢谢。