使用UISwitch进行动画制作

时间:2011-08-05 02:23:20

标签: iphone objective-c ios 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;
    }
}

我想让切换器在UI元素显示“enterStartingAddress”和“startingAddress”时向下移动动画。

我是iOS编程和Objective-C的新手,所以任何帮助都会受到赞赏。感谢。

1 个答案:

答案 0 :(得分:0)

如果您将界面元素设置为隐藏,则不会执行任何动画,因为它会在动画时隐藏。您要做的是为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;
        }]; 
    }
}