-2

我会像打字机一样逐个字符地制作一个 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)on时使用打字机动画。- (NSString *)sanitizedDescription:(NSString *)obj我能怎么做?

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

4

1 回答 1

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];
    }    

}
于 2013-11-07T20:14:19.140 回答