Unable to run animation continuously in Xcode

Asked 2 years ago, Updated 2 years ago, 40 views

In Xcode, we create an animation in which the updated number moves up and down every time the number increases.

http://qiita.com/noppefoxwolf/items/32530bb5f011ac2c9c91

I made it by referring to this site, but
If you count the numbers from 0 to 100 in the for statement, the first 0 is followed by the last 100 in the for statement, and
Animated only once, 0 → 100.

If you look at the log and breakpoint, the count is increasing by 1 from 0 to 100, and
The numbers are updated and animated after the loop is over.

I want to loop 100 times and count them in order, what should I do?
Thank you for your cooperation.

-(void)viewDidLoad{
    label = [[UILabel alloc] init];
    label.frame = CGRectMake(100,190,100,50);
    label.backgroundColor=[UIColor yellowColor];
    label.textColor=[UIColor blackColor];
    label.font = [UIFont fontWithName:@"AppleGothic" size:22];
    label.text = [NSString stringWithFormat: @ "%d", count ];
    myView addSubview:label;

   for (count = 0; count <100; count++) {

        Anim = CATransition animation;
        Anim setType:kCATransitionMoveIn;
        Anim setSubtype: kCATransitionFromBottom;
        Anim setDuration: 0.2f;
        coinlabel.layer addAnimation:Anim forKey:nil;
        label.text = [NSString stringWithFormat: @ "%d", count ];
    }
}

ios objective-c xcode

2022-09-30 10:39

1 Answers

I saw the reference site and sample code.

First of all, the reference site only considers one-time execution, and the code is not programmed to repeat.

If you look at the sample code based on that assumption, you can see that the animation is registered in the for statement all at once, so 100 animations are registered at the same time.
As a result, only the last animation registered has been run.

Therefore, it must be implemented in some way as to register the next animation after the previous animation has finished.

The following is an example of a sample code taken over as much as possible.

int_count=0;// Variables for numbers to display on labels
int_maxCount = 100;// upper limit to count up

- (void) viewDidLoad{
    superviewDidLoad;

    UILabel*label=[[UILabel alloc] init];
    label.frame = CGRectMake(100,190,100,50);
    label.backgroundColor=[UIColor yellowColor];
    label.textColor=[UIColor blackColor];
    label.font = [UIFont fontWithName:@"AppleGothic" size:22];
    label.text = [NSString stringWithFormat: @"%d",_count];
    self.view addSubview:label;

    label.tag = 100; // Set the tag for extraction by another method

    [self addTransitionToLabel:label withCount:_count]; // Add Animation
}

/*
 *  CAAnimation (parent class of CATransition) delivery method
 *  An animation designating the class at the destination is called when the animation is finished.
 */
- (void) animationDidStop: (CAAnimation*) animation finished: (BOOL) flag {
    UILabel*label=(UILabel*) [self.viewviewWithTag:100]; // Get the label with the tag

    // Determine if the stopped animation was added to label.layer
    // (CAT transition is automatically assigned as a key.
    // key transition to remove animation from label.layer)
    if(anim==[label.layer animationForKey:@"transition"){

        Remove previous animation from //label.layer
        [ label.layer removeAnimationForKey:@"transition";

        _count++;// Increment the count here

        if(_count>_maxCount){
            // After finishing the animation until _maxCount, finish the method without adding the next animation
            return;
        }

        // Register the next animation
        [self addTransitionToLabel:label withCount:_count];
    }
}

/*
 *  Making animation registration part a different method
 */
- (void) addTransitionToLabel: (UILabel*) label withCount: (int) count {
    CATransition* Anim= CATransition animation;
    Anim setType:kCATransitionMoveIn;
    Anim setSubtype: kCATransitionFromBottom;
    Anim setDuration: 0.2f;

    Anim setDelegate:self; // Specify self (this class) as the destination for CAAnimation
    Anim setRemovedOnCompletion: NO; // Do not allow automatic deletion to be determined by animationDidStop

    label.layer addAnimation:Anim forKey:nil;
    label.text = [NSString stringWithFormat: @ "%d", count ];
}

In this example, the animation runs immediately after the screen is displayed, but you can run it again at any time by running addTransitionToLabel:withCount:.

There are some parts of the code that should not be written because it is an example code.Rewrite as appropriate if it is actually used.


2022-09-30 10:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.