如何在视图的背景中移动图像?

时间:2012-12-19 16:23:34

标签: iphone objective-c ios image

我在我的应用程序中遇到问题我有一个动态图像,它工作正常。 但是我的图像也移动了一个按钮,当图像在按钮之前时我无法点击。如何确保图像在我的视图背景上移动,这样我仍然可以按下按钮。

这是动态影像的代码

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

2 个答案:

答案 0 :(得分: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]];
    }
}

答案 1 :(得分: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,因此对于低范围和高性能要求,您可能会考虑使用不同的算法来排除一个索引