-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 的 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]];
    }
}
于 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+N 次调用,概率为 1/max^N,因此对于低范围和高性能要求,您可能会考虑使用不同的算法来排除一个索引。

于 2012-12-19T16:40:13.937 回答