2012-03-16 4 views
0

프로그래밍 방식으로 xCode에 사용자 지정 botton (이미지)을 삽입합니다. UIbutton을 클릭 한 후 버튼을 iPhone 화면의 맞춤 위치로 이동하려고합니다.단추를 클릭 한 후 이미지 이동

프로그래밍 방식 버튼 : 내가 클릭 한 후 버튼을 이동하기 위해 노력하고있어

self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 

    UIButton *MyButton = [UIButton buttonWithType:nil]; 

    MyButton.frame = CGRectMake(100, 170, 100, 30); 

    [MyButton setImage:[UIImage imageNamed:@"tap.png"] forState:nil]; 

    [self.view addSubview:MyButton]; 

    [MyButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 

.

-(void)buttonPressed { 
    NSLog(@"Button Pressed!"); 


    CGRect frame = MyButton.frame; 
    frame.origin.x = 100; // new x coordinate 
    frame.origin.y = 4; // new y coordinate 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration: 0.1]; 
    MyButton.frame = frame; 
    [UIView commitAnimations]; 

    } 

하지만 아무 것도 발생하지 않습니다.

i를 .h 파일

IBOutlet UIButton *MyButton; 
@property (nonatomic, retain) UIButton *MyButton; 

답변

0

이미 탭 이벤트를 수신하기 때문에, UIButton를 사용하는 것이 더 쉽습니다. 그렇지 않으면 UIGestureRecognizer을 UIImageView에 추가해야합니다. Interface Builder에서 UIButton을 설정하고 IBOutlet에 연결하면이 동일한 코드를 사용하여 이동할 수 있습니다.

편집

귀하의 문제가 새로운 버튼을 만드는 대신하여 MyButton에 속성에 할당되어있을 수 있습니다. 대신 :

UIButton *MyButton = [UIButton buttonWithType:nil]; 

시도 :

MyButton = [UIButton buttonWithType:UIButtonTypeCustom]; // UIButtonTypeCustom is what you would need to properly display your image. If you aren't setting an image for the button yet, then instead try UIButtonTypeRoundedRect. 
+0

으악 죄송 이미지가 costum있는 UIButton – Frenck

+0

당신이 가지고있는 정확한 문제에 대한 추정 할 수있다? 당신이 아주 가까이있는 것처럼 보입니다. – FreeAsInBeer

+0

메시지를 편집했습니다. @FreeAsInBeer 저를 도울 수 있기를 바랍니다. – Frenck

관련 문제