2012-12-19 2 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

나는 당신의 질문의 문구를 수정하고 문법을 개선 좋을 것 처음에 : 귀하의 방정식은 내가 몇 가지 문서와 함께 해결하기 위해 노력했습니다 그러나 몇 군데에 결함이 거의 사운드,이다 서, 이해하기 어렵다. –

+4

들여 쓰기 설정을 변경하는 것이 좋습니다. – CodaFi

+0

나는 그 질문을 정말로 이해하지 못합니까? 다시 말해주세요. – RyanG

답변

0

코드입니다 무작위 색인보다는 오히려.

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

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

참고이 방법은 항상 한 번 arc4random 호출하고 + N은 1/최대의 확률로 호출 일이 필요하다고^N, 낮은 범위, 높은 성능 요구 사항에 대한 그래서, 당신은에 다른 알고리즘을 고려해 볼 수 있습니다 하나의 색인을 제외하십시오.

3

arc4random_uniform() 호출은 args의 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]]; 
    } 
} 
관련 문제