创建一个像打字机一样的字符串动画

时间:2013-11-07 19:57:35

标签: objective-c animation nsstring ios7

我会像打字机一样逐个字符地创建一个NSString动画。该字符串将放在UILabel上。有可能吗?如果是,怎么样?

提前致谢。

更新

woz的方法效果很好,但我不能用它来解决我的问题。我会试着解释一下我的情况。在第一个视图的应用程序中,我将显示实际位置,因此我添加了以下方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.longitude];
        latitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.latitude];
    }

    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        //NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            [address sizeToFit];

            address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                            [self sanitizedDescription:placemark.thoroughfare],
                            [self sanitizedDescription:placemark.subThoroughfare],
                            [self sanitizedDescription:placemark.postalCode],
                            [self sanitizedDescription:placemark.locality],
                            [self sanitizedDescription:placemark.country]];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

- (NSString *)sanitizedDescription:(NSString *)obj
{
    if (obj == nil)
    {
        obj = @"...";
        return obj;
    }
    return obj;
}

现在,我会在调用if (obj == nil)上的- (NSString *)sanitizedDescription:(NSString *)obj时使用打字机动画。我该怎么办?

抱歉,我正在开始使用Obj-C :(

1 个答案:

答案 0 :(得分:2)

这是一些可以帮助您入门的代码。我正在使用NSTimer定期向UILabel添加一个字符。

- (void)viewDidLoad {

    NSTimer *typingTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
        target:self
        selector:@selector(typeALetter:)
        userInfo:nil
        repeats:YES];

    NSString *stringToType = @"The quick brown fox jumps over the lazy dog.";
    NSUInteger index = 0;
}

- (void)typeALetter:(id)sender {

    theLabel.text = [theLabel.text stringByAppendingFormat:@"%@", [stringToType substringWithRange:NSMakeRange(index, 1)]];

    if (index < stringToType.length) {
        index++;
    }
    else {
        [typingTimer invalidate];
    }    

}