2012-12-19 45 views
-3

我在我的應用程序中有一個問題,我有一個移動圖像,它工作正常。 但是我的圖像也在移動一個按鈕,當圖像位於按鈕之前時,我無法點擊。我如何確保圖像在我的視圖背景上移動,以便我仍然可以按下按鈕。如何在視圖的背景中移動圖像?

這是運動圖像

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [[self navigationController] setNavigationBarHidden:YES animated:YES]; 
    [self loadImage]; 
    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cow.png"]]; 
    image.frame =self.view.bounds; 
    [[self view] addSubview:image]; 


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



} 



-(void) moveImage: (NSTimer*)timer { 

    CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width); 
    CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height); 
CGPoint pointOne=CGPointMake(x,y); 
    image.center=pointOne; 
} 
} 
+1

在我建議改寫你的問題,提高其語法首先,因爲它是目前站立,很難理解。 – 2012-12-19 16:26:46

+4

改變縮進設置可能也是一個好主意。 – CodaFi

+0

我真的不明白這個問題嗎?請重新說明。 – RyanG

回答

0

我猜你要找的是不是當前索引,這樣您就可以選擇一個隨機的,不同的指數隨機指數的代碼,而不僅僅是一個隨機索引。

- (NSUInteger)randomUnsignedLessThan:(NSInteger)max excluding:(NSUInteger)exclude { 

    NSInteger firstTry = -1; 
    while (firstTry == exclude) firstTry = arc4random() % max; 
    return firstTry; 
} 

注意,這種方法將總是調用arc4random一次,需要1 + N 1/MAX的概率來電^ N,所以對於低範圍和性能要求高,你可能會考慮不同的算法來排除一個索引。

3

問題在於arc4random_uniform()調用的上限,而不是基於0的args計數(因爲您巧妙地有人會說,外推)。你的公式是近聲,但在少數地方有缺陷,我已經試過了一些文件,以糾正:

-(void)someAction:(id)sender { 
    NSInteger imageIndex1 = arc4random_uniform(self.images.count); //upper bounds, so no -1 
    NSInteger imageIndex2 = arc4random_uniform(self.images.count); //upper bounds, so no -1 
    NSInteger imageIndex3 = arc4random_uniform(self.images.count); //upper bounds, so no -1 
    _Bool b1 = true, b2 = true, b3 = true; //I can only assume you've been declaring GNU C booleans because of the lowercase false. 
               //be careful, in Objective-C land, BOOL is signed char. 
    if (imageIndex1 == imageIndex2) { 
     b1 = false; 
     imageIndex2 = arc4random_uniform(self.images.count); //upper bounds, so no -1 
    } 
    if(imageIndex1 == imageIndex3) { 
     b2 = false; 
     imageIndex1 = arc4random_uniform(self.images.count); //upper bounds, so no -1 
    } 
    if (imageIndex2 == imageIndex3) { 
     b3 = false; 
     imageIndex3 = arc4random_uniform(self.images.count); //upper bounds, so no -1 
    } 
    //You have to use ors here, otherwise your UI will never actually update, considering that in checking 
    //for unique factors, then reassigning to another unique factor if a check fails, one of them has got 
    //to be true before the UI can update, rather than all 3 at once. 
    //Perhaps an individual check of each boolean would be more effective. 
    if(b1 == true || b2 == true || b3 == true) { 
     [self.picture1 setImage:self.images[imageIndex1]]; 
     [self.picture2 setImage:self.images[imageIndex2]]; 
     [self.picture3 setImage:self.images[imageIndex3]]; 
    } 
}