2012-03-16 22 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を使用することが容易になるだろう。それ以外の場合は、UIImageViewにUIGestureRecognizerを追加する必要があります。 Interface BuilderでUIButtonを設定し、IBOutletに接続したら、この同じコードを使用して移動できます。

EDIT

あなたの問題は、あなたが新しいボタンを作成する代わりに、あなたに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

関連する問題