在幻灯片中添加过渡效果

时间:2012-03-17 22:57:22

标签: xcode ios5 slideshow effects photo-gallery

我正在尝试为幻灯片添加过渡效果:这是我的代码

- (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 :(得分:0)

您可以使用documentation所说的方法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];
}

其中containerViewimgView的父视图,最有可能是self.view