开发者

iOS fading animation for multiple objects

开发者 https://www.devze.com 2023-03-29 21:22 出处:网络
I have an iOS app in which every time the view is loaded, all of the objects in the view quickly fade in.

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 fi开发者_Go百科ve 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.


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...    
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号