iphone fading of images

后端 未结 3 1678
猫巷女王i
猫巷女王i 2020-12-09 00:16

wat i want is to show an image in image view and fade the image after 5 seconds and show the next image .the images are stored in an array objects...i was able to make a su

3条回答
  •  生来不讨喜
    2020-12-09 00:27

    @user652878 This answeres your question regarding fading seemlessly from one image to the next:

    Create the following properties in your view controllers header:

    UIImageView *imageViewBottom, *imageViewTop;
        NSArray *imageArray; 
    

    Then add this code to your implimentaion file:

    int topIndex = 0, prevTopIndex = 1; 
    - (void)viewDidLoad {
    
        imageViewBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
        [self.view addSubview:imageViewBottom]; 
    
        imageViewTop = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
        [self.view addSubview:imageViewTop];
    
        imageArray = [NSArray arrayWithObjects:  
                      [UIImage imageNamed:@"lori.png"],
                      [UIImage imageNamed:@"miranda.png"],
                      [UIImage imageNamed:@"taylor.png"],
                      [UIImage imageNamed:@"ingrid.png"],
                      [UIImage imageNamed:@"kasey.png"], 
                      [UIImage imageNamed:@"wreckers.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:^
            {
                imageViewTop.alpha = 0.0;
            }]; 
            imageViewTop.image = [imageArray objectAtIndex:prevTopIndex];
            imageViewBottom.image = [imageArray objectAtIndex:topIndex];
        }else{
            [UIView animateWithDuration:5.0 animations:^
             {
                 imageViewTop.alpha = 1.0;
             }];    
            imageViewTop.image = [imageArray objectAtIndex:topIndex];
            imageViewBottom.image = [imageArray objectAtIndex:prevTopIndex];
        }
        prevTopIndex = topIndex; 
        if(topIndex == [imageArray count]-1){
            topIndex = 0; 
        }else{
            topIndex++; 
        }
    }
    

提交回复
热议问题