2014-10-01 110 views
0

我有幾張圖片想切換。有一個簡單的功能,我對取得這樣做:使用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]; 
    } 

而且不工作,不知道爲什麼。圖像靜止不動。我沒有導入石英庫幷包含標題。

+0

http://stackoverflow.com/questions/7638831/fade-dissolve-when-changing-uiimageviews-image/38350024#38350024 – 2016-07-13 11:27:17

回答

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工作應該由這裏來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]; 
+0

我代表什麼? – 2014-10-01 08:41:59

+0

愛德華,請看看我更新的答案,我想知道爲什麼它不起作用。謝謝。 – 2014-10-01 09:36:36

+0

@EvgeniyKleban我用更簡單的代碼編輯了我的答案,檢查它..它像一個魅力:) – Shivaay 2014-10-01 11:12:10

2

我想你需要:

[UIView animateWithDuration:1 options:UIViewAnimationOptionCurveEaseIn 
    animations:^{ 
     //Change frame here. 
    } completion:^ (BOOL completed) {}   
]; 
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++; 
} 
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) { 
    }]; 
} 

編輯 也許這個代碼將幫助您:

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

帕維爾,感謝您的幫助,但沒有檢查條件的錯誤(因爲我可以看到輸出在NSLog中)。 – 2014-10-01 06:47:32

+0

@EvgeniyKleban在你的代碼中同時執行兩個語句「if(!isMoved)」和「if(isMoved)」。 這是你所期望的嗎? – 2014-10-01 06:54:23

+0

Pawel,是的,因爲在實現文件中聲明瞭布爾值,所以每次函數被調用時都會執行不同的語句。在我的代碼中,它每2秒發生一次。 – 2014-10-01 07:54:10