2012-11-17 70 views
1

如何檢測UIImageview是否已更改,以及如何在顯示圖像時顯示NSLog?這裏是我的代碼:監控UIImageView的圖像屬性更改

-(void)changePhotoStatus 
{ 
    NSArray *myArray = [NSArray arrayWithObjects:[NSString stringWithFormat:@"FasterText.png"],[NSString stringWithFormat:@"Don'tTouchText.png"],nil]; 

    fasttext.image = [UIImage imageNamed:[myArray objectAtIndex:arc4random() % [myArray count]]]; 

    if (fasttext.image == [UIImage imageNamed:@"Don'tToucText.png"]) { 
     NSLog(@"cahn"); 
    } 
} 

我使用NSTimer來調用圖像的更改。但不會出現NSLog。請幫助。

+0

仔細檢查你的nstimer代碼。或發佈它 –

回答

1
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changePhotoStatus) userInfo:nil repeats:YES]; 

當您完成更改圖像時,請不要忘記invalidate計時器。

+0

如果它啓動,則不需要使計時器無效。只有當你想防止它被解僱時。 –

+0

@CameronLowellPalmer Plz檢查這篇文章 - http://stackoverflow.com/questions/3499040/invalidating-an-nstimer –

+0

這篇文章幾乎是蘋果計時器編程主題的精確副本。 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html#//apple_ref/doc/uid/20000807-CJBJCBDE,正如它說的,如果它重複,你會需要自己使其無效。然而,使其失效取決於情況。 –

1

您可以使用鍵值編碼如下

- (void)registerSelfAsObserverForImageView 
{ 
    [imageView addObserver:self 
       forKeyPath:@"image" 
        options:(NSKeyValueObservingOptionNew | 
          NSKeyValueObservingOptionOld) 
       context:NULL]; 
} 


- (void)observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context 
{ 
    // Put your code here to handle event of the imageview (object) changing value :) 

    [super observeValueForKeyPath:keyPath 
         ofObject:object 
          change:change 
          context:context]; 
} 

不要忘記在dealloc的註銷,或當你不需要更新了

- (void)unregisterForChangeNotification 
{ 
    [imageView removeObserver:self forKeyPath:@"image"]; 
} 
0

這裏是一個解決方案,假設你具有正確設置視圖文件...

#import "JDemoViewController.h" 

@interface JDemoViewController() 

@property (nonatomic, retain) NSArray *imageNameArray; 

- (void) handleTimerEventFired; 

@end 

@implementation JDemoViewController 

@synthesize imageView = _imageView; 
@synthesize imageNameArray = _imageNameArray; 

- (void) dealloc 
{ 
    self.imageView = nil; 
    self.imageNameArray = nil; 
    [super dealloc]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.imageNameArray = [NSArray arrayWithObjects:@"img1",@"img2",@"img3",@"img4", nil]; 

    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(handleTimerEventFired) userInfo:nil repeats:YES]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (void) handleTimerEventFired 
{  
    NSString *tempImageName = [self.imageNameArray objectAtIndex:(arc4random()%4)]; 

    UIImage* image = [UIImage imageNamed:tempImageName]; 

    self.imageView.image = image; 

    if(tempImageName == @"img3") 
    { 
     NSLog(@"Image 3 was Loaded !"); 
    } 

} 

@end