2011-01-20 18 views
4

これは簡単な質問です。 CGRect/UIButtonをユーザーが選択したときに画面上の別の場所に移動する方法を学びたいと思います。あなたの助けを前もってありがとう。タップしたときのUIView/UIControlの画面位置の変更

- (void)showMeSomeButtons:(CGRect)frame{ 

    button = [[UIButton alloc] initWithFrame:frame]; 
    button.frame = CGRectMake(240, 150, 50, 50); 

    UIImage *buttonGraphic = [UIImage imageNamed:@"1.png"]; 

    [button setBackgroundImage:buttonGraphic forState:UIControlStateNormal]; 
    [button addTarget:self.viewController action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    [self addSubview:button]; 
} 

-(void)buttonClicked{ 

    ??? 
} 

答えて

14

私はあなたがIBAction sが単一(id)sender method属性を期待しているので@selector(buttonClicked:)文で:を追加する必要があると思います。

次のようなコードを実行できます。

- (void)buttonClicked:(id)sender { 
    CGRect frame = button.frame; 
    frame.origin.x = 500; // new x coordinate 
    frame.origin.y = 500; // new y coordinate 
    button.frame = frame; 
} 

アニメーションを作成する場合は、beginAnimationブロックを追加できます。

- (void)buttonClicked:(id)sender { 
    CGRect frame = button.frame; 
    frame.origin.x = 500; // new x coordinate 
    frame.origin.y = 500; // new y coordinate 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration: 0.25]; 
    button.frame = frame; 
    [UIView commitAnimations]; 
} 
+0

二本に関する質問、私はそれはCGRectは "という名前のメンバーを持っていない」と言われますトップのコードを入力します。 x ''と 'y'と同じものは何かを追加することを忘れました。また、これはより基本的な質問ですが、一番上の行に(id)送信者が追加されましたか?本当にありがとう。 –

+0

申し訳ありませんが、私の答えを更新しました。実際にはframe.origin.xです。 – Gianz

+0

[docs](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/eventoverview/EventHandlingBasics/EventHandlingBasics.html)によると、IBActionには次の要件があります。 "[...]戻り値の型はvoidの残りの部分は、idと入力された(慣習的に)送信者という名前の1つのパラメータです。 "コロンは**オプションではありません。それに応じてあなたの答えを編集しました。あなたが気にしないことを願っています。 – Regexident

1

CGRectOffset(rect, dx, dy)

は、デルタXでRECTの起源を移動し、Y.

関連する問題