-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;
}
}
4

2 に答える 2

3

問題は、arc4random_uniform()呼び出しが引数の 0 ベースのカウントではなく、上限を取るという事実にあります (巧妙に言うと、外挿されます)。あなたの方程式はほぼ健全ですが、いくつかの場所で欠陥があり、いくつかのドキュメントで修正しようとしました:

-(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]];
    }
}
于 2012-12-19T16:50:43.007 に答える
0

現在のインデックスではないランダム インデックスを探していると思います。これにより、単なるランダム インデックスではなく、ランダムな別のインデックスを選択できるようになります。

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

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

このアプローチでは、常に arc4random が 1 回呼び出され、1/max^N の確率で 1+N 回の呼び出しが必要になることに注意してください。そのため、範囲が狭く、パフォーマンス要件が高い場合は、別のアルゴリズムを検討して 1 つのインデックスを除外することを検討してください。

于 2012-12-19T16:40:13.937 に答える