使用NSTimer切换图像

时间:2014-10-01 06:05:33

标签: ios objective-c nstimer

我有几个想要切换的图像。我做了一个简单的功能:

-(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];
    }

也不行,不明白为什么。图像静止不动。我做了导入石英库并包含标题。

4 个答案:

答案 0 :(得分:3)

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

#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];

答案 1 :(得分:2)

我认为你需要:

[UIView animateWithDuration:1 options:UIViewAnimationOptionCurveEaseIn 
   animations:^{
      //Change frame here.
   } completion:^ (BOOL completed) {}         
];

答案 2 :(得分:1)

试试这段代码。这对我有用。

#pragma mark -
#pragma mark viewDidAppear
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    updateBCK = [NSTimer scheduledTimerWithTimeInterval:(4.0) target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    [updateBCK fire];

}

#pragma mark -
#pragma mark viewDidDisAppear
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if ([updateBCK isValid]){
        [updateBCK invalidate];
        updateBCK = nil;
    }
}
-(void)changeImage{
    static int i=0;
    if (i == [myImages count]){
        i=0;
    }
    [UIImageView beginAnimations:nil context:NULL];
    [UIImageView setAnimationDuration:0.6];
    backgroundImageView.alpha=1; 
    backgroundImageView.image =[myImages objectAtIndex:i];

    [UIImageView commitAnimations];
    i++;
}

答案 3 :(得分:1)

您的代码中存在错误。设置isMoved = YES后,您无法检查isMoved == YES。

我不知道这是不是你的意思,但试试这段代码:

-(void)imageSlide{

    [UIView animateWithDuration:1 animations:^{
        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;
        }
        else
        {
            CGRect frame = self.imageSlideshow.frame;
            frame.origin.x = self.imageSlideshow.frame.origin.x + 320;
            self.imageSlideshow.frame = frame;
            NSLog(@"2 caze work");
            isMoved = NO;
        }

    } completion:^(BOOL finished) {
    }];
}

EDIT 也许这段代码可以帮到你:

-(void)slideshow
{
    static int i = 0;
    UIImage * img = [UIImage imageNamed:(i % 2) ? @"ipd1.jpg" : @"ipd2.jpg"];

    [UIView transitionWithView:self.imageSlideshow duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.imageSlideshow.image = img;
    } completion:^(BOOL finished) {
        i++;
        [self performSelector:@selector(slideshow) withObject:nil afterDelay:2.0];
    }];
}