iOS 多个对象的淡入淡出动画

发布于 2024-11-30 16:41:02 字数 1642 浏览 1 评论 0原文

我有一个 iOS 应用程序,每次加载视图时,视图中的所有对象都会快速淡入。

在我的代码中,我有一个

- (void)fadeInEverything

函数,在这个函数中,我调用五个不同的函数,每个函数上都有一个 NSTimer 的

[self fadeInLabel];


[NSTimer scheduledTimerWithTimeInterval:0.2
                                 target:self
                               selector:@selector(fadeInTextField)
                               userInfo:nil
                                repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4
                                 target:self
                               selector:@selector(fadeInButton)
                               userInfo:nil
                                repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.6
                                 target:self
                               selector:@selector(fadeInTextView)
                               userInfo:nil
                                repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.8
                                 target:self
                               selector:@selector(fadeInFlipsideViewButton)
                               userInfo:nil
                                repeats:NO];

函数

- (void)fadeInTextView
{
    [resultView setAlpha:0];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];

    [resultView setAlpha:1];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
}

我的问题是,我们是否有一种方法可以制作一个带有参数(对象)并淡入淡出 它在,所以我不必有五个几乎相同的功能?这将为我节省大量空间和混乱。

I have an iOS app in which every time the view is loaded, all of the objects in the view quickly fade in.

In my code, I have a

- (void)fadeInEverything

function, and within this function, I call five different functions, with an NSTimer on each of them, so that the objects fade in sequentially, like this:

[self fadeInLabel];


[NSTimer scheduledTimerWithTimeInterval:0.2
                                 target:self
                               selector:@selector(fadeInTextField)
                               userInfo:nil
                                repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4
                                 target:self
                               selector:@selector(fadeInButton)
                               userInfo:nil
                                repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.6
                                 target:self
                               selector:@selector(fadeInTextView)
                               userInfo:nil
                                repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.8
                                 target:self
                               selector:@selector(fadeInFlipsideViewButton)
                               userInfo:nil
                                repeats:NO];

I have a function that looks like this for each object:

- (void)fadeInTextView
{
    [resultView setAlpha:0];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];

    [resultView setAlpha:1];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
}

My question is, would there we a way to make one function that would take a parameter (the object) and fade it in, so I don't have to have five almost identical functions? It would save me a lot of space and clutter.

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

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

发布评论

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

评论(1

烟花易冷人易散 2024-12-07 16:41:02

您可以通过引用传递对象来完成此操作:

-(void)fadeInView:(UIView **)aView { // the ** indicates that this method is expecting a reference
    // your fading code
    // this works, because all view's (UILabels, UITextFields etc. inherit UIView. 
}

并且调用方法可能如下所示:

-(void)callFader {
    [self fadeInView:&mytextView]; // make sure to put the & before the variable so that it's sent as a reference
    // and more fadeInView calls...    
}

You could pass your object by reference to accomplish that:

-(void)fadeInView:(UIView **)aView { // the ** indicates that this method is expecting a reference
    // your fading code
    // this works, because all view's (UILabels, UITextFields etc. inherit UIView. 
}

And the calling method could look like this:

-(void)callFader {
    [self fadeInView:&mytextView]; // make sure to put the & before the variable so that it's sent as a reference
    // and more fadeInView calls...    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文