简体   繁体   中英

drawing NSString using drawRect and animate it

I am drawing a NSString using drawRect and is it possible for me to animate this NSString once it has been drawn? I want it to fade in/out after 3.0 seconds. How can I do so?

You cannot directly fade the drawn string itself. You can fade layers and you can fade views. Draw the string onto one of these and fade the layer or view. This is generally most easily done with a UILabel or CATextLayer , but you can draw onto your own layer and fade that if you prefer.

Make a UILabel, then use animation to perform your desires

- (void)faceInLabel{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    label.text = @"My String";
    [myView addSubview:label];
    label.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        label.alpha = 1;
    }
    completion:^(BOOL finished)
     {
         [NSTimer timerWithTimeInterval:3 target:self selector:@selector(fadeOutLabel) userInfo:nil   repeats:NO];
      }];
}
- (void)fadeOutLabel
{
    [UIView animateWithDuration:0.5 animations:^{
        label.alpha = 0;
    }];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM