我在应用程序上有一些图像,我希望图像每天午夜随机变化。
我在下面的代码中尝试过这样做没有成功,但无法确定如何使其正常工作。
- (void)viewDidLoad
{
[super viewDidLoad];
NSDateFormatter *formater = [[NSDateFormatter alloc]init];
[formater setDateFormat:@"EEE dd-MM-yyyy' "];
timeLabel.text = [formater stringFromDate:[NSDate date]];
timerIsCount = NO;
[self updateTimer];
}
-(void)datePic
{
timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(randomCard) userInfo:nil repeats:YES];
}
- (void)updateTimer
{
NSCalendar *calender = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlag = NSDayCalendarUnit;
NSDateComponents *components = [calender components:unitFlag fromDate:0];
int day = [components day];
if (day) {
//[self randomCard];
}
}
答案 0 :(得分:0)
在这里你需要拍摄一系列图像。和一个int值。
您希望每晚都能更改图像,因此您必须设置计时器 这里 Slidingimageview是imageview
NSInteger index = 0;
arrslidingimages = [[NSArray alloc] initWithObjects:@" 01.png",@" 02.png",@" 03.png",@& #34; 04.png",@" 05.png" @" 06.png",@" 07.png",零];
- (void)viewDidAppear:(BOOL)动画{
[super viewDidAppear:animated]; //滑动图像的动画
slideTransition = [CATransition animation];
CATransition * slideTransition; //实例变量
slideTransition.duration = 1.0;
slideTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
slideTransition.type = kCATransitionFade;
slideTransition.delegate = self;
[NSTimer scheduledTimerWithTimeInterval:60 * 60 * 24 target:self selector:@selector(changeImage :) userInfo:nil repeats:YES];
}
- (void)changeImage:(NSTimer *)计时器 {
//更改吊索图像的计时器并设置页面控制器的当前页面
slideTransition.subtype = kCATransitionFromRight; //或kCATransitionFromRight
[_ Slidingimageview.layer addAnimation:slideTransition forKey:nil];
指数++;
if(index == [arrslidingimages count]){
索引= 0; }_Slidingimageview.image = [UIImage imageNamed:[arrslidingimages objectAtIndex:index]];
}
您必须通过以秒为单位查找时间来设置午夜计时器。如果您想要更多随机图像,请在阵列中添加这些图像
注意:现在在代码中我设置了默认值,就像你运行应用程序这个计时器启动时一样(EXA。如果你今天凌晨12:00运行app,那么图像将在明天中午12:00更改)
希望这件事有效