gpt4 book ai didi

ios - 使用 NSTimer 切换图像

转载 作者:行者123 更新时间:2023-11-28 18:10:15 25 4
gpt4 key购买 nike

我有几张图片想切换。我为此做了一个简单的功能:

-(void)imageSlide{

if (!isMoved){
CGRect frame = self.imageSlideshow.frame;
frame.origin.x = self.imageSlideshow.frame.origin.x - 320;
self.imageSlideshow.frame = frame;
NSLog(@"1 caze work");
isMoved = YES;
}

if (isMoved){
CGRect frame = self.imageSlideshow.frame;
frame.origin.x = self.imageSlideshow.frame.origin.x + 320;
self.imageSlideshow.frame = frame;
NSLog(@"2 caze work");
isMoved = NO;
}
}

有调用该函数的 NSTimer:

[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(imageSlide)
userInfo:nil
repeats:YES];

BOOL isMoved; 放置在类的实现中。

我想要的是删除图像,然后再次显示(每 2 秒重复一次)。如果能有流畅的动画效果就好了。

那个代码:

for (int i=0; i<99; i++){

self.imageSlideshow.image = [UIImage imageNamed:(i % 2) ? @"ipd1.jpg" : @"ipd2.jpg"];

CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;

[self.imageSlideshow.layer addAnimation:transition forKey:nil];
}

也不起作用,不知道为什么。图像静止。我确实导入了 quartz 库并包含了标题。

最佳答案

您可以使用以下代码实现此目的:-

#import <QuartzCore/QuartzCore.h>
...
imageView.image = [UIImage imageNamed:(i % 2) ? @"3.jpg" : @"4.jpg"];

CATransition *transition = [CATransition animation];
transition.duration = 2.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;

[imageView.layer addAnimation:transition forKey:nil];

而 NSTimer Job 应该在这里通过 transition.duration 来完成。所以,这里不再需要 NSTimer。

礼貌:- https://stackoverflow.com/a/2834693/1865424

这段代码也可以:-

 // create the view that will execute our animation
UIImageView* imageSlideshow = [[UIImageView alloc] initWithFrame:self.view.frame];

// load all the frames of our animation
imageSlideshow.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@“ipd1.jpg"],
[UIImage imageNamed:@“ipd2.jpg"], nil];

// all frames will execute in 1.75 seconds
imageSlideshow.animationDuration = 1.75;
// repeat the animation forever
imageSlideshow.animationRepeatCount = 0;
// start animating
[imageSlideshow startAnimating];
// add the animation view to the main window
[self.view addSubview:imageSlideshow];

关于ios - 使用 NSTimer 切换图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26135064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com