繁体   English   中英

使用UISwitch制作动画

[英]Animating with UISwitch

我希望能够创建使用UISwitch启动的动画。 当前,当开关打开时,我有以下代码可以隐藏一些UI元素:

-(IBAction)displayStartingAddress:(id)sender{
    NSLog(@"starting displayStartingAddress");
    if (enterStartingAddress.hidden==YES) {
        enterStartingAddress.hidden=NO;
        startingAddress.hidden=NO;

    }else{
        enterStartingAddress.hidden=YES;
        startingAddress.hidden=YES;
    }
}

我希望该开关在显示“ enterStartingAddress”和“ startingAddress”时使UI元素的动画向下移动。

我对iOS编程和Objective-C还是陌生的,所以将不胜感激。 谢谢。

如果立即将界面元素设置为“隐藏”,则将不执行任何动画,因为它在制作动画时会被隐藏。 您要做的是为alpha属性设置动画。 那就是物体的可见性。 这是使帧和Alpha动画的示例:

-(IBAction)displayStartingAddress:(id)sender{
    CGRect frame1 = enterStartingAddress.frame;
    CGRect frame2 = startingAddress.frame;

    if (enterStartingAddress.alpha==0) { //not the best way to do it to be honest
        frame1.origin.y += 100;
        frame2.origin.y += 100;
        [UIView animateWithDuration:0.5 animations:^{  
             enterStartingAddress.alpha = 1;
             startingAddress = 1;
             enterStartingAddress.frame = frame1;
             startingAddress.frame = frame2;
        }];    
    }

    else{
        frame1.origin.y -= 100;
        frame2.origin.y -= 100;
        [UIView animateWithDuration:0.5 animations:^{  
             enterStartingAddress.alpha = 0;
             startingAddress = 0;
             enterStartingAddress.frame = frame1;
             startingAddress.frame = frame2;
        }]; 
    }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM