UIView/UIControl在点击时改变屏幕位置
问题描述:
我很清楚这是一个简单的问题。我想了解如何在用户选择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{
???
}
答
我认为你需要添加一个:
在@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];
}
答
CGRectOffset(rect, dx, dy)
由Delta X及Y.这个
两个问题的移动RECT的起源,当我进入前代码它告诉我,“的CGRect没有名为“成员x'“和'y'一样,我忘了添加一些东西。而且这也是一个更基本的问题,但顶端添加的(id)发件人是什么?非常感谢。 – 2011-01-20 23:05:16