Добавить эффект перехода в слайд-шоу - PullRequest
0 голосов
/ 18 марта 2012

Я пытаюсь добавить эффект перехода в слайд-шоу: вот мой код

- (void)viewDidLoad
{
[super viewDidLoad];
photos = [[NSMutableArray alloc] init];

NSXMLParser *photoParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL 
URLWithString:@"http://localhost/index.xml"]];
[photoParser setDelegate:(id)self];
[photoParser parse];

currentImage = 0;

NSURL *imageURL = [NSURL URLWithString:[photos objectAtIndex:0]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
[imgView setImage:[UIImage imageWithData:imageData]];

timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
                                         target: self
                                       selector: @selector(handleTimer:)
                                       userInfo: nil
                                        repeats: YES];
}

- (void) handleTimer: (NSTimer *) timer {
currentImage++;
if ( currentImage >= photos.count )
    currentImage = 0;

NSURL *imageURL = [NSURL URLWithString:[photos objectAtIndex:currentImage]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
[imgView setImage:[UIImage imageWithData:imageData]];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
attributes:(NSDictionary *)attributeDict { 
if ( [elementName isEqualToString:@"photo"]) {
    [photos addObject:[attributeDict objectForKey:@"url"]];
}
}

Я думаю, что я должен использовать

[UIView transitionFromView:currentView toView:nextView duration:1.0 
options:UIViewAnimationOptionTransitionCrossDissolve completion:nil];

, но я не могу заполнитьtransitionFRomView: и toView: поля.У меня только один взгляд

Спасибо за вашу помощь

1 Ответ

0 голосов
/ 18 марта 2012

Вы можете использовать метод transitionWithView:duration:options:animations:completion:, для которого в документации написано:

Вы можете использовать этот блок для добавления, удаления, отображения или скрытия подпредставлений указанного представления.

Это пример того, как вы можете использовать этот метод:

- (void) handleTimer: (NSTimer *) timer {
    currentImage++;
    if (currentImage >= photos.count) currentImage = 0;

    NSURL *imageURL = [NSURL URLWithString:[photos objectAtIndex:currentImage]];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL]
   [UIView transitionWithView:containerView
       duration:2
       options:UIViewAnimationOptionTransitionFlipFromLeft
       animations:^{
           [imgView removeFromSuperview];
           imgView = // create a new image view
           [imgView setImage:[UIImage imageWithData:imageData]];
           [containerView addSubview:toView];
       } completion:NULL];
}

Где containerView - родительское представление imgView, и, наиболее вероятно, self.view.

...