加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – iphone淡化图像

发布时间:2020-12-14 20:01:37 所属栏目:百科 来源:网络整理
导读:我想要的是在图像视图中显示图像并在5秒后淡化图像并显示下一个图像.图像存储在数组对象中…我能够在5秒后在图像之间成功转换.我想要的是在5秒后淡出第一个图像并显示下一个在无限循环中发生…下面是代码,它对图像之间的正常过渡非常有用..我想要的是图像的
我想要的是在图像视图中显示图像并在5秒后淡化图像并显示下一个图像.图像存储在数组对象中…我能够在5秒后在图像之间成功转换.我想要的是在5秒后淡出第一个图像并显示下一个在无限循环中发生…下面是代码,它对图像之间的正常过渡非常有用..我想要的是图像的褪色…并在下面的代码中添加淡入过渡

- (void)viewDidLoad {

    imgView.animationImages=[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];


    imgView.animationDuration=20.0;
    imgView.animationRepeatCount=0;
    [imgView startAnimating];
    [self.view addSubview:imgView];
    [imgView release];



    [super viewDidLoad];
}

解决方法

您可以使用重复的NSTimer来调用一种特殊的方法,该方法可以连续地将图像视图的alpha属性设置为0.0到1.0.以下是我编写的一些代码:

- (void)viewDidLoad {
    imgView.animationImages=[NSArray arrayWithObjects:  
                             [UIImage imageNamed:@"lori.png"],nil];



    imgView.animationDuration=20.0;
    imgView.animationRepeatCount=0;
    [imgView startAnimating];
    [self.view addSubview:imgView];
    [imgView release];
    //The timers time interval is the imageViews animation duration devided by the number of images in the animationImages array. 20/5 = 4 
    NSTimer *timer = [NSTimer timerWithTimeInterval:4.0 
                                              target:self 
                                            selector:@selector(onTimer) 
                                            userInfo:nil 
                                             repeats:YES];

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

    [super viewDidLoad];
}

//It is important that the animation durations within these animation blocks add up to 4 
//(the time interval of the timer). If you change the time interval then the time intervals 
//in these blocks must also be changed to refelect the amount of time an image is displayed. 
//Failing to do this will mean your fading animation will go out of phase with the switching of images.   

-(void)onTimer{
        [UIView animateWithDuration:3.0 animations:^{
            imgView.alpha = 0.0; 
        }];
        [UIView animateWithDuration:1.0 animations:^{
            imgView.alpha = 1.0; 
        }];
    }

如果希望淡入淡出持续时间持续5秒而不是4秒,则需要将图像视图animationDuration属性增加到25,然后增加两个块中的淡入淡出动画持续时间,使得淡入淡出时间= 5.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读