2011-01-20 23 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

我認爲你需要添加一個:@selector(buttonClicked:)聲明,因爲IBAction小號想到一個(id)sender方法屬性。

你可以做類似於下面的代碼。

- (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

兩個問題的移動RECT的起源,當我進入前代碼它告訴我,「的CGRect沒有名爲「成員x'「和'y'一樣,我忘了添加一些東西。而且這也是一個更基本的問題,但頂端添加的(id)發件人是什麼?非常感謝。 – 2011-01-20 23:05:16

1

CGRectOffset(rect, dx, dy)

由Delta X及Y.這個