2014-10-11 3 views
0

나는이 곳의 답을 찾기 위해 어디에서나 검색했지만 찾은 대답은 저에게 효과가 없습니다 ... 버튼을 만들고 싶습니다. 누르면 더 무작위로. 이것은 간단하며 xcode가 문제를 해결하지 못한다는 것을 알고 있습니다. 내 코드를 실행할 때 버튼이 움직이지 않는다는 점을 제외하고는 시뮬레이터가 잘 실행됩니다. 다음은 내가 사용한 코드입니다.눌렀을 때 버튼이 무작위로 이동하는 방법

-(IBAction)Counter { 
[self moveButton]; 

}

-(void)moveButton { 
int xCo = arc4random() %200; 
int yCo = arc4random() %200; 
CGRect frame = Butoon2.frame; 
frame.origin.x = xCo; 
frame.origin.y = yCo; 

[UIView beginAnimations:nil context:nil]; 

[UIView setAnimationDuration:0.001]; 
Butoon2.frame = frame; 
[UIView commitAnimations]; 

} 귀하의 의견 주셔서 대단히

감사합니다!

+0

당신이 배울 필요가 우선은이 질문은 아무것도에 대한이 없습니다 수 있도록 xcode'는, IDE에서가 아니라 언어 '이다 'xcode IDE '를 사용하여 태그를 제거하고'objective-c' 태그를 추가했습니다. 또한 Apple 코딩 가이드 라인 (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html)을 읽으십시오.이 글을 읽으면 변수 ('Butoon2')가 소문자로 시작해야하므로'butoon2' 여야합니다. 변수와 메소드는 소문자로 시작하고 클래스는 대문자로 시작하십시오. – Popeye

+0

@Popeye 빠른 답장을 보내 주셔서 대단히 감사합니다 ... 나는 읽을 것입니다! 그것만이 잘못된 것입니까? 다른 제안 할 수 있습니까? 감사! –

답변

0

을보십시오이 :

-(IBAction)Counter:(id)sender { 
    [self moveButton]; 
} 

-(void)moveButton { 
    int xCo = arc4random() %(int)(self.view.frame.size.width-Butoon2.frame.size.width); 
    int yCo = arc4random() %(int)(self.view.frame.size.height-Butoon2.frame.size.height); 
    CGRect frame = Butoon2.frame; 
    frame.origin.x = xCo; 
    frame.origin.y = yCo; 

    [UIView beginAnimations:nil context:nil]; 

    [UIView setAnimationDuration:0.001]; 
    self.Button2.frame = frame; 
    [UIView commitAnimations]; 
} 
+0

고마워요 -'데모 '비트가 마음에 들지 않습니다. 데모가 'ViewController *'유형의 객체에 없습니다. –

+0

See my edit @JackWhitaker –

0

이 시도 :

-(void)moveButton { 
int xCo = arc4random() %200; 
int yCo = arc4random() %200; 
CGRect frame = Butoon2.frame; 
frame.origin.x = xCo; 
frame.origin.y = yCo; 

[UIView animateWithDuration:0.001 
         delay:0 
        options:UIViewAnimationOptionCurveEaseInOut 
       animations:^(void) { 
        Button2.frame = frame; 
       } 
       completion:nil]; 
} 
+0

빠른 제안을 주셔서 감사합니다 ... 불행히도 여전히 작동하지 않습니다 : '( –

+0

'Button2'의 값을 확인하십시오, 아마도 그것은 0이 아닙니다. – Levi

관련 문제