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++;
}
4

1 に答える 1

2

たくさんのオプションがあります。「コンテナ」ビューがある場合は、新しい UIImageView を透明 (アルファ = 0) にしてから、UIView アニメーション ブロックを使用して一方の画像をフェードインし、もう一方の画像をフェードアウトします (または両方でアルファ = 1 のままにして、一方をスライドさせます)。あなたが望む側。

たとえば、メイン ビューである self.view があるとします。UIImageView *oldView が 1 つあり、現在は 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

これにより、より多くの/異なるオプションが提供されます(そして、その作業も少なくなります!)。たとえば、最初の例と同じ基本オブジェクトを使用しますが、newView は oldView と同じフレームを持ちます。

transitionFromView:oldView toView:newView duration:2 options: UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished)) { /*whatever*/}];
于 2012-08-07T23:24:27.920 に答える