使用 UISwitch 制作动画

发布于 2024-11-28 03:59:02 字数 526 浏览 2 评论 0原文

我希望能够创建使用 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 非常陌生,所以任何帮助将不胜感激。谢谢。

I want to be able to create an animation that is started using a UISwitch. I currently have this code to hide some UI elements when the switch is on:

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

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

And I want the switch to cause an animation to the UI elements to move them down when the "enterStartingAddress" and "startingAddress" is shown.

I am very new to iOS programming and Objective-C so any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄离 2024-12-05 03:59:02

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

If you set the interface elements to hidden right away, no animation will be performed because it will be hidden while it's animating. What you want to do is to animate the alpha property. That is the visibility of the object. Here is an example of animating the frame and the alpha a bit:

-(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;
        }]; 
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文