2012-08-07 55 views
0

經過對這裏的一些研究,我發現了一個在iPhone應用程序中創建圖像幻燈片放映的解決方案。所有工作正常,目前的圖像顯示一個接一個。如何在幻燈片中優雅地過渡圖像?

我的問題是,我可以讓圖像交叉溶解/淡入淡出,而不是隻出現,如果可以,我可以得到一些建議。

我的代碼

.M

} 
int topIndex = 0, prevTopIndex = 1; 
- (void)viewDidLoad 
{ 


imagebottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)]; 
[self.view addSubview:imagebottom]; 

imagetop = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)]; 
[self.view addSubview:imagetop]; 

imageArray = [NSArray arrayWithObjects: 
       [UIImage imageNamed:@"image1.png"], 
       [UIImage imageNamed:@"image2.png"], 
       [UIImage imageNamed:@"image3.png"], 
       [UIImage imageNamed:@"ip2.png"], 
       nil]; 


NSTimer *timer = [NSTimer timerWithTimeInterval:5.0 
             target:self 
             selector:@selector(onTimer) 
             userInfo:nil 
             repeats:YES]; 

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; 
[timer fire]; 

[super viewDidLoad]; 
} 

-(void)onTimer{ 
if(topIndex %2 == 0){ 
    [UIView animateWithDuration:5.0 animations:^ 
    { 
     imagebottom.alpha = 0.0; 
    }]; 
    imagetop.image = [imageArray objectAtIndex:prevTopIndex]; 
    imagetop.image = [imageArray objectAtIndex:topIndex]; 
}else{ 
    [UIView animateWithDuration:5.0 animations:^ 
    { 
     imagetop.alpha = 1.0; 
    }]; 
    imagetop.image = [imageArray objectAtIndex:topIndex]; 
    imagebottom.image = [imageArray objectAtIndex:prevTopIndex]; 
} 
prevTopIndex = topIndex; 
if(topIndex == [imageArray count]-1){ 
    topIndex = 0; 
}else{ 
    topIndex++; 
} 

回答

2

你有一堆的選項。如果你有一個「容器」視圖,你可以創建一個新的UIImageView透明(alpha = 0),然後使用一個UIView動畫塊淡入一個圖像,另一個出來(或者把alpha = 1,例如,你有你的主視圖,self.view,你有一個UIImageView * oldView,現在是rect(0,0,320,100),並且你想把它向右滑動滑動的NewView的ImageView的首先你設置NewView的框架(-320,0,320,100)然後按[self.view addSubview NewView的]爲了動畫的變化:。

[UIView animateWithDuration:2 animations:^ 
    { 
    oldView.frame = CGRectMake(320, 0, 320, 100); 
    newView.frame = CGRectMake(0,0,320, 100); 
    } 
completion:^(BOOL finished) 
    { 
    [oldView removeFromSuperView]; 
    } ]; 

您還可以使用的UIView的

選項
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion 

它給你更多/不同的選項(和它的工作太少!)。例如,使用在第一個例子相同的基本對象,但具有相同的幀oldView的NewView的:

transitionFromView:oldView toView:newView duration:2 options: UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished)) { /*whatever*/}]; 
+0

感謝您的回覆,即時通訊無法理解如何應用到我的代碼,你能就此展開畢竟,第二個版本聽起來不錯 – JSA986 2012-08-08 09:46:23